a)Which of the following true about source and listener objects? They must alway
ID: 3667995 • Letter: A
Question
a)Which of the following true about source and listener objects?
They must always be different objects
They can be the same object
The objects must always be of different types
Both objects must be of the same type
b) Which of the following statements is incorrect?
A source object can only have one listener object
The listener object's class must implement the corresponding event-listener interface
A listener may listen for multiple sources
The listener object must be registered by the source object
c) The listener for an button event is an instance of ________.
ItemListener
ActionListener
MouseListener
KeyListener
d) Which of the following statements registers a panel object aPanel as a listener for a button variable aButton?
aButton.addActionEventListener(aPanel);
aPanel.addEventListener(aPanel);
aButton.addActionListener(aPanel);
addActionListener(aPanel);
e) What is the benefit of using adapter classes rather than listeners interfaces?
It makes the response time for responding to events much quicker
It makes it possible to respond to several events at the same time
It enables multiple inheritance of classes
It makes it unnecessary to implement unused event handling methods
f) In the program below, which object is listening for the events of okButton?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Quiz3C extends JFrame
{
private JButton okButton = new JButton("OK");
public Quiz3C()
{
add(okButton);
okButton.addActionListener(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
System.out.println("The OK button is clicked");
}
});
}
public static void main(String[] args)
{
JFrame frame = new Quiz3C();
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
JButton
frame
An anonymous object
okButton
g) Which of the following will occur if you fail to register an object as a listener for the click events of a button?
The containing frame will be considered as the default listener
A compilation error will occur
A class cast exception will be thrown at run-time
Nothing will happen when the button is clicked
h) When a mouse event is generated how is the location of the click point determined?
The event handler must generate a secondary event to obtain the location
It is automatically saved as an instance variable in the object of which the mouse was clicked
It is obtained by calling a method of the mouse object
It is extracted from the mouse event that is passed to the event handler
Explanation / Answer
a)
The fact about source and listener objects is that they can be the same objects.
b)
The statement that is incorrect is that a source object can only have one listener object.
No source can be attached to more than one action listener interface class.
c)
The listener for a button event is an instance of Action Listener.
The listener for a button event is an instance of Action Listener. Action Listener is an interface that implements the action performed. A button always listens to the Action Performed listener objects.
d)
The statement that registers a panel object aPanel as a listener for a button variable aButton is aButton.addActionListener(aPanel);
The object of a listener class receives the event and performs necessary event when the event associated with the component is generated. The button is associated with action listener, it means that when a button is clicked, an action event will be generated. Here, the listener object is aPanel.
e)
The benefit of using adapter classes rather than listener’s interfaces is that it makes it necessary to implement unused event handling methods.
f)
The correct option is “An anonymous object” because the action listener is implemented anonymously and the Action listener class can be defined using internal anonymous class and external class.
g)
If you fail to register an object as a listener for the click events of a button, nothing will happen when the button is clicked. Thus, the component will not respond to user interactions and the button does nothing if it is not registered to a listener.
h)
When a mouse event is generated, the location of the click point is determined when it is extracted from the mouse event that is passed to the event handler.
Here, the two positions x and y can be extracted from the event objects that is passed to the event handler method.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.