Event Handling Interview Questions in Java

List of topic-wise frequently asked java interview questions with the best possible answers for job interviews.

Event Handling Interview Questions in Java

Question 1.
What are the advantages of the model over the event-inheritance model?
Answer:
The event-delegation model has two advantages over the event-inheritance model. They are:

a) It enables event handling by objects other than the ones that generate the events. This allows a clean separation between a component’s design and its use.

b) It performs much better in applications where many events are generated. This performance improvement is due to the fact that the event-delegation model does not have to repeatedly process unhandled events as is the case of the event inheritance.

Question 2.
What are source and listener?
Answer:
Source: A source is an object that generates an event. This occurs when the internal state of that object changes in some way.
Listener: A listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events.
Second, it must implement methods to receive and process these notifications.

Question 3.
What is adapter class?
Answer:
An adapter class provides an empty implementation of all methods in an event listener interface. Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. You can define a new class to act listener by extending one of the adapter classes and implementing only those events in which you are interested.
For example, the MouseMotionAdapter class has two methods, mouseDragged( )and mouseMoved( ).

The signatures of these empty are exactly as defined in the MouseMotionListener interface. If you are interested in only mouse drag events, then you could simply extend MouseMotionAdapter and implement mouseDragged( ).

Question 4.
What is an event and what are the models available for event handling?
Answer:
An event is an event object that describes a state of change in a source. In other words, an event occurs when an action is generated, like pressing a button, clicking the mouse, selecting a fist, etc.
There are two types of models for handling events and they are:

a) event-inheritance model and
b) event-delegation model

Question 5.
What are the advantages of the model over the event-inheritance model?
Answer:
The event-delegation model has two advantages over the event-inheritance model. They are:

a) It enables event handling by objects other than the ones that generate the events. This allows a clean separation between a component’s design and its use.

b) It performs much better in applications where many events are generated. This performance improvement is due to the fact that the event-delegation model does not have to repeatedly process unhandled events as is the case of the event inheritance.

Question 6.
what is the difference between the JDK 1.02 event model and the event-delegation model introduced with JDK 1.1?
Answer:
The JDK 1.02 event model uses an event inheritance or bubbling approach. In this model, components are required to handle their own events. If they do not handle a particular event, the event is inherited by (or bubbled up to) the component’s container. The container then either handles the event or it is bubbled up to its container and so on until the highest-level container has been tried. In the event-delegation model, specific objects are designated as event handlers for GUI components. These objects implement event-listener interfaces. The event-delegation model is more efficient than the event-inheritance model because it eliminates the processing required to support the bubbling of unhandled events.

Question 7.
What is the highest-level event class of the event-delegation model?
Answer:
The java. util.EventObject class is the highest-level class in the event- delegation class hierarchy.

Question 8.
What event results from the clicking of a button?
Answer:
The ActionEvent event is generated as the result of the clicking of a button.

Question 9.
How can a GUI component handle its own events?
Answer:
A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener.

Question 10.
What is the difference between the JDK 1.02 event model and the event-delegation model introduced with JDK 1.1?
Answer:
The JDK 1.02 event model uses an event inheritance or bubbling approach. In this model, components are required to handle their own events. If they do not handle a particular event, the event is inherited by (or bubbled up to) the component’s container. The container then either handles the event or it is bubbled up to its container and so on until the highest-level container has been tried. In the event-delegation model, specific objects are designated as event handlers for GUI components. These objects implement event-listener interfaces. The event-delegation model is more efficient than the event-inheritance model because it eliminates the processing required to support the bubbling of unhandled events.

Question 11.
What is the purpose of the enableEvents( ) method?
Answer:
The enableEvents( ) method is used to enable an event for a particular object. Normally, an event is enabled when a listener is added to an object for a particular event. The enableEvents( ) method is used by objects that handle events by overriding their event-dispatch methods.

Question 12.
What interface is extended by AW^T event listeners?
Answer:
All AWT event listeners extend the java. util.EventListener interface.

Question 13.
How can a GUI component handle its own events?
Answer:
A component can handle its own events by implementing the required event-listener interface and adding itself as its own event listener.

Question 14.
What is the purpose of the enableEvents( ) method?
Answer:
The enableEvents( ) method is used to enable an event for a particular object. Normally, an event is enabled when a listener is added to an object for a particular event. The enableEvents( ) method is used by objects that handle events by overriding their event-dispatch methods.