Deck 10: Graphical User Interfaces

Full screen (f)
exit full mode
Question
Which container is used to group multiple user-interface components together?

A) text area
B) table
C) panel
D) rectangle
Use Space or
up arrow
down arrow
to flip the card.
Question
Which of the following is an event source?

A) A JButton object.
B) An event listener.
C) An inner class.
D) An event adapter.
Question
What is the nickname for the graphical user interface library in Java?

A) Applet
B) GUI
C) JComponent
D) Swing
Question
Which statement should be added to this code fragment to ensure that the frame is shown?
JFrame frame = new JFrame();

A) frame.setVisible(true);
B) frame.visible = true;
C) JFrame.setVisible();
D) frame.setVisible();
Question
A/an ____ object contains methods that describe the actions to be taken when a user clicks a user-interface graphical object.

A) Event listener
B) Event source
C) Action listener
D) Action method
Question
Place drawing instructions inside the __________ method, which is called whenever the component needs to be repainted.

A) paintComponent
B) draw
C) paint
D) drawComponent
Question
To associate an event listener with a JButton component, you must use the ___ method of the JButton class.

A) addEventListener
B) addActionListener
C) addButtonListener
D) addListener
Question
When an event occurs, the event source notifies all associated ____.

A) components
B) panels
C) interfaces
D) event listeners
Question
Which of the following statements should be added to this code fragment to set the frame size to a width of 400 and a height of 200?
Final int FRAME_WIDTH = 400;
Final int FRAME_HEIGHT = 200;
JFrame frame = new JFrame();

A) frame.size = (FRAME_WIDTH, FRAME_HEIGHT);
B) frame.addSize(FRAME_WIDTH, FRAME_HEIGHT);
C) frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
D) frame.setSize(FRAME_HEIGHT, FRAME_WIDTH);
Question
Based on the following statement, which of the following statements sets the title of the frame:
JFrame frame = new JFrame();

A) frame.title = "An Empty Frame";
B) frame.setTitle(JFrame.EMPTY);
C) frame.addTitle("An Empty Frame");
D) frame.setTitle("An Empty Frame");
Question
To respond to a button event, a listener must supply instructions for the ____ method of the ActionListener interface.

A) actionEvent
B) actionPerformed
C) eventAction
D) eventResponse
Question
Complete the following statement to construct a circle.
Graphics g;
G)____________________(x, y, width, height);

A) drawCircle
B) drawOval
C) fillCircle
D) DrawOval
Question
When added to the code below, which statement will set the color of the next item drawn to green?
Public class ItalianFlagComponent extends JComponent
{
Public void paintComponent(Graphics

A) g.setColor(GREEN);
B) g.setColor(0, 255, 0);
C) g.setColor(Color.GREEN);
D) g.setColor("GREEN");
G) {
G)drawRect(100, 100, 30, 60);
) . .
____________________________
) . .
}
}
Question
Which of the following statements about events and graphical user interface programs is true?

A) Your program must indicate the specific types of events to which it wishes to respond.
B) Your program will automatically receive notifications about all events that have occurred.
C) Your program must respond to notifications of all types of events that are sent to it while it is running.
D) Your program must override the default methods to handle events.
Question
The methods of a/an ____ describe the actions to be taken when an event occurs.

A) event source
B) event listener
C) event interface
D) action source
Question
Consider the following code snippet:
JPanel panel = new JPanel();
JFrame frame = new JFrame();
JButton button = new JButton("Click me");
JLabel label = new JLabel("Show the answer");
Frame.add(label);
Frame.add(button);
Panel.add(frame);
Which of the following statements is true?

A) This code will correctly build the user interface.
B) The button and label should be added to the panel first, and then the panel should be added to the frame.
C) The frame should be added to the panel before adding the button and label to the frame.
D) The panel should be added to the frame first, and then the button and label should be added to the panel.
Question
When drawing complex shapes, provide a(n) ____ to set the position of the shape.

A) constructor
B) viewer
C) component
D) frame
Question
Which of the following statements about listener classes is true?

A) A listener class can be declared as an anonymous inner class.
B) A listener class cannot be declared as an inner class.
C) A listener class must be declared as an inner class.
D) A listener class must be declared as an anonymous inner class.
Question
____ are generated when the user presses a key, clicks a button, or selects a menu item.

A) Listeners
B) Interfaces.
C) Events.
D) Errors.
Question
Which of the following statements will compile without error?

A) public interface AccountListener()
{
Void actionPerformed(AccountEvent event);
}
B) public interface AccountListener()
{
Void actionPerformed(AccountEvent);
}
C) public interface AccountListener
{
Void actionPerformed(AccountEvent event);
}
D) public abstract AccountListener
{
Void actionPerformed(AccountEvent event);
}
Question
How do you specify what the program should do when the user clicks a button?

A) Specify the actions to take in a class that implements the ButtonListener interface.
B) Specify the actions to take in a class that implements the ButtonEvent interface .
C) Specify the actions to take in a class that implements the ActionListener interface.
D) Specify the actions to take in a class that implements the EventListener interface.
Question
Consider the following code snippet:
Public class ClickListener implements ActionListener
{
Public void actionPerformed(ActionEvent event)
{
System.out.println("I was clicked.");
}
}
Public class ButtonTester
{
Public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
Frame.add(button);
ActionListener listener = new ClickListener();
Button.addActionListener(listener);
)..
}
}
Which of the following statements is correct?

A) Class ButtonTester is an interface type.
B) A ClickListener object listens for the action events that buttons generate.
C) Class ClickListener is an interface type.
D) Class ButtonTester implements an interface type.
Question
What's the difference between a text field and a text area?

A) A text field is used for input only and a text area is used for output only.
B) A text field can be edited by the user but a text area cannot.
C) A text area can have scroll bars but a text field cannot.
D) A text field is for a single line of text and a text area is for multiple lines of text.
Question
Which statement will add a dollar sign and the value of the double variable balance to a JTextArea component called results?

A) results.addText("$" + balance);
B) results.append("$" + balance);
C) results.addText("$" + Double.parseDouble(balance));
D) results.append("$ + (String) balance");
Question
Event listeners are often installed as ____ classes so that they can have access to the surrounding fields, methods, and final variables.

A) Inner
B) Interface
C) Abstract
D) Helper
Question
Consider the following code snippet that is supposed to show the total order amount when the button is clicked:
Public static void main(String[] args)
{
Final Order myOrder = new Order();
JButton button = new JButton("Calculate");
Final JLabel label = new JLabel("Total amount due");
) . .
Class MyListener implements ActionListener
{
Public void actionPerformed(ActionEvent event)
{
Label.setText("Total amount due " + myOrder.getAmountDue());
}
}
ActionListener listener = new MyListener();
}
What is wrong with this code?

A) button should be declared as final
B) The listener has not been attached to the button.
C) The listener cannot access the methods of the myOrder object.
D) The listener cannot access the methods of the label object.
Question
To build a user interface that contains multiple graphical components, the components ____.

A) Must be added directly to a frame component.
B) Must each be added to a separate panel.
C) Must be added to a panel that is contained within a frame.
D) Must be added to a frame that is contained within a panel.
Question
If a text field holds an integer, what expression do you use to read its contents?

A) (int) textField.getText()
B) (Integer) textField.getText()
C) Integer.parseInt(textField.getText())
D) Int.parseInteger(textField.getText())
Question
A(n) ____ has an instance method addActionListener() for specifying which object is responsible for implementing the action associated with the object.

A) JFrame
B) JSlider
C) JButton
D) JLabel
Question
Which of the following statements creates a button with "Calculate" as its label?

A) Button JButton = new Button("Calculate")
B) button = new Button(JButton, "Calculate")
C) Button = new JButton("Calculate")
D) JButton button = new JButton("Calculate")
Question
Consider the following code snippet:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
Which statement would add the panel to the frame?

A) frame.add(panel);
B) frame.add(JPanel panel);
C) frame.addComponent(panel);
D) frame.addComponent(JPanel panel);
Question
Consider the following code snippet:
Public static void main(String[] args)
{
Final Order myOrder = new Order();
JButton button = new JButton("Calculate");
Final JLabel label = new JLabel("Total amount due");
) . .
Class MyListener implements ActionListener
{
Public void actionPerformed(ActionEvent event)
{
) . .
}
}
}
Which of the local variables can be accessed within the actionPerformed method?

A) Only button can be accessed..
B) All of the local variables can be accessed.
C) label and myOrder can be accessed.
D) Only myOrder can be accessed.
Question
Use the ____ method to specify the height and width of a graphical component when you add it to a panel.

A) setPreferredDimensions.
B) setInitialDimensions.
C) setPreferredSize.
D) setInitialSize.
Question
Which of the following statements about an inner class is true?

A) An inner class may not be declared within a method of the enclosing scope.
B) An inner class may only be declared within a method of the enclosing scope.
C) An inner class can access variables from the enclosing scope only if they are passed as constructor or method parameters.
D) The methods of an inner class can access variables declared in the enclosing scope.
Question
Consider the following code snippet:
Public static void main(String[] args)
{
Order myOrder = new Order();
JButton button = new JButton("Calculate");
JLabel label = new JLabel("Total amount due");
) . .
Class MyListener implements ActionListener
{
Public void actionPerformed(ActionEvent event)
{
Label.setText("Total amount due " + myOrder.getAmountDue());
}
}
}
What is wrong with this code?

A) label must be declared as final.
B) myOrder must be declared as final.
C) label and myOrder must be declared as final.
D) label and button must be declared as final.
Question
Insert the missing statement(s) in the following code fragment. The code is intended to display a message "Your cost is: " in a text area and display the cost on the next line.
Double cost = 500.99;
String message = "Your cost is:";
JTextArea result = new JTextArea(10, 30);
__________________________________________
Result.setEditable(false);

A) result.setText(message);
Result.append(cost);
B) result.addText(message + "\n" + cost);
C) result.append(message + cost);
D) result.setText(message + "\n");
Result.append(cost);
Question
Which statements will create an editable text field and assign "Your choice" to it?

A) JTextField txt = new TextField("Your choice");
Txt)isEditable(true);
B) JTextField txt = new JTextArea("Your choice ");
Txt)isEditable(true);
C) JTextField txt.setText("Your choice ");
Txt)setEditable(true);
D) JTextField txt = new JTextField("Your choice ");
Txt)setEditable(true);
Question
What happens if a text field's width is set to 10 characters and the user types more characters into it?

A) The user interface automatically widens the field's width.
B) An InputMismatchException is thrown.
C) The earlier characters are deleted and only the last ten are shown and entered.
D) The characters are entered but only those that fit into the width are shown.
Question
An inner class can access local variables from the enclosing method only if they are declared as ____.

A) private
B) public
C) static
D) final
Question
Consider the following code snippet:
Import ____________________
Import java.awt.event.ActionListener;
/**
An action listener that prints.
*/
Public class ClickListener implements ActionListener
{
Public void actionPerformed(ActionEvent event)
{
System.out.println("I was clicked.");
}
}
Which of the following statements will complete this code?

A) java.swing.event.ActionEvent;.
B) javax.swing.event.ActionEvent;
C) javax.awt.event.ActionEvent;
D) java.awt.event.ActionEvent;
Question
An event listener for a button must implement the ____ interface.

A) EventListener
B) ActionListener
C) ActionEvent
D) ActionPerformed
Question
Which of the following statements about event listeners is true?

A) A program must have a separate event listener for each type of event to which it wishes to respond.
B) A single event listener can be used to respond to multiple types of events to which the program wishes to respond.
C) To install a listener, you do not need to know the event source.
D) A program can have only one listener which covers all events.
Question
When a component is added to a container, what does the exact location and size of the component depend on?

A) The layout manager of the container
B) The type of the component
C) The location of the of the container
D) It is random unless explicitly specified
Question
User-interface components are arranged by placing them inside ____________________.

A) Swing components
B) borders
C) JPanels
D) containers
Question
Consider the following code snippet:
JPanel panel = new JPanel();
JFrame frame = new JFrame();
JLabel label = new JLabel("Show the answer");
JTextField field = new JTextField(5);
Frame.add(label);
Frame.add(field);
Panel.add(frame);
Which of the following statements is true?

A) This code will correctly show all components.
B) The label and field should be added to the panel first, and then the panel should be added to the frame.
C) The frame should be added to the panel before adding the label and field to the frame.
D) The panel should be added to the frame first, and then the label and field should be added to the panel.
Question
You have a class that extends the JComponent class. In this class you have created a painted graphical object. Your code will change the data in the graphical object. What additional code is needed to ensure that the graphical object will be updated with the changed data?

A) You must call the component object's paintComponent() method.
B) You must call the component object's repaint() method.
C) You do not have to do anything - the component object will be automatically repainted when its data changes.
D) You must use a Timer object to cause the component object to be updated.
Question
When drawing complex shapes, make a separate class for each part and provide a(n) ___ method that draws the shape.

A) draw
B) add
C) sketch
D) outline
Question
Which java package must be imported if a program wishes to respond to button events?

A) javax.swing.event.ActionListener
B) java.awt.event.ActionListener
C) javax.swing.JButton
D) java.awt.JButton
Question
Complete this code fragment to ensure that the application exits properly when the user closes the frame:
JFrame frame = new JFrame();

A) frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
B) JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
C) JFrame.setCloseOperation(JFrame.EXIT);
D) frame.setCloseOperation(JFrame.EXIT);
Question
The ____ method should be called whenever you modify the shapes that the paintComponent method draws.

A) draw
B) paint
C) paintComponent
D) repaint
Question
To draw an ellipse, you must include which of the following import statements?

A) import java.awt.Graphics;
B) import javax.awt;
C) import javax.swing.JPanel;
D) import java.awt.Ellipse;
Question
The Graphics class is part of the ____________________ package.

A) java.swing
B) java.awt
C) javax.swing
D) javax.awt
Question
Which of the following statements is correct?

A) Methods of an inner class can access instance variables from the surrounding scope.
B) If an interface variable refers to an object, then the object need not belong to a class.
C) It is illegal to have variables whose type is an interface.
D) Methods of an inner class cannot access final variables from the surrounding scope.
Question
Which java package must be imported if a program wishes to respond to button events?

A) javax.swing.event.ActionListener
B) java.awt.event.ActionListener
C) javax.swing.JButton
D) java.awt.JButton
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/54
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 10: Graphical User Interfaces
1
Which container is used to group multiple user-interface components together?

A) text area
B) table
C) panel
D) rectangle
C
2
Which of the following is an event source?

A) A JButton object.
B) An event listener.
C) An inner class.
D) An event adapter.
A
3
What is the nickname for the graphical user interface library in Java?

A) Applet
B) GUI
C) JComponent
D) Swing
D
4
Which statement should be added to this code fragment to ensure that the frame is shown?
JFrame frame = new JFrame();

A) frame.setVisible(true);
B) frame.visible = true;
C) JFrame.setVisible();
D) frame.setVisible();
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
5
A/an ____ object contains methods that describe the actions to be taken when a user clicks a user-interface graphical object.

A) Event listener
B) Event source
C) Action listener
D) Action method
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
6
Place drawing instructions inside the __________ method, which is called whenever the component needs to be repainted.

A) paintComponent
B) draw
C) paint
D) drawComponent
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
7
To associate an event listener with a JButton component, you must use the ___ method of the JButton class.

A) addEventListener
B) addActionListener
C) addButtonListener
D) addListener
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
8
When an event occurs, the event source notifies all associated ____.

A) components
B) panels
C) interfaces
D) event listeners
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
9
Which of the following statements should be added to this code fragment to set the frame size to a width of 400 and a height of 200?
Final int FRAME_WIDTH = 400;
Final int FRAME_HEIGHT = 200;
JFrame frame = new JFrame();

A) frame.size = (FRAME_WIDTH, FRAME_HEIGHT);
B) frame.addSize(FRAME_WIDTH, FRAME_HEIGHT);
C) frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
D) frame.setSize(FRAME_HEIGHT, FRAME_WIDTH);
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
10
Based on the following statement, which of the following statements sets the title of the frame:
JFrame frame = new JFrame();

A) frame.title = "An Empty Frame";
B) frame.setTitle(JFrame.EMPTY);
C) frame.addTitle("An Empty Frame");
D) frame.setTitle("An Empty Frame");
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
11
To respond to a button event, a listener must supply instructions for the ____ method of the ActionListener interface.

A) actionEvent
B) actionPerformed
C) eventAction
D) eventResponse
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
12
Complete the following statement to construct a circle.
Graphics g;
G)____________________(x, y, width, height);

A) drawCircle
B) drawOval
C) fillCircle
D) DrawOval
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
13
When added to the code below, which statement will set the color of the next item drawn to green?
Public class ItalianFlagComponent extends JComponent
{
Public void paintComponent(Graphics

A) g.setColor(GREEN);
B) g.setColor(0, 255, 0);
C) g.setColor(Color.GREEN);
D) g.setColor("GREEN");
G) {
G)drawRect(100, 100, 30, 60);
) . .
____________________________
) . .
}
}
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
14
Which of the following statements about events and graphical user interface programs is true?

A) Your program must indicate the specific types of events to which it wishes to respond.
B) Your program will automatically receive notifications about all events that have occurred.
C) Your program must respond to notifications of all types of events that are sent to it while it is running.
D) Your program must override the default methods to handle events.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
15
The methods of a/an ____ describe the actions to be taken when an event occurs.

A) event source
B) event listener
C) event interface
D) action source
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
16
Consider the following code snippet:
JPanel panel = new JPanel();
JFrame frame = new JFrame();
JButton button = new JButton("Click me");
JLabel label = new JLabel("Show the answer");
Frame.add(label);
Frame.add(button);
Panel.add(frame);
Which of the following statements is true?

A) This code will correctly build the user interface.
B) The button and label should be added to the panel first, and then the panel should be added to the frame.
C) The frame should be added to the panel before adding the button and label to the frame.
D) The panel should be added to the frame first, and then the button and label should be added to the panel.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
17
When drawing complex shapes, provide a(n) ____ to set the position of the shape.

A) constructor
B) viewer
C) component
D) frame
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
18
Which of the following statements about listener classes is true?

A) A listener class can be declared as an anonymous inner class.
B) A listener class cannot be declared as an inner class.
C) A listener class must be declared as an inner class.
D) A listener class must be declared as an anonymous inner class.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
19
____ are generated when the user presses a key, clicks a button, or selects a menu item.

A) Listeners
B) Interfaces.
C) Events.
D) Errors.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
20
Which of the following statements will compile without error?

A) public interface AccountListener()
{
Void actionPerformed(AccountEvent event);
}
B) public interface AccountListener()
{
Void actionPerformed(AccountEvent);
}
C) public interface AccountListener
{
Void actionPerformed(AccountEvent event);
}
D) public abstract AccountListener
{
Void actionPerformed(AccountEvent event);
}
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
21
How do you specify what the program should do when the user clicks a button?

A) Specify the actions to take in a class that implements the ButtonListener interface.
B) Specify the actions to take in a class that implements the ButtonEvent interface .
C) Specify the actions to take in a class that implements the ActionListener interface.
D) Specify the actions to take in a class that implements the EventListener interface.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
22
Consider the following code snippet:
Public class ClickListener implements ActionListener
{
Public void actionPerformed(ActionEvent event)
{
System.out.println("I was clicked.");
}
}
Public class ButtonTester
{
Public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton("Click me!");
Frame.add(button);
ActionListener listener = new ClickListener();
Button.addActionListener(listener);
)..
}
}
Which of the following statements is correct?

A) Class ButtonTester is an interface type.
B) A ClickListener object listens for the action events that buttons generate.
C) Class ClickListener is an interface type.
D) Class ButtonTester implements an interface type.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
23
What's the difference between a text field and a text area?

A) A text field is used for input only and a text area is used for output only.
B) A text field can be edited by the user but a text area cannot.
C) A text area can have scroll bars but a text field cannot.
D) A text field is for a single line of text and a text area is for multiple lines of text.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
24
Which statement will add a dollar sign and the value of the double variable balance to a JTextArea component called results?

A) results.addText("$" + balance);
B) results.append("$" + balance);
C) results.addText("$" + Double.parseDouble(balance));
D) results.append("$ + (String) balance");
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
25
Event listeners are often installed as ____ classes so that they can have access to the surrounding fields, methods, and final variables.

A) Inner
B) Interface
C) Abstract
D) Helper
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
26
Consider the following code snippet that is supposed to show the total order amount when the button is clicked:
Public static void main(String[] args)
{
Final Order myOrder = new Order();
JButton button = new JButton("Calculate");
Final JLabel label = new JLabel("Total amount due");
) . .
Class MyListener implements ActionListener
{
Public void actionPerformed(ActionEvent event)
{
Label.setText("Total amount due " + myOrder.getAmountDue());
}
}
ActionListener listener = new MyListener();
}
What is wrong with this code?

A) button should be declared as final
B) The listener has not been attached to the button.
C) The listener cannot access the methods of the myOrder object.
D) The listener cannot access the methods of the label object.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
27
To build a user interface that contains multiple graphical components, the components ____.

A) Must be added directly to a frame component.
B) Must each be added to a separate panel.
C) Must be added to a panel that is contained within a frame.
D) Must be added to a frame that is contained within a panel.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
28
If a text field holds an integer, what expression do you use to read its contents?

A) (int) textField.getText()
B) (Integer) textField.getText()
C) Integer.parseInt(textField.getText())
D) Int.parseInteger(textField.getText())
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
29
A(n) ____ has an instance method addActionListener() for specifying which object is responsible for implementing the action associated with the object.

A) JFrame
B) JSlider
C) JButton
D) JLabel
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
30
Which of the following statements creates a button with "Calculate" as its label?

A) Button JButton = new Button("Calculate")
B) button = new Button(JButton, "Calculate")
C) Button = new JButton("Calculate")
D) JButton button = new JButton("Calculate")
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
31
Consider the following code snippet:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
Which statement would add the panel to the frame?

A) frame.add(panel);
B) frame.add(JPanel panel);
C) frame.addComponent(panel);
D) frame.addComponent(JPanel panel);
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
32
Consider the following code snippet:
Public static void main(String[] args)
{
Final Order myOrder = new Order();
JButton button = new JButton("Calculate");
Final JLabel label = new JLabel("Total amount due");
) . .
Class MyListener implements ActionListener
{
Public void actionPerformed(ActionEvent event)
{
) . .
}
}
}
Which of the local variables can be accessed within the actionPerformed method?

A) Only button can be accessed..
B) All of the local variables can be accessed.
C) label and myOrder can be accessed.
D) Only myOrder can be accessed.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
33
Use the ____ method to specify the height and width of a graphical component when you add it to a panel.

A) setPreferredDimensions.
B) setInitialDimensions.
C) setPreferredSize.
D) setInitialSize.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
34
Which of the following statements about an inner class is true?

A) An inner class may not be declared within a method of the enclosing scope.
B) An inner class may only be declared within a method of the enclosing scope.
C) An inner class can access variables from the enclosing scope only if they are passed as constructor or method parameters.
D) The methods of an inner class can access variables declared in the enclosing scope.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
35
Consider the following code snippet:
Public static void main(String[] args)
{
Order myOrder = new Order();
JButton button = new JButton("Calculate");
JLabel label = new JLabel("Total amount due");
) . .
Class MyListener implements ActionListener
{
Public void actionPerformed(ActionEvent event)
{
Label.setText("Total amount due " + myOrder.getAmountDue());
}
}
}
What is wrong with this code?

A) label must be declared as final.
B) myOrder must be declared as final.
C) label and myOrder must be declared as final.
D) label and button must be declared as final.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
36
Insert the missing statement(s) in the following code fragment. The code is intended to display a message "Your cost is: " in a text area and display the cost on the next line.
Double cost = 500.99;
String message = "Your cost is:";
JTextArea result = new JTextArea(10, 30);
__________________________________________
Result.setEditable(false);

A) result.setText(message);
Result.append(cost);
B) result.addText(message + "\n" + cost);
C) result.append(message + cost);
D) result.setText(message + "\n");
Result.append(cost);
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
37
Which statements will create an editable text field and assign "Your choice" to it?

A) JTextField txt = new TextField("Your choice");
Txt)isEditable(true);
B) JTextField txt = new JTextArea("Your choice ");
Txt)isEditable(true);
C) JTextField txt.setText("Your choice ");
Txt)setEditable(true);
D) JTextField txt = new JTextField("Your choice ");
Txt)setEditable(true);
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
38
What happens if a text field's width is set to 10 characters and the user types more characters into it?

A) The user interface automatically widens the field's width.
B) An InputMismatchException is thrown.
C) The earlier characters are deleted and only the last ten are shown and entered.
D) The characters are entered but only those that fit into the width are shown.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
39
An inner class can access local variables from the enclosing method only if they are declared as ____.

A) private
B) public
C) static
D) final
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
40
Consider the following code snippet:
Import ____________________
Import java.awt.event.ActionListener;
/**
An action listener that prints.
*/
Public class ClickListener implements ActionListener
{
Public void actionPerformed(ActionEvent event)
{
System.out.println("I was clicked.");
}
}
Which of the following statements will complete this code?

A) java.swing.event.ActionEvent;.
B) javax.swing.event.ActionEvent;
C) javax.awt.event.ActionEvent;
D) java.awt.event.ActionEvent;
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
41
An event listener for a button must implement the ____ interface.

A) EventListener
B) ActionListener
C) ActionEvent
D) ActionPerformed
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
42
Which of the following statements about event listeners is true?

A) A program must have a separate event listener for each type of event to which it wishes to respond.
B) A single event listener can be used to respond to multiple types of events to which the program wishes to respond.
C) To install a listener, you do not need to know the event source.
D) A program can have only one listener which covers all events.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
43
When a component is added to a container, what does the exact location and size of the component depend on?

A) The layout manager of the container
B) The type of the component
C) The location of the of the container
D) It is random unless explicitly specified
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
44
User-interface components are arranged by placing them inside ____________________.

A) Swing components
B) borders
C) JPanels
D) containers
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
45
Consider the following code snippet:
JPanel panel = new JPanel();
JFrame frame = new JFrame();
JLabel label = new JLabel("Show the answer");
JTextField field = new JTextField(5);
Frame.add(label);
Frame.add(field);
Panel.add(frame);
Which of the following statements is true?

A) This code will correctly show all components.
B) The label and field should be added to the panel first, and then the panel should be added to the frame.
C) The frame should be added to the panel before adding the label and field to the frame.
D) The panel should be added to the frame first, and then the label and field should be added to the panel.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
46
You have a class that extends the JComponent class. In this class you have created a painted graphical object. Your code will change the data in the graphical object. What additional code is needed to ensure that the graphical object will be updated with the changed data?

A) You must call the component object's paintComponent() method.
B) You must call the component object's repaint() method.
C) You do not have to do anything - the component object will be automatically repainted when its data changes.
D) You must use a Timer object to cause the component object to be updated.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
47
When drawing complex shapes, make a separate class for each part and provide a(n) ___ method that draws the shape.

A) draw
B) add
C) sketch
D) outline
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
48
Which java package must be imported if a program wishes to respond to button events?

A) javax.swing.event.ActionListener
B) java.awt.event.ActionListener
C) javax.swing.JButton
D) java.awt.JButton
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
49
Complete this code fragment to ensure that the application exits properly when the user closes the frame:
JFrame frame = new JFrame();

A) frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
B) JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
C) JFrame.setCloseOperation(JFrame.EXIT);
D) frame.setCloseOperation(JFrame.EXIT);
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
50
The ____ method should be called whenever you modify the shapes that the paintComponent method draws.

A) draw
B) paint
C) paintComponent
D) repaint
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
51
To draw an ellipse, you must include which of the following import statements?

A) import java.awt.Graphics;
B) import javax.awt;
C) import javax.swing.JPanel;
D) import java.awt.Ellipse;
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
52
The Graphics class is part of the ____________________ package.

A) java.swing
B) java.awt
C) javax.swing
D) javax.awt
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
53
Which of the following statements is correct?

A) Methods of an inner class can access instance variables from the surrounding scope.
B) If an interface variable refers to an object, then the object need not belong to a class.
C) It is illegal to have variables whose type is an interface.
D) Methods of an inner class cannot access final variables from the surrounding scope.
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
54
Which java package must be imported if a program wishes to respond to button events?

A) javax.swing.event.ActionListener
B) java.awt.event.ActionListener
C) javax.swing.JButton
D) java.awt.JButton
Unlock Deck
Unlock for access to all 54 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 54 flashcards in this deck.