Unit 6 (Event Handlers) Flashcards

1
Q

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

A

B) To handle events and perform actions in response

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

B) Two classes interacting where one class notifies the other about certain events

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which class in Java is typically used to handle simple button click events?

A) JButton
B) ActionListener
C) ActionEvent
D) JFrame

A

B) ActionListener

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A) Implementing the ActionListener interface and overriding the actionPerformed method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A) getSource()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A) The frame will close.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A) Calling the addActionListener method on the button

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

B) To handle events like button clicks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A

B) The event is discarded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A) Using the removeActionListener method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

B) It executes in response to an action event.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

D) Direct method calls without interface implementation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A) It prints “Button clicked!” to the console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which interface must be implemented to handle item state changes in checkboxes?

A) ItemListener
B) ActionListener
C) ChangeListener
D) ComponentListener

A

A) ItemListener

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In event-driven programming, which Java feature is used to handle events like mouse clicks or key presses?

A) Polymorphism
B) Encapsulation
C) Callbacks
D) Inheritance

A

C) Callbacks

17
Q

What is the output of this code if the button is clicked multiple times?

public class CounterFrame extends JFrame {
private int count = 0;
private JButton countButton = new JButton(“Count”);
private JLabel label = new JLabel(“Count: 0”);

public CounterFrame() {
    countButton.addActionListener(e -> {
        count++;
        label.setText("Count: " + count);
    });
    add(countButton, BorderLayout.NORTH);
    add(label, BorderLayout.SOUTH);
    setSize(200, 100);
    setVisible(true);
} } A) The label updates to show the increasing count each time the button is clicked. B) The label displays "Count: 1" no matter how many times the button is clicked. C) The count increases but the label does not update. D) A runtime error occurs because the label text is not properly formatted.
A

A) The label updates to show the increasing count each time the button is clicked.

18
Q

Which interface must be implemented to handle changes in item selection, such as in a checkbox?
A) ActionListener
B) ItemListener
C) WindowListener
D) KeyListener

A

B) ItemListener

19
Q

How would you handle multiple different types of events in a single GUI component?
A) Implement multiple listeners
B) Use a single universal listener interface
C) Implement ActionListener for all event types
D) Java does not support handling multiple event types in a single component

A

A) Implement multiple listeners

20
Q

Why would you unsubscribe an event listener from a component in a Java GUI application?
A) To free up resources when they are no longer needed
B) To increase the performance of the application
C) It’s mandatory to unsubscribe all listeners before closing the application
D) Unsubscribing is not supported in Java

A

A) To free up resources when they are no longer needed

21
Q

What is a common use of anonymous classes in Java GUI event handling?
A) To create temporary components
B) To implement listener interfaces for event handling directly within the method where components are created
C) To declare new Java standard classes
D) To replace the functionality of lambda expressions

A

B) To implement listener interfaces for event handling directly within the method where components are created