Вы находитесь на странице: 1из 18

Gaddis Starting Out With Java 5 From Control Structures to Objects Chapter 07 A First Look At GUI Applications Multiple

ple Choice
1. ____ is a library of classes that do not replace ____, but provide an improved alternative for creating GUI applications. a. AWT, Swing b. Swing, AWT c. JFC, AWT d. JFC, Swing ANS: B 2. Programs that operate in a GUI environment must be a. Event driven b. In color c. Dialog boxes d. Layout managers ANS: A

7. To use the ActionListener interface, as well as other event listener interfaces, you must have the following import statement in your code: a. import java.swing; b. import java.awt; c. import java.awt.*; d. import java.awt.event.*; ANS: D 8. Assume that message, contentPane, and panel have been created and initialized. In what order would you execute the following statements to display message on a JFrame object? 1. panel.add(message); 2. setVisible(true); 3. contentPane getContentPane(); 4. contentPane.add(panel); a. 1, 2, 3, 4 b. 2, 1, 3, 4 c. 3, 4, 1, 2 d. 1, 3, 4, 2

panel.add(message); contentPane getContentPane(); contentPane.add(panel); setVisible(true);

ANS: D 9. When you write an action listener class for a JButton component, it must a. Have a method named actionPerformed b. Implement the ActionLIstener interface c. The method actionPerformed must take an argument of the ActionEvent type d. All of the above ANS: D 10. A common technique for writing an event listener class is to write it as a private inner class inside the class that creates the GUI. a. True b. False ANS: A 11. In a Swing application, you create a frame object from the a. Jlabel class. b. JFrame class. c. Jpanel class. d. AbstractButton class. ANS: B 12. To use the Color class, which is used to set the foreground and background of various objects, use the following import statement a. import java.swing; b. import java.awt; c. import java.awt.*; d. import java.awt.event.*; ANS: C 13. The ____ layout manager arranges components in rows. a. GridLayout b. BorderLayout c. FlowLayout d. RegionLayout ANS: C 14. If panel is a JPanel object, which of the following statements adds the GridLayout to it? a. panel.setLayout(new GridLayout(2,3)); b. panel.addLayout(new GridLayout(2,3)); c. panel.GridLayout(2,3);

d. ANS: A

panel.attachLayout(GridLayout(2,3));

15. The following statement adds the FlowLayout manager to the container, contentPane, centers the components, and separates the components in a row by 8 pixels and the rows by 10 pixels. contantPane.setLayout(new FlowLayout()); a. True b. False ANS: A 16. When using the BorderLayout manager, how many components can each region hold? a. 1 b. 2 c. 5 d. No limit ANS: A 17. The GridLayout manager limits each cell to only one component. To put two or more components in a cell a. Resize the cells so they can hold more b. You can nest panels inside the cells, and add other components to the panels c. The first statement is false, the GridLayout manager does not have this restriction d. Resize the components to fit in the cell ANS: B 18. Which of the following statements is not true? a. Radio buttons are round and Check boxes are square. b. Radio buttons are often grouped together and are mutually exclusive; Check boxes are not c. Radio buttons and Check boxes both implement ActionListener d. They are all true ANS: C 19. How many buttons can be selected at the same time as the result of the following code? hours new JRadioButton(Hours); minutes new JRadioButton(Minutes); seconds new JRadioButton(Seconds); days new JRadioButton(Days); months new JRadioButton(Months); years new JRadioButton(Years); timeOfDayButtonGroup new ButtonGroup(); dateButtonGroup new ButtonGroup(); timeOfDayButtonGroup.add(hours); timeOfDayButtonGroup.add(minutes); timeOfDayButtonGroup.add(seconds); dateButtonGroup.add(days);

dateButtonGroup.add(months); dateButtonGroup.add(years); a. 1 b. 2 c. 3 d. 4 ANS: B 20. Although check boxes may be grouped in a ButtonGroup like radio buttons are, they are not normally grouped as such. a. True b. False ANS: A 21. To click the JRadioButton variable, radio, code the following a. radio.Click(); b. Click(radio); c. Click(radio, true); d. radio.doClick(); ANS: D 22. To add button group, bGroup, to a Panel, panel, a. use the statement, panel.add(bGroup); b. use the statement, bGroup.add(panel); c. use the statement, Panel panel new Panel(bGroup); d. add each button to panel one at a time, e.g. panel.add(button1); ANS: D 23. What will be the result of executing the following statement? panel.setBorder(BorderFactory.createLineBorder(Color.blue, 5)); a. The JPanel referenced by panel will have a blue line border that is 5 millimeters thick. b. The JPanel referenced by panel will have a blue line border that is 5 pixels thick. c. The JPanel referenced by panel will have a blue line border that is 5 characters thick. d. The JPanel referenced by panel will have a blue line border that is 5 inches thick. ANS: B 24. When an application uses many components, rather than deriving just one class from the Jframe class, it is often better to encapsulate smaller groups of related components and their event listeners into their own class. A commonly used technique to do this is a. To derive a class from the JAbstractButton class to contain other components and their related code b. To derive a class from the JComponent class to contain other components and their related code c. To derive a class from the JPanel class to contain other components and their related code d. To derive a class from the JFrame class to contain other components and their related code ANS: C

25. You should not have an event listener for a ButtonGroup, if you do not want to execute any code when the user selects a button from the group. a. True b. False ANS: A 26. To include Swing and AWT components in your program, use the following import statements a. import java.swing.*; import java.awt.*; b. import java.swing.*; import javax.awt.*; c. import javax.swing.*; import java.awt.*; d. import javax.swing.*; import javax.awt.*; ANS: C 27. ____ components are coupled with their underlying peers. a. Lightweight b. Featherweight c. Middleweight d. Heavyweight ANS: D

30. The System.exit method will end the application. a. True b. False ANS: A 31. A ____ is a basic window that has a border around it, a title bar, and a set of buttons for minimizing, maximizing, and closing the window. a. Pane b. Container c. Frame d. Dialog box ANS: C 32. Which of the following statements creates a class that derived from the JFrame class? a. JFrame DerivedClass new JFrame(); b. class JFrame DerivedClass; c. JFrame(DerivedClass); d. public class DerivedClass extends JFrame{} ANS: D 33. What does the following statement do? addButton.addActionListener(new AddButtonListener());

a. Creates a AddButtonListener object b. Registers the AddButtonListener object with the addButton c. Creates a AddButtonListener object and registers the AddButtonListener object with the addButton d. Nothing, the statement is invalid ANS: C 34. The ActionEvent argument that is passed to an action listeners actionPerformed method is the event object that was generated in response to an event. a. True b. False ANS: A 35. Event listeners must a. implement an interface b. be included in private inner classes c. not receive any arguments d. exit the application once it has handled the event ANS: A 36. If button1 is a JButton object, which of the following statements will make its background blue? a. button1.makeBackground(blue); b. button1.setBackground(Color.blue); c. button1.makeBackground(Color.blue); d. button1.set.Background(blue); ANS: B 37. The ____ layout manager arranges components in five regions. a. GridLayout b. BorderLayout c. FlowLayout d. RegionLayout ANS: B 38. Which of the following is not a rule for the FlowLayout manager? a. Multiple components can be added to a container that uses a FlowLayout manager b. New components will be added in a row from left to right c. When there is no more room in a row, additional components are put on the next row d. All of the above are rules for the FlowLayout manager ANS: D 39. The FlowLayout manager does not allow the programmer to align components. a. True b. False

ANS: B 40. When a component is added to a region in the BorderLayout manager, a. The component retains its original size b. It results in a compile time error, if it is too large c. The component is stretched so it fills up the entire region d. The region is resized to fit the component ANS: C 41. When adding components to a container that is governed by the GridLayout manager, a. You cannot specify a cell b. You specify the cell with the row and column numbers in the add statement c. You must add them starting with the lower, right cell d. The components are added automatically by filling up the first column, then the second, etc. ANS: A 42. Which of the following statements is not true? a. Radio buttons are round and Check boxes are square. b. Radio buttons are often grouped together and are mutually exclusive; Check boxes are not c. Radio buttons implement ActionListener; Check boxes implement ItemListener d. They are all true ANS: D 43. Why doesnt the following code compile correctly? import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ColorCheckBoxWindow extends JFrame { private Container contentPane; private JCheckBox greenCheckBox; private final int WINDOW_WIDTH 300, WINDOW_HEIGHT 100; public ColorCheckBoxWindow() { super(Green Check Box); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); greenCheckBox new JCheckBox(Green); greenCheckBox.addItemListener(new CheckBoxListener()); contentPane getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(greenCheckBox); setVisible(true); } public void itemStateChanged(ItemEvent e) { if (e.getSource( greenCheckBox)

{ System.exit(); } } } a. b. c. d. ANS: C 44. The ItemListener interface is in the java.swing.* package. THEN WHERE ?????????? a. True b. False ANS: B 45. To determine if the JCheckBox variable, checkBox, has been selected, code the following a. if (isSelected(checkBox)) {code to execute, if selected} b. if (checkBox.isSelected()) {code to execute, if selected} c. if (checkBox) {code to execute, if selected} d. If (checkBox.doClick()) {code to execute, if selected} ANS: B 46. To add button group, bGroup, to a Panel, panel, a. use the statement, panel.add(bGroup); b. use the statement, bGroup.add(panel); c. use the statement, Panel panel new Panel(bGroup); d. add each button to panel one at a time, e.g. panel.add(button1); ANS: D 47. What will be the result of executing the following statement? panel.setBorder(BorderFactory.createTitledBorder(Title)); a. The JPanel referenced by panel will have an etched border with the title Title displayed on it. b. The JPanel referenced by panel will have an empty border with the title Title displayed on it. c. The JPanel referenced by panel will have a line border with the title Title displayed on it. d. The JPanel referenced by panel will have a compound border with the title Title displayed on it. ANS: A 48. When an application uses many components, instead of deriving just one class from the Jframe class, a better approach is to a. Break the application into several smaller applications b. Reconsider the design of the application c. Encapsulate smaller groups of related components and their event listeners into their own classes d. Just go ahead and do it in one large class ColorCheckBoxWindow is not implementing the correct listener The button cannot be added to the content pane The itemStateChanged method should be coded in a CheckBoxListener class greenCheckBox should not be a private member

ANS: C l 49. Whenever you include a Button or ButtonGroup in your code, you should always include an event listener. a. True b. False ANS: B

Gaddis Starting Out With Java 5 From Control Structures to Objects Chapter 13 Advanced GUI Applications Multiple Choice
1. What will be the results of executing the following statements? x.setEditable(true); x.setText(Tiny Tim); a. The text field x will have the value Tiny Tim and be read only b. The text field x will have the value Tiny Tim and the user will be able to change its value c. The text field x will have the value true d. The text field x have the value trueTiny Tim ANS: B 2. The default selection mode for a JList component is a. Single selection b. Single interval selection c. Multiple interval selection d. There is no default setting ANS: C 3. When an item in a Jlist object is selected it generates a(n) a. Action listener event b. Action performed event c. List selection listener event d. List selection event ANS: D 4. To determine which item in a list is currently selected use a. The getSelectedIndex method b. The getSelectedValue method c. Both (a) and (b) d. Neither (a) or (b) ANS: C

5. To add the JList object addressList to a scroll pane called scrollPane, use the following a. JScrollPane scrollPane new JScrollPane(addressList); b. scrollPane.add(addressList); c. addressList.add(scrollPane); d. addressList.addScrollPane(scrollPane); ANS: A 6. By default the scroll bars are always displayed. a. True b. False ANS: B 7. If addressList is a JList component, what will the value of x be after the following statement is executed: x addressList.getSelectedIndex(); a. The value of the first selected item b. The index of the first selected item c. An array of objects containing the items selected d. An array containing the indices of all the selected items in the list ANS: B 8. When an item in the combo box is selected, the combo box executes its action event listeners actionPerformed method, passing a. an ItemEvent object as an argurment b. a SelectionEvent object as an argurment c. an ActionEvent object as an argument d. the combo box as an argument ANS: C 9. If the combo box addressBox contains a list of strings, why is the returned value of getSelectedItem method in the following code cast to String? String selectedAddress; selectedAddress (String) addressBox.getSelectedItem(); a. This is not necessary. b. Because the syntax of the method requires it. c. It makes the program more readable d. Because the return type of the method is an Object ANS: D 10. If a user enters a value in the text box of an editable text box and the value is not in the list, what will the method getSelected Index return? a. The index of the item that the user replaced b. The index of the item the user entered c. 1 d. 0, since the item is not in the list

ANS: C 11. The ImageIcon class supports all the following file types, except a. BMP b. JPEG c. GIF d. PNG ANS: A 12. When creating images, the argument passed to the image parameter in the JLabel constructor can be an ImageIcon object, or any object that implements the Icon interface. a. True b. False ANS: A 13. What will display when the following code is executed: JLabel label new JLabel (It is a beautiful day.); labed.setIcon(SunnyFace.gif); a. A label with the text It is a beautiful day. to the left of the SunnyFace image. b. A label with the text It is a beautiful day. to the right of the SunnyFace image. c. A label with the text It is a beautiful day. below the SunnyFace image. d. A label with the text It is a beautiful day. above the SunnyFace image. ANS: B 14. To force the JFrame that encloses a window to resize itself automatically when a larger object is placed in the frame, use the a. The pack method b. The resize method c. The grow method d. The enlarge method ANS: A 15. A mnemonic is a. A key on the keyboard that you press to quickly access a component such as a button b. A key on the keyboard that you press in combination with the SHIFT key to quickly access a component such as a button c. A key on the keyboard that you press in combination with the ALT key to quickly access a component such as a button d. A key on the keyboard that you press in combination with the CTRL key to quickly access a component such as a button ANS: C 16. To add a tool tip to a component use a. The setToolTipText method b. The addToolTipText method c. The toolTipText method

d. ANS: A

The appendToolTipText method

17. The default directory for the JFileChooser constructor is a. Your login directory if you are using UNIX b. Probably My Documents if you are using Windows c. Neither (a) or (b) d. Both (a) and (b) ANS: D 18. What will happen when the following code is executed? JPanel panel new JPanel(); Color selectedColor; JColorChooser colorChooser new JColorChooser(); selectedColor colorChooser.showDialog(null, Select color, Color.blue); a. A Color Box will be displayed in the upper, left-hand corner of the screen with Select color in its Title Bar and blue pre-selected b. A Color Box will be displayed in the center of the screen with Select color in its Title Bar and blue pre-selected c. A Color Box will be displayed in the center of the screen with Select color as the OK buttons text and blue pre-selected d. A Color Box will be displayed in the lower, right-hand corner of the screen with Select color in its Title Bar and blue pre-selected ANS: B 19. A menu system may consist of each of the following, except a. Check box menu item b. Combo box menu item c. Radio button menu item d. Menu item ANS: B 20. Which of the following is not a class used in constructing a menu system? a. JMenuItem b. JCheckBoxMenuItem c. JButton d. JRadioButtonMenuItem ANS: C 21. In the following code the setPreferredSize method sets the size of the text, Have a good day. label new Jlabel(Have a good day, SwingConstants.CENTER); label.setPreferredSize(new Dimension(400,200)); a. True b. False

ANS: B 22. The Dimension class is part of the a. javax.swing.event.* package b. java.awt.* package c. javax.swing.* package d. java.awt package ANS: D 23. Arrange the following sets of code in the correct order to create a JMenu object consisting of one JMenuItem object. 1 normalItem new JMenuItem(Normal); normalItem.setMnemonic(KeyEvent,VK_N); normalItem.addActionListener(new NormalListener()); 2 viewMenu new JMenu(View); viewMenu.setMnemonic(KeyEvent.VK_V); 3 viewMenu.add(normalItem);

24. What will the following code do when it is executed? JTextArea message JTextArea(greetings, 50, 70); JScrollPane scrollPane new JScrollPane(message); a. It will create a JScrollPane object for the JTextArea object message and display a horizontal scroll bar on the text area b. It will create a JScrollPane object for the JTextArea object message and display a vertical scroll bar on the text area c. It will create a JScrollPane object for the JTextArea object message and display both vertical and horizontal scroll bars on the text area d. It will create a JScrollPane object for the JTextArea object message and display no scroll bars on the text area ANS: C 25. When using a slider, by default, tick marks are not displayed, and setting their spacing does not cause them to be displayed. a. True b. False ANS: A 26. What will happen when the following statement is executed? x.setEditable(false); a. The boolean variable x will be set to false b. The text field x will be made read-only c. The text field x will be editable d. The boolean variable x will be editable ANS: B 27. When you create an instance of the JList class,

a. b. c. d. ANS: A

You pass an array of objects to the constructor The list has a default selection mode of Single Selection Mode The text field x will be editable You must indicate the maximum number of list items the user may select at any one time

28. The following statement textList.setSelectionMode(ListSelectModel.SINGLE_INTERVAL_SELECTION); a. Sets the textList component to single selection mode b. Sets the textList component to ListSelectModel c. Sets the textList component to single interval selection mode d. Sets the value of textList to SINGLE_INTERVAL_SELECTION ANS: C 29. The getSelectedIndex method returns a. The index of the selected item b. 1 if no item is selected c. Both (a) and (b) d. Neither (a) or (b) ANS: C 30. If nameList is a JList component, use the following statement to display 4 rows in nameList a. nameList.setRowCount(4); b. nameList.setDisplayRowCount(4); c. nameList.setShowRowCount(4); d. nameList.setVisibleRowCount(4); ANS: D 31. When a JList component is added to a JScrollPane object, a border will automatically appear around the list. a. True b. False ANS: A

32. If addressList exists and has values stored in it and addrList is an array with values stored in it, whatwill be the results when the following code is executed? addressList.setListData(addrList); a. The values of addrList will be attached at the end of the current addressList b. The values in addressList will be replaced by the values in addrList c. The values of addrList will be inserted at the beginning of the current addressList d. addressList will be unchanged ANS: B

33. The JComboBox class is in the a. javax.swing package b. java.awt.* package c. java awt.event.* package d. java.swing.event.* package ANS: A 34. If the combo box, addressList, contains the array, addresses, what does the following code do? int index; String address; index addressList.getSelectedIndex(); address addresses[index]; a. It returns the value of the selected item and stores it in the String address. b. It returns the index of the selected item, then stores the index of the selected item in address. c. It returns the index of the selected item, then stores the value of address in that location in the combo box d. It returns the index of the selected item, then stores the reference of the selected item in address. ANS: D 35. If a user enters a value in the text box of an editable text box and the value is not in the list, what will the method getSelected Item return? a. The value that the user replaced b. The index of the item the user entered c. The value that appears in the text field, even if it is not in the combo boxx list d. Nothing, since the item is not in the list ANS: C 36. The ImageIcon class constructor accepts a. An integer that references the image b. A String argument, which is the name of the image file c. A String argument, which is the name of the image d. It does not accept any arguments ANS: B 37. You can create a label with an image, or with both an image and text. a. True b. False ANS: A 38. What will display when the following code is executed: JButton button new JButton (It is a beautiful morning.); button.setIcon(SunnyFace.gif); a. A button with the text It is a beautiful morning. to the left of the SunnyFace image. b. A button with the text It is a beautiful morning. to the right of the SunnyFace image. c. A button with the text It is a beautiful morning. below the SunnyFace image. d. A button with the text It is a beautiful morning. above the SunnyFace image.

ANS: B 39. What will display when the following code is executed: imagePanel new JPanel(); imageLabel new JLabel(); imagePanel.Add(imageLabel); ImageIcon sunnyFaceImage new ImageIcon(SunnyFace.gif); imageLabel.setIcon(sunnyFaceImage); pack(); a. The JFrame that encloses the window will resize itself to accommodate the SunnyFace image b. imagePanel will resize itself to accommodate the SunnyFace image c. The SunnyFace image will resize itself to fit on imageLabel d. The SunnyFace image will resize itself to fit on imagePanel ANS: A 40. Which of the following assigns ALTC as a mnemonic key for the JButton object, clearButton? a. clearButton.addMnemonic(KeyEvent.VK_C); b. clearButton.setMnemonic(KeyEvent.C); c. clearButton.setMnemonic(KeyEvent.VK_C); d. clearButton.assignMnemonic(KeyEvent.VK_C); ANS: C 41. A tool tip is a. A guide provided by the Help menu as to which tool to use for various programming techniques b. Another name for the Tool Bar c. Used to make programming easier d. Text that is displayed in a small box when the user holds the mouse cursor over a component ANS: D 42. If the argument passed to the showOpenDialog method is null, a. The dialog box will normally be displayed in the upper, left-hand corner of the screen b. The dialog box will normally be centered in the screen c. Nothing will be displayed because there was nothing passed to the method d. 1 will be returned from the method ANS: B 43. What will be the results of executing the following code, if the user clicks OK? JPanel panel new JPanel(); Color selectedColor; JColorChooser colorChooser new JColorChooser(); selectedColor colorChooser.showDialog(null, Select color, Color.blue); panel.setForeground(selectedColor); a. The foreground color will remain unchanged b. The foreground color will be set to white c. The foreground color will be set to black

d. ANS: D

The foreground color will be set to blue

44. A menu system may consist of each of the following except a. Menu item b. Separator bar c. Menu bar d. It may have all the above ANS: D 45. Which of the following is not a class used in constructing a menu system? a. JMenuItem b. JCheckBoxMenuItem c. JComboBoxMenuItem d. JRadioButtonMenuItem ANS: C 46. Normally, a labels preferred size is determined by the setPreferredSize method. a. True b. False ANS: B 47. Arrange the following sets of code in the correct order to create a JMenu object consisting of one JMenuItem object. 1. copyItem new JMenuItem(Copy); copyItem.setMnemonic(KeyEvent,VK_C); copyItem.addActionListener(new CopyListener()); 2. editMenu new JMenu(Edit); editMenu.setMnemonic(KeyEvent.VK_E); 3. editMenu.add(copyItem); a. 3, 2, 1 b. 2, 1, 3

c. d. ANS: B

1, 3, 2 2, 3, 1

48. The JMenuBar component can contain a. JFrame components b. JPanel components

c. d. ANS: C

JMenu components JTextComponent components

49. What does the following statement do? JTextArea textField JTextArea(message, 25, 15); a. It creates a text area with 25 columns and 15 rows that will initially display the text stored in the String object message b. It creates a text area with 25 columns and 15 rows that will initially display the text stored in the Text area textField c. It creates a text area with 25 rows and 15 columns that will initially display the text stored in the String object message d. It creates a text area with 25 rows and 15 columns that will initially display the text message ANS: C 50. When you write a change listener class method for a slider, it must implement the ChangeListener interface which is in the javax.swing.event package. a. True b. False ANS: A