Unit 6 (Event Handlers) Flashcards
What is the role of an event listener in Java?
A) To generate events
B) To handle events and perform actions in response
C) To display events to the user
D) To store event data
B) To handle events and perform actions in response
What does the callback mechanism involve in GUI programming?
A) A component calling methods from the operating system
B) Two classes interacting where one class notifies the other about certain events
C) A loop where events are constantly checked
D) Automatic event generation without user interaction
B) Two classes interacting where one class notifies the other about certain events
Which class in Java is typically used to handle simple button click events?
A) JButton
B) ActionListener
C) ActionEvent
D) JFrame
B) ActionListener
What is required to handle an event from a JButton in Java?
A) Implementing the ActionListener interface and overriding the actionPerformed method
B) Extending the JFrame class and overriding the actionPerformed method
C) Implementing the ActionEvent class and creating a custom method
D) Directly modifying the JButton class
A) Implementing the ActionListener interface and overriding the actionPerformed method
Which method is used to determine the source of an event when multiple components use the same listener?
A) getSource()
B) getComponent()
C) getListener()
D) getEventSource()
A) getSource()
What will be the output of the following code when the ‘Exit’ button is clicked?
public class MyFrameWithExit extends JFrame {
private JButton exitButton = new JButton(“Exit”);
public MyFrameWithExit() {
super(“Exit Frame”);
exitButton.addActionListener(e -> System.exit(0));
add(exitButton);
setSize(300, 200);
setVisible(true);
}
}
A) The frame will close.
B) Nothing happens.
C) An error will occur.
D) The button will disable itself.
A) The frame will close.
In the context of Java GUI, what does subscribing a listener to a button involve?
A) Calling the addActionListener method on the button
B) Modifying the button’s properties
C) Overriding the button’s default methods
D) Implementing all methods in the ActionListener interface in the button class
A) Calling the addActionListener method on the button
What is the purpose of implementing interfaces like ActionListener in Java GUI components?
A) To inherit properties from superclass components
B) To handle events like button clicks
C) To improve the look and feel of the GUI
D) To increase the processing speed of events
B) To handle events like button clicks
What happens if a registered listener does not handle an event?
A) The event is stored for later use.
B) The event is discarded.
C) The event is sent to another listener.
D) The GUI throws an exception.
B) The event is discarded.
How do you unsubscribe a listener from a JButton in Java?
A) Using the removeActionListener method
B) By setting the listener to null
C) By deleting the JButton object
D) It’s not possible to unsubscribe a listener.
A) Using the removeActionListener method
What is the role of the actionPerformed method in event handling?
A) It initializes the components that generate actions.
B) It executes in response to an action event.
C) It generates action events.
D) It registers components to receive action events.
B) It executes in response to an action event.
Consider the following code snippet. What is the likely issue?
JButton button = new JButton(“Submit”);
button.addActionListener(e -> doSomething());
A) The button does not have a valid ActionListener.
B) The lambda expression is incorrectly implemented.
C) The doSomething() method must accept an ActionEvent parameter.
D) There is no issue with the code.
C) The doSomething() method must accept an ActionEvent parameter. (Note: This depends on the implementation of doSomething(). If it doesn’t need event data and is accessible in the context, D could also be correct.)
Which of these is not a valid way to handle events in Java?
A) Using inner classes
B) Using anonymous classes
C) Using lambda expressions
D) Direct method calls without interface implementation
D) Direct method calls without interface implementation
What would be the effect of clicking a JButton configured with the following ActionListener?
button.addActionListener(e -> System.out.println(“Button clicked!”));
A) It prints “Button clicked!” to the console.
B) It displays “Button clicked!” in a new window.
C) It causes the application to terminate.
D) It clears the text on the button.
A) It prints “Button clicked!” to the console.
Which interface must be implemented to handle item state changes in checkboxes?
A) ItemListener
B) ActionListener
C) ChangeListener
D) ComponentListener
A) ItemListener