Chapter 8

Additional Web Questions

1) All Java events are encapsulated within objects.

a)_____ true

b)_____ false

Answer: A. All Java events are encapsulated within a subclass of the java.util.EventObject class.

2) As far as event-handling is concerned, it is only important to know that an event took place, but not which component was responsible for firing a given event. For this reason, it is not possible to query an event object for the component that fired the event.

a)_____ true

b)_____ false

Answer: B. Absolutely untrue—the base class for all events contains the method getSource() which returns a reference to the object that was responsible for firing the event. Knowing which object fired an event can be of paramount importance for event handlers that receive events fired from multiple components.

3) The events that are fired by a component are clearly documented within the Java API Documentation for each component.

a)_____ true

b)_____ false

Answer: B. At the time this book was published, the most current revision of the Java API Documentation does not clearly state the events fired by each component. Within the chapter, however, we have provided enough information to help you become an event super-sleuth.

4) If your program depends upon the order in which a component fires events to listeners, you are in for a lot of trouble.

a)_____ true

b)_____ false

Answer: A. You bet! The order in which listeners receive an event is undefined, which means you can not count on any particular order. Moreover, you should probably reconsider your tactics and provide for one listener that then notifies others as appropriate.

5) Event handling can be broken-down into two distinct steps: (1) Determine in which event type you have interest, (2) create a suitable handler method for that event type.

a)_____ true

b)_____ false

Answer: B. An important step was neglected—you must register as an event listener with the object that is responsible for firing the event. Without registering as a listener, the events will never be fired to your handler method.

6) If you do not provide implementations of methods required by event listener interfaces, default methods will be provided which do nothing.

a)_____ true

b)_____ false

Answer: B. Sorry, Charlie—the point of an interface is to form a pact between your class and the compiler that your class will provide implementations for each required method. At the very minimum, a class which implements an interface must provide an empty method for each method specified within the interface.

7) An event must be handled within the class that contains the object that fired the event.

a)_____ true

b)_____ false

Answer: B. Events can be handled by any class that conforms to the appropriate listener interface. Often times, event handling is delegated to a class devoted specifically to that purpose.

8) All Swing components fire ActionEvent events.

a)_____ true

b)_____ false

Answer: B. Not all Swing components fire ActionEvent events.

9) A single listener interface is used for handling all mouse-related events.

a)_____ true

b)_____ false

Answer: B. Mouse events are fired via two listener interfaces—MouseListener and MouseMotionListener. The MouseListener interface provides methods relating to button presses and focus. The MouseMotionListener interface provides methods relating to mouse position.

10) It is possible to create your own adapter classes to aid in the selective handling of events.

a)_____ true

b)_____ false

Answer: A. Absolutely—so long as the adapter class contains a default implementation for each method of the interfaces is supports, the adapter class will work just as well as those provided in the API.