Introduction to Java Programming, Sixth Edition, Y. Daniel Liang
Chapter 14 Event-Driven Programming
Section 14.2 Event and Event Source
1
Pressing a button generates a(n) __________ event.
A.
ItemEvent
B.
MouseEvent
C.
MouseMotionEvent
D.
ActionEvent
E.
ContainerEvent
2
Clicking the closing button on the upper-right corner of a frame generates a(n) __________ event.
A.
ItemEvent
B.
WindowEvent
C.
MouseMotionEvent
D.
ComponentEvent
E.
ContainerEvent
3
Which of the following statements are true?
A.
If a component can generate an event, any subclass of the component can generate the same type of event.
B.
All the event classes are subclasses of EventObject.
C.
a component on which an event is generated is called the source object.
D.
Every GUI component can generate MouseEvent, KeyEvent, FocusEvent, and ComponentEvent.
Section 14.3 Listeners, Registrations, and Handling Events
4
The component that processes the listener is called ___________.
A.
the source object
B.
the listener object
C.
the adapter object
D.
the adaptee object
5
Which of the following statements are true?
A.
Each event class has a corresponding listener interface.
B.
The listener object?s class must implement the corresponding event-listener interface.
C.
A source may have multiple listeners.
D.
The listener object must be registered by the source object.
E.
A listener may listen for multiple sources.
6
Which of the following statements registers a panel object p as a listener for a button variable jbt?
A.
addActionListener(p);
B.
jbt.addActionListener(p);
C.
jbt.addActionEventListener(p);
D.
jbt.addEventListener(p);
7
The interface __________ should be implemented to listen for a button action event.
A.
MouseListener
B.
ActionListener
C.
FocusListener
D.
WindowListener
E.
ContainerListener
8
The method in the ActionEvent __________ returns the action command of the button.
A.
getActionCommand()
B.
getModifiers()
C.
paramString()
D.
getID()
9
To be a listener for ActionEvent, an object must be an instance of ____________.
A.
ActionEvent
B.
ActionListener
C.
EventObject
D.
WindowListener
E.
WindowEvent
10
The handler (e.g., actionPerformed) is a method in ________.
A.
a source object
B.
a listener object
C.
both source and listener object
D.
the Object class
E.
the EventObject class
11
Every event object has the ________ method.
A.
getSource()
B.
getActionCommand()
C.
getTimeStamp()
D.
getWhen()
E.
getKeyChar()
12
Which of the following statements are true?
A.
Inner classes can make programs simple and concise.
B.
An inner class can be declared public or private subject to the same visibility rules applied to a member of the class.
C.
An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class.
D.
An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class.
13
Suppose A is an inner class in Test. A is compiled into a file named _________.
A.
A.class
B.
Test$A.class
C.
A$Test.class
D.
Test&A.class
14
Which statement is true about a non-static inner class?
A.
It must implement an interface.
B.
It is accessible from any other class.
C.
It can only be instantiated in the enclosing class.
D.
It must be final if it is declared in a method scope.
E.
It can access private instance variables in the enclosing object.
15
Which of the following statements are true?
A.
An anonymous inner class is an inner class without a name.
B.
An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause.
C.
An anonymous inner class must implement all the abstract methods in the superclass or in the interface.
D.
An anonymous inner class always uses the no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, the constructor is Object().
E.
An anonymous inner class is compiled into a class named OuterClassName$n.class.
16
Suppose A is an anonymous inner class in Test. A is compiled into a file named _________.
A.
A.class
B.
Test$A.class
C.
A$Test.class
D.
Test$1.class
E.
Test&1.class
17
Which of the following are correct names for listener adapters.
A.
ActionAdapter
B.
MouseAdapter
C.
KeyAdapter
D.
WindowAdapter
18
Analyze the following code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame {
   public void Test() {
     JButton jbtOK = new JButton("OK");
     add(jbtOK);
     jbtOK.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         System.out.println("The OK button is clicked");
       }
     });
   }
   public static void main(String[] args) {
     JFrame frame = new Test();
     frame.setSize(300, 300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}
A.
The program has a syntax error because no listeners are registered with jbtOK.
B.
The program has a runtime error because no listeners are registered with jbtOK.
C.
The message "The OK button is clicked" is displayed when you click the OK button.
D.
The actionPerformed method is not executed when you click the OK button, because no instance of Test is registered with jbtOK.
E.
When you run the program, the button is not displayed, because the constructor is declared wrong. It should be declared public Test(), not public void Test().
19
Analyze the following code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener {
   public Test() {
     JButton jbtOK = new JButton("OK");
     getContentPane().add(jbtOK);
   }
   public void actionPerformed(ActionEvent e) {
     System.out.println("The OK button is clicked");
   }
   public static void main(String[] args) {
     JFrame frame = new Test();
     frame.setSize(300, 300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}
A.
The program has a syntax error because no listeners are registered with jbtOK.
B.
The program has a runtime error because no listeners are registered with jbtOK.
C.
The message "The OK button is clicked" is displayed when you click the OK button.
D.
The actionPerformed method is not executed when you click the OK button, because no instance of Test is registered with jbtOK.
E.
None of the above.
20
Analyze the following code.
1. import java.awt.*;
2. import java.awt.event.*;
3. import javax.swing.*;
4.
5. public class Test extends JFrame {
6. public Test() {
7. JButton jbtOK = new JButton("OK");
8. JButton jbtCancel = new JButton("Cancel");
9. getContentPane().add(jbtOK);
10. getContentPane().add(jbtCancel);
11. jbtOK.addActionListener(this);
14. jbtCancel.addActionListener(this);
13. }
14.
15. public void actionperformed(ActionEvent e) {
16. System.out.println("A button is clicked");
17. }
18.
19. public static void main(String[] args) {
20. JFrame frame = new Test();
21. frame.setSize(300, 300);
22. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23. frame.setVisible(true);
24. }
25.}
A.
The program has syntax errors on Lines 11 and 12 because Test does not implement ActionListener.
B.
The program has syntax errors on Line 15 because the signature of actionperformed is wrong.
C.
The program has syntax errors on Line 20 because new Test() is assigned to frame (a variable of JFrame).
D.
The program has runtime errors on Lines 9 and 10 because jbtOK and jbtCancel are added to the same location in the container.
E.
None of the above.
21
Analyze the following code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends A {
   public static void main(String[] args) {
     A frame = new Test();
     frame.setSize(200, 100);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
   JButton jbtOK = new JButton("OK");
   public Test() {
     getContentPane().add(jbtOK);
     jbtOK.addActionListener(this);
   }
   public void actionPerformed(ActionEvent e) {
     super.actionPerformed(e);
     if (e.getSource() == jbtOK)
       System.out.println("OK button is clicked");
   }
}
class A extends JFrame implements ActionListener {
   JButton jbtCancel = new JButton("Cancel");
   public A() {
     getContentPane().setLayout(new FlowLayout());
     getContentPane().add(jbtCancel);
     jbtCancel.addActionListener(this);
   }
   public void actionPerformed(ActionEvent e) {
     if (e.getSource() == jbtCancel)
       System.out.println("Cancel button is clicked");
   }
}
A.
The program displays Cancel button on the left of the OK button.
B.
When you click the OK button the message "OK button is clicked" is displayed.
C.
When you click the Cancel button the message "Cancel button is clicked" is displayed.
D.
If the super.actionPerformed(e) statement in the actionPerformed method in the Test class is omitted, no message is displayed if you click the Cancel button.
E.
All of the above.
Section 14.4 Mouse Events
22
To detect whether the right button of the mouse is pressed, you use the method __________ in the MouseEvent object evt.
A.
evt.isAltDown()
B.
evt.isControlDown()
C.
evt.isMetaDown()
D.
evt.isShiftDown()
23
To listen to mouse clicked events, the listener must implement the __________ interface or extend the _______ adapter.
A.
MouseListener/MouseAdapter
B.
MouseMotionListener/MouseMotionAdapter
C.
WindowListener/WindowAdapter
D.
ComponentListener/ComponentAdapter
24
To listen to mouse moved events, the listener must implement the __________ interface or extend the _______ class.
A.
MouseListener/MouseAdapter
B.
MouseMotionListener/MouseMotionAdapter
C.
WindowListener/WindowAdapter
D.
ComponentListener/ComponentAdapter
25
To get the x coordinate of the mouse pointer for the MouseEvent evt, you use __________.
A.
evt.getX()
B.
evt.getPoint().x
C.
Either A or B
D.
Neither A nor B
26
The listener's __________ method is invoked after a mouse button is pressed (but not released yet).
A.
public void mousePressed(MouseEvent e)
B.
public void mouseReleased(MouseEvent e)
C.
public void mouseEntered(MouseEvent e)
D.
public void mouseExited(MouseEvent e)
E.
public void mouseClicked(MouseEvent e)
27
The listener's __________ method is invoked after a mouse button is released.
A.
public void mousePressed(MouseEvent e)
B.
public void mouseReleased(MouseEvent e)
C.
public void mouseEntered(MouseEvent e)
D.
public void mouseExited(MouseEvent e)
E.
public void mouseClicked(MouseEvent e)
Section 14.5 Keyboard Events
28
To listen to keyboard actions, the listener must implement the __________ interface or extend the _________ class.
A.
MouseListener/MouseAdapter
B.
KeyListener/KeyAdapter
C.
WindowListener/WindowAdapter
D.
ComponentListener/ComponentAdapter
29
Which of the following statements are true?
A.
The keyPressed handler is invoked when a key is pressed.
B.
The keyReleased handler is invoked when a key is released.
C.
The keyTyped handler is invoked when a key is entered.
D.
The keyTyped handler is invoked when a Unicode character is entered.
30
To check whether a DELETE key is pressed or released, which handler should be used?
A.
keyPressed(KeyEvent e)
B.
keyReleased(KeyEvent e)
C.
keyTyped(KeyEvent e)
31
The getKeyCode() method of the KeyEvent returns __________.
A.
a character
B.
the ASCII code of the character
C.
the Unicode code of the character
D.
None of the above.
32
What is the value of evt.getKeyCode() or evt.getChar() for the keyTyped() events?
A.
A character
B.
The ASCII code of the character
C.
The Unicode code of the character
D.
VK_UNDEFINED
33
To enable a component to listen to keyboard events, you need to ____
A.
Implement the KeyListener interface for the component.
B.
Override the isFocusTraversable method defined in the Component class to return true.
C.
Invoke the component?s requestFocus method to set focus on this component.
D.
All of the above.
34
To be a listener for ActionEvent, an object must be an instance of ____________.
A.
ActionEvent
B.
ActionListener
C.
EventObject
D.
WindowListener
E.
WindowEvent
35
You can use getWhen() to find the time of an ____________.
A.
ActionEvent
B.
MouseEvent
C.
KeyEvent
D.
ComponentEvent
E.
WindowEvent
Section 14.6 (Optional) The Timer Class
36
Which of the following statements are true?
A.
You can add a listener in the Timer constructor;
B.
You can use the addActionListener method in the Timer class to add a listener.
C.
You can specify a delay in the Timer constructor;
D.
You can specify a delay using the setDelay method;
37
Which of the following statements are true?
A.
You must always specify a listener when creating a Timer object.
B.
You can add multiple listeners for a Timer object.
C.
To stop a timer, invoke timer.stop().
D.
To start a timer, invoke timer.start().
E.
When a timer is created, it is automatically started.