0% found this document useful (0 votes)
11 views14 pages

Event Handling

This document provides a comprehensive overview of event handling in Java, covering key concepts such as event types, listener interfaces, and methods associated with handling various events. It includes questions and answers related to event sources, listeners, and the event delegation model, highlighting the importance of interfaces like ActionListener, MouseListener, and KeyListener. Additionally, it discusses adapter classes and the role of the event queue in managing events.

Uploaded by

lochansawant0107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

Event Handling

This document provides a comprehensive overview of event handling in Java, covering key concepts such as event types, listener interfaces, and methods associated with handling various events. It includes questions and answers related to event sources, listeners, and the event delegation model, highlighting the importance of interfaces like ActionListener, MouseListener, and KeyListener. Additionally, it discusses adapter classes and the role of the event queue in managing events.

Uploaded by

lochansawant0107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Event Handling

1. What is an event in Java?


A) A signal that something has happened
B) A method
C) A class
D) A thread
Answer: A

2. Which package contains the event handling classes in Java?


A) java.awt
B) java.swing
C) java.event
D) java.util
Answer: A

3. Which interface is used for handling action events?


A) ActionListener
B) KeyListener
C) MouseListener
D) EventListener
Answer: A

4. Which method is defined in the ActionListener interface?


A) actionPerformed()
B) eventOccurred()
C) handleEvent()
D) actionTriggered()
Answer: A
5. What type of event does the MouseListener interface handle?
A) Key events
B) Mouse events
C) Window events
D) Focus events
Answer: B

6. Which method in MouseListener is invoked when a mouse button is pressed?


A) mouseClicked()
B) mousePressed()
C) mouseReleased()
D) mouseEntered()
Answer: B

7. What is the purpose of the ComponentListener interface?


A) To listen to action events
B) To listen to component resizing and moving
C) To listen to key presses
D) To listen to mouse movements
Answer: B

8. Which of the following is NOT a part of event delegation model in Java?


A) Event source
B) Event listener
C) Event object
D) Event handler
Answer: D

9. What does the term "event source" refer to?


A) The object that generates events
B) The object that listens to events
C) The event object itself
D) The event queue
Answer: A

10. Which method in the KeyListener interface is invoked when a key is typed?
A) keyPressed()
B) keyReleased()
C) keyTyped()
D) keyEntered()
Answer: C

11. Which event is generated when the user clicks on a button?


A) ActionEvent
B) MouseEvent
C) KeyEvent
D) FocusEvent
Answer: A

12. How do you register an event listener to a button?


A) button.addListener()
B) button.addActionListener()
C) button.registerListener()
D) button.listen()
Answer: B

13. What does the term "event listener" refer to?


A) The object that generates events
B) The object that listens for events
C) The event object itself
D) The event queue
Answer: B

14. Which interface must be implemented to listen for mouse events?


A) MouseListener
B) MouseAdapter
C) MouseMotionListener
D) All of the above
Answer: D

15. Which of the following classes is an adapter class for mouse events?
A) MouseListener
B) MouseAdapter
C) ActionListener
D) EventListener
Answer: B

16. What is the purpose of the FocusListener interface?


A) To handle mouse focus events
B) To handle component visibility events
C) To handle keyboard focus events
D) To handle window events
Answer: C

17. Which method in FocusListener is called when a component gains focus?


A) focusGained()
B) focusLost()
C) focusChanged()
D) focusTriggered()
Answer: A
18. What is a key feature of the event delegation model in Java?
A) Single-threaded
B) Synchronous
C) Asynchronous
D) Non-blocking
Answer: C

19. What type of event is a WindowEvent?


A) Action event
B) Mouse event
C) Component event
D) Window state change event
Answer: D

20. Which interface should you implement to handle window events?


A) WindowListener
B) WindowAdapter
C) ActionListener
D) EventListener
Answer: A

21. What does the method windowClosing() do in WindowListener?


A) Closes the window
B) Opens the window
C) Saves the window state
D) Cancels the window closing
Answer: D

22. Which event is generated when a user resizes a component?


A) ComponentEvent
B) WindowEvent
C) MouseEvent
D) ActionEvent
Answer: A

23. Which method is called when a mouse button is released?


A) mousePressed()
B) mouseReleased()
C) mouseClicked()
D) mouseDragged()
Answer: B

24. What is the difference between mouseClicked() and mousePressed()?


A) mousePressed() is called after mouseClicked()
B) mouseClicked() is called after mousePressed()
C) They are the same
D) mousePressed() is never called
Answer: B

25. Which method would you override to handle a key being pressed?
A) keyPressed()
B) keyTyped()
C) keyReleased()
D) handleKey()
Answer: A

26. What does the method keyReleased() do?


A) Detects when a key is pressed
B) Detects when a key is released
C) Detects when a key is typed
D) None of the above
Answer: B

27. Which method in a MouseMotionListener is invoked when the mouse is moved?


A) mouseMoved()
B) mouseDragged()
C) mouseClicked()
D) mouseReleased()
Answer: A

28. What is the purpose of the ActionEvent class?


A) To define mouse events
B) To define action events
C) To define key events
D) To define window events
Answer: B

29. Which listener would you use to handle drag-and-drop operations?


A) DragSourceListener
B) DropTargetListener
C) Both A and B
D) MouseListener
Answer: C

30. What type of event does a TextListener handle?


A) Focus event
B) Action event
C) Text change event
D) Key event
Answer: C

31. Which method is called when the text is changed in a TextField?


A) textChanged()
B) textValueChanged()
C) textValueChanged()
D) keyTyped()
Answer: C

32. How many methods are there in the KeyListener interface?


A) 1
B) 2
C) 3
D) 4
Answer: C

33. What is the purpose of the MouseMotionListener interface?


A) To handle mouse clicks
B) To handle mouse movements and drags
C) To handle keyboard input
D) To handle window events
Answer: B

34. What does the term "event queue" refer to?


A) The storage for event listeners
B) The queue for pending events
C) The list of registered components
D) The thread for handling events
Answer: B

35. What happens if an event listener is not registered to an event source?


A) The program crashes
B) The event is ignored
C) The event is queued
D) The event is handled by default
Answer: B

36. Which of the following events can be generated by a JTextField?


A) ActionEvent
B) FocusEvent
C) KeyEvent
D) All of the above
Answer: D

37. Which class provides default implementations of event listener methods?


A) EventListener
B) ComponentAdapter
C) MouseAdapter
D) KeyAdapter
Answer: B

38. Which package is primarily used for event handling in Java?


A) java.util
B) java.awt.event
C) javax.swing
D) java.io
Answer: B
39. Which class is used to handle button click events in Java?
A) MouseListener
B) ActionListener
C) KeyListener
D) ComponentListener
Answer: B

40. What method must be implemented when using ActionListener?


A) actionPerformed()
B) eventPerformed()
C) buttonClicked()
D) onClick()
Answer: A

41. Which listener interface is used to receive keyboard events?


A) MouseListener
B) KeyListener
C) ActionListener
D) FocusListener
Answer: B

42. Which method in the MouseListener interface is called when a mouse button is clicked?
A) mouseClicked()
B) mousePressed()
C) mouseReleased()
D) mouseEntered()
Answer: A

43. How do you register an event listener in Java?


A) addListener()
B) addEvent()
C) addActionListener()
D) registerListener()
Answer: C

44. Which method is called when the mouse enters a component?


A) mouseExited()
B) mouseEntered()
C) mouseClicked()
D) mousePressed()
Answer: B) mouseEntered()

8. What is the purpose of the FocusListener interface?

A) To handle mouse events


B) To handle keyboard events
C) To handle focus events in GUI components
D) To handle action events
Answer: C) To handle focus events in GUI components

9. Which method is called when the focus is gained on a component?

A) focusLost()
B) focusGained()
C) focusChanged()
D) focusAcquired()
Answer: B) focusGained()

10. What type of event does the WindowListener interface handle?


A) Component events
B) Window events
C) Mouse events
D) Keyboard events
Answer: B) Window events

11. What is the purpose of the Adapter classes in Java event handling?

A) To create GUI components


B) To provide default implementations of listener methods
C) To manage threads
D) To extend Java's reflection capabilities
Answer: B) To provide default implementations of listener methods

12. Which of the following is NOT a method in the WindowListener interface?

A) windowOpened()
B) windowClosing()
C) windowActivated()
D) windowResized()
Answer: D) windowResized()

13. Which interface would you use to handle item selection in a JComboBox?

A) ActionListener
B) ItemListener
C) MouseListener
D) KeyListener
Answer: B) ItemListener
14. What event does the MouseMotionListener interface handle?

A) Mouse button events


B) Mouse movement events
C) Focus events
D) Action events
Answer: B) Mouse movement events

15. Which method is invoked when the mouse is dragged?

A) mouseDragged()
B) mouseMoved()
C) mousePressed()
D) mouseReleased()
Answer: A) mouseDragged()

16. Which component's events cannot be handled by the MouseListener interface?

A) JButton
B) JTextField
C) JPanel
D) JLabel
Answer: D) JLabel

17. In Java Swing, which method is used to create a new thread to handle long-running tasks
to avoid freezing the UI?

A) SwingWorker
B) EventQueue
C) Thread
D) Runnable
Answer: A) SwingWorker

18. How can you prevent an event from being processed further?

A) stopEvent()
B) consume()
C) ignoreEvent()
D) cancelEvent()
Answer: B) consume()

19. Which interface is used for handling mouse wheel events?

A) MouseWheelListener
B) MouseListener
C) WheelListener
D) ScrollListener
Answer: A) MouseWheelListener

20. What is the primary use of the ActionEvent class?

A) To represent mouse events


B) To represent keyboard events
C) To represent action events like button clicks
D) To represent window events
Answer: C) To represent action events like button clicks

You might also like