Java Code Exam
QUESTION 1
- A/an ____ object contains methods that describe the actions to be taken when a user clicks a user-interface graphical object.
| Event listener | ||
| Event source | ||
| Action listener | ||
| Action method |
2 points
QUESTION 2
- Which statement should be added to this code fragment to ensure that the frame is shown?
JFrame frame = new JFrame();
| frame.setVisible(true); | ||
| frame.visible = true; | ||
| JFrame.setVisible(); | ||
| frame.setVisible(); |
2 points
QUESTION 3
- Consider the following code snippet:
import ____________________
import 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?
| java.swing.event.ActionEvent;. | ||
| javax.swing.event.ActionEvent; | ||
| javax.awt.event.ActionEvent; | ||
| java.awt.event.ActionEvent; |
2 points
QUESTION 4
- When drawing complex shapes, provide a(n) ____ to set the position of the shape.
| constructor | ||
| viewer | ||
| component | ||
| frame |
2 points
QUESTION 5
- Which statement will add a dollar sign and the value of the double variable balance to a JTextArea component called results?
| results.addText(“$” + balance); | ||
| results.append(“$” + balance); | ||
| results.addText(“$” + Double.parseDouble(balance)); | ||
| results.append(“$ + (String) balance”); |
2 points
QUESTION 6
- Which java package must be imported if a program wishes to respond to button events?
| javax.swing.event.ActionListener | ||
| java.awt.event.ActionListener | ||
| javax.swing.JButton | ||
| java.awt.JButton |
2 points
QUESTION 7
- When drawing complex shapes, make a separate class for each part and provide a(n) ___ method that draws the shape.
| draw | ||
| add | ||
| sketch | ||
| outline |
2 points
QUESTION 8
- Which of the following statements will compile without error?
| public interface AccountListener() { void actionPerformed(AccountEvent event); } |
||
| public interface AccountListener() { void actionPerformed(AccountEvent); } |
||
| public interface AccountListener { void actionPerformed(AccountEvent event); } |
||
| public abstract AccountListener { void actionPerformed(AccountEvent event); } |
2 points
QUESTION 9
- To respond to a button event, a listener must supply instructions for the ____ method of the ActionListener interface.
| actionEvent | ||
| actionPerformed | ||
| eventAction | ||
| eventResponse |
2 points
QUESTION 10
- 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?
| Only button can be accessed.. | ||
| All of the local variables can be accessed. | ||
| label and myOrder can be accessed. | ||
| Only myOrder can be accessed. |
2 points
QUESTION 11
- Use the ____ method to specify the height and width of a graphical component when you add it to a panel.
| setPreferredDimensions. | ||
| setInitialDimensions. | ||
| setPreferredSize. | ||
| setInitialSize. |
2 points
QUESTION 12
- Which of the following statements about events and graphical user interface programs is true?
| Your program must indicate the specific types of events to which it wishes to respond. | ||
| Your program will automatically receive notifications about all events that have occurred. | ||
| Your program must respond to notifications of all types of events that are sent to it while it is running. | ||
| Your program must override the default methods to handle events.
11)____ are generated when the user presses a key, clicks a button, or selects a menu item. |
2 points
QUESTION 13
- Which of the following statements creates a button with “Calculate” as its label?
| Button JButton = new Button(“Calculate”) | ||
| button = new Button(JButton, “Calculate”) | ||
| Button = new JButton(“Calculate”) | ||
| JButton button = new JButton(“Calculate”) |
2 points
QUESTION 14
- The methods of a/an ____ describe the actions to be taken when an event occurs.
| event source | ||
| event listener | ||
| event interface | ||
| action source |
2 points
QUESTION 15
- 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);__________________________________________
setEditable(false);
| result.setText(message); result.append(cost); |
||
| result.addText(message + “\n” + cost); | ||
| result.append(message + cost); | ||
| result.setText(message + “\n”); result.append(cost); |
2 points
QUESTION 16
- Which of the following statements is correct?
| Methods of an inner class can access instance variables from the surrounding scope. | ||
| If an interface variable refers to an object, then the object need not belong to a class. | ||
| It is illegal to have variables whose type is an interface. | ||
| Methods of an inner class cannot access final variables from the surrounding scope. |
2 points
QUESTION 17
- 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();
| frame.size = (FRAME_WIDTH, FRAME_HEIGHT); | ||
| frame.addSize(FRAME_WIDTH, FRAME_HEIGHT); | ||
| frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); | ||
| frame.setSize(FRAME_HEIGHT, FRAME_WIDTH); |
2 points
QUESTION 18
- 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);
add(label);
frame.add(field);
panel.add(frame);Which of the following statements is true?
| This code will correctly show all components. | ||
| The label and field should be added to the panel first, and then the panel should be added to the frame. | ||
| The frame should be added to the panel before adding the label and field to the frame. | ||
| The panel should be added to the frame first, and then the label and field should be added to the panel. |
2 points
QUESTION 19
- What happens if a text field’s width is set to 10 characters and the user types more characters into it?
| The user interface automatically widens the field’s width. | ||
| An InputMismatchException is thrown. | ||
| The earlier characters are deleted and only the last ten are shown and entered. | ||
| The characters are entered but only those that fit into the width are shown. |
2 points
QUESTION 20
- 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”);
add(label);
frame.add(button);
panel.add(frame);Which of the following statements is true?
| This code will correctly build the user interface. | ||
| The button and label should be added to the panel first, and then the panel should be added to the frame. | ||
| The frame should be added to the panel before adding the button and label to the frame. | ||
| The panel should be added to the frame first, and then the button and label should be added to the panel. |
