Unit 2
Unit 2
2. Which of these packages contains all the classes and methods required for event handling in Java?
- A) java.awt
- B) java.util
- C) java.event
- D) java.awt.event
- Answer: D) java.awt.event
5. Which of these methods can be used to know the degree of adjustment made by the user?
- A) getAdjustment()
- B) calculateAdjustment()
- C) getValue()
- D) adjustValue()
- Answer: C) getValue()
7. A source generates an event and sends it to ___________ listeners that can handle the event.
- A) Only one
- B) At least two
- C) One or more
- D) Exactly three
- Answer: C) One or more
13. An event is generated when the internal state of the event source is ___________.
- A) Constant
- B) Initialized
- C) Changed
- D) Reset
- Answer: C) Changed
15. At the root of the Java event class hierarchy is ___________ which is the superclass for all
events.
- A) EventRoot
- B) EventBase
- C) EventSource
- D) EventObject
- Answer: D) EventObject
26. Generated when a window is activated, de-activated, closed, de-activated, de-iconified, iconified,
opened or quit.
- A) WindowEvent
- B) AppletEvent
- C) FrameEvent
- D) SystemEvent
- Answer: A) WindowEvent
33. If a class extends the ActionListener interface, it must contain a method called. __________.
- A) action()
- B) actionPerformed()
- C) execute()
- D) handleAction()
- Answer: B) actionPerformed()
38. In Java, an event is an _________ which specifies the change of state in the source.
- A) Abstract
- B) Object
- C) Entity
- D) Indicator
- Answer: B) Object
39. In Java, events are all the activities that occur between ___________.
- A) A and C
- B) A and B
- C) B and D
- D) C and D
- Answer: B) A and B
41. In which package the methods for receiving and processing events are defined?
- A) java.util
- B) javax.event
- C) java.awt
- D) java.awt.event
- Answer: D) java.awt.event
50. Name the method defined in EventObject class that returns the Object generated from the
event. Select the one correct answer.
- A) getObject()
- B) getEventSource()
- C) getSourceObject()
- D) getSource()
- Answer: D) getSource()
68. The method in the ActionEvent ________ returns the action command of the button.
- A) getAction()
- B) fetchAction()
- C) getCommand()
- D) getActionCommand()
- Answer: D) getActionCommand()
69. The method that is used for registering a keyboard event is known as
____________________.
- A) registerKeyListener()
- B) addKeyListener()
- C) attachKeyListener()
- D) setKeyListener()
- Answer: B) addKeyListener()
70. The MouseEvent class does not define the _____________ integer constant.
- A) MOUSE_WHEELMOVE
- B) MOUSE_DRAGGED
- C) MOUSE_MOVED
- D) MOUSE_PRESSED
- Answer: A) MOUSE_WHEELMOVE
72. The MouseListener's _______________ method is called after the mouse button is released.
- A) public void mouseUp(MouseEvent e)
- B) public void mouseReleased(MouseEvent e)
- C) public void mouseClicked(MouseEvent e)
- D) public void mousePressed(MouseEvent e)
- Answer: B) public void mouseReleased(MouseEvent e)
74. The signature for the registration method for an ActionEvent should be.
- A) public void addActionListener(ActionListener l)
- B) public void registerActionListener(ActionListener l)
- C) public void addEventActionListener(ActionListener l)
- D) public void attachActionListener(ActionListener l)
- Answer: A) public void addActionListener(ActionListener l)
75. The superclass of ContainerEvent, FocusEvent, and WindowEvent is _______________.
- A) ComponentEvent
- B) AWTEvent
- C) EventObject
- D) InputEvent
- Answer: B) AWTEvent
86. What kind of event is fired when the user selects an item from a menu?
- A) MenuEvent
- B) ItemEvent
- C) ActionEvent
- D) SelectEvent
- Answer: C) ActionEvent
87. When a component obtains keyboard focus, which method is invoked?
- A) void focusChanged(FocusEvent fe)
- B) void focusGained(FocusEvent fe)
- C) void keyboardFocus(FocusEvent fe)
- D) void focusActivated(FocusEvent fe)
- Answer: B) void focusGained(FocusEvent fe)
92. When two or more objects are added as listeners for the same event, which listener is first
invoked to handle the event?
- A) The first listener added
- B) The last listener added
- C) There is no way to determine which listener will be invoked first.
- D) All listeners are invoked simultaneously.
- Answer: C) There is no way to determine which listener will be invoked first.
93. When we need to use checkboxes or items from the list or use a checkable menu, an
________________ is generated.
- A) CheckEvent
- B) ItemEvent
- C) CheckboxEvent
- D) SelectEvent
- Answer: B) ItemEvent
97. Which are various methods of WindowListener interface from the following?
- A) void windowOpened(WindowEvent e)
- B) void windowClosing(WindowEvent e)
- C) void windowActivated(WindowEvent e)
- D) All of the above
- Answer: D) All of the above
99. Which class is at the root in the Java event class hierarchy?
- A) EventClass
- B) EventObject
- C) EventSource
- D) RootEvent
- Answer: B) EventObject
100. Which class is used for this Processing Method processActionEvent()?
- A) Button
- B) List
- C) MenuItem
- D) All of the above
- Answer: D) All of the above
107. Which method does display the message whenever there is an item selection or deselection
of the CheckboxMenuItem menu?
- A) itemSelected()
- B) itemChanged()
- C) itemStateChanged method.
- D) itemClicked()
- Answer: C) itemStateChanged method.
108. Which method is applicable if alphanumeric key is pressed?
- A) keyTyped
- B) keyPressed
- C) keyReleased
- D) keyActivated
- Answer: A) keyTyped
118. Which of the following is the highest class in the event delegation class hierarchy?
- A) java.util.EventObject
- B) java.awt.event.Event
- C) java.awt.event.DelegateEvent
- D) java.awt.event.HighestEvent
- Answer: A) java.util.EventObject
A) ActionEvent
- B) ButtonEvent
- C) ClickEvent
- D) PressEvent
- Answer: A) ActionEvent
123. Which of these methods can be used to obtain the command name for invoking ActionEvent
object?
- A) getActionCommand()
- B) retrieveCommand()
- C) fetchActionCommand()
- D) obtainCommandName()
- Answer: A) getActionCommand()
127. Which of these constant values will change when the button at the end of the scrollbar is clicked
to increase its value?
- A) UNIT_DECREMENT
- B) UNIT_INCREMENT
- C) BLOCK_DECREMENT
- D) BLOCK_INCREMENT
- Answer: B) UNIT_INCREMENT
128. Which of these events is generated when the computer gains or loses input focus?
- A) FocusEvent
- B) InputFocusEvent
- C) WindowEvent
- D) KeyEvent
- Answer: A) FocusEvent
129. Which of these events will be notified if the scrollbar is manipulated?
- A) AdjustmentEvent()
- B) ScrollEvent()
- C) ScrollbarEvent()
- D) ManipulationEvent()
- Answer: A) AdjustmentEvent()
131. Which of these events is generated when the component is added or removed?
- A) ContainerEvent
- B) AddRemoveEvent
- C) ComponentChangeEvent
- D) ComponentEvent
- Answer: A) ContainerEvent
136. Which of these methods are used to determine the type of adjustment event?
- A) getAdjustmentType()
- B) determineAdjustmentType()
- C) fetchAdjustmentType()
- D) obtainAdjustmentType()
- Answer: A) getAdjustmentType()
137. Which of these methods are used to register a mouse motion listener?
- A) addMouseListener()
- B) addMouseMotionListener()
- C) registerMouseMotionListener()
- D) attachMouseMotionListener()
- Answer: B) addMouseMotionListener()
138. Which of these methods can be used to know the degree of adjustment made by the user?
- A) getDegree()
- B) fetchAdjustment()
- C) determineAdjustment()
- D) getValue()
- Answer: D) getValue()
140. Which of these methods is used to get the x-coordinate of the mouse?
- A) getX()
- B) fetchX()
- C) obtainX()
- D) determineX()
- Answer: A) getX()
Certainly! Here are the answers and options for the provided multiple-choice s:
143. Which of these methods is used to obtain the object that generated a ComponentEvent?
- A) getComponent()
- B) fetchObject()
- C) determineObject()
- D) getContainer()
- Answer: D) getContainer()
144. Which of these methods is used to obtain the object that generated a WindowEvent?
- A) getWindowObject()
- B) fetchWindowObject()
- C) determineWindowObject()
- D) getWindow()
- Answer: D) getWindow()
147. Which of these methods will respond when you click any button by mouse?
- A) mouseClicked()
- B) buttonClicked()
- C) actionPerformed()
- D) All of these
- Answer: C) actionPerformed()
149. Which two parameters are required for JTable constructor to create a table?
- A) Data array and Column Headings
- B) Rows and Columns
- C) Table size and Color
- D) Data array and Table size
- Answer: A) Data array and Column Headings
150. windowEvent is a subclass of ________________________
- A) EventObject()
- B) ComponentEvent()
- C) AWTEvent()
- D) WindowEvent()
- Answer: C) AWTEvent()
152. __________ is generated when a checkbox is clicked, a list item is clicked, a choice selection is
made, or a checkable menu item is selected or deselected.
- A) MouseEvent
- B) ItemEvent
- C) ActionEvent
- D) CheckboxEvent
- Answer: B) ItemEvent
153. ___________ class provides an empty implementation of all methods in an Event Listener
interface?
- A) Adapter
- B) EmptyListener
- C) AbstractListener
- D) NullListener
- Answer: A) Adapter
154. _________________ generates an event and sends it to one or more listeners.
- A) EventGenerator
- B) EventDispatcher
- C) EventEmitter
- D) Event Source
- Answer: D) Event Source
156. For the following code, select the method that can be used to handle the event.
```java
import java.awt.event.;
import java.awt.;
import java.applet.;
157. For Key events, KeyEvent class is used 2) For Mouse motion events, MouseEvent class is
used 3) For Component events, ComponentEvent class is used 4) For Window events,
WindowEvent class is used
- A) 1, 2, 3 are true
- B) 1, 2, 4 are true
- C) 2, 3, 4 are true
- D) 1, 3, 4 are true
- Answer: D) 1, 3, 4 are true
158. What will be the output of the following code?
```java
import java.awt.;
import java.applet.;
import java.awt.event.;
public mma1(adm a) {
this.a = a;
}
160. What will happen when you attempt to compile and run the following code?
```java
import java.awt.;
import java.awt.event.;
MClick() {
this.addMouseListener(this);
}
162. A Frame's background color is set to Color.Yellow, and a Button's background color is set
to Color.Blue. Suppose the Button is added to a Panel, which is added to the Frame. What
background color will be used with the Panel?
- A) Color.Blue
- B) Color.Yellow
- C) Color.Green
- D) Color.Red
- Answer: B) Color.Yellow
```java
import java.awt.;
import java.awt.event.;
import java.applet.;
Options:
1. `Button no=new Button("NO");`
2. `Button undecided=new Button("Undecided");`
3. `Button maybe=new Button("Maybe");`
4. `Button no=new Button("No");`
---
168: After inserting which statement in the following program, the output window will be
closed when the close button is clicked?
```java
import java.awt.event.;
import java.awt.;
AdapterDemo(Demo d) {
d1=d;
}
public void windowClosing(WindowEvent we) {
d1.dispose();
}
}
```
Options:
1. `addWindowListener(new AdapterDemo(this));`
2. `this.addWindowListener(new AdapterDemo(this));`
3. `AdapterDemo ad = new AdapterDemo(this);`
4. `new AdapterDemo(this).addWindowListener();`
Options:
1. `MouseListener`
2. `All of the Above`
3. `WindowAdapter`
4. `ActionListener`
---
170:An event is generated when the internal state of the event source is __________.
Options:
1. `stopped`
2. `changed`
3. `initialized`
4. `paused`
Answer: 2. `changed`
---
171:Analyse the following code and find out errors:
```java
import java.awt.;
import java.applet.;
import java.awt.event.;
public class Demo extends Applet implements ItemListener {
Label jlb1 = new Label("Select Color:");
Choice ch = new Choice();
TextField jtf1 = new TextField(12);
public void init() {
city=new List(4);
city.addItem("Pune");
city.addItem("Mumbai");
city.addItem("Nagpur");
city.addItem("Kolhapur");
city.addItem("Solapur");
jtf=new TextField(12);
add(jtf);
}
public void itemStateChanged(ItemEvent ie) {
String s=(String)ie.getItem();
jtf1.setText(s);
}
}
```
Options:
1. `Error is in the statement String s=(String)ie.getItem();`
2. `Error is in the statement city=new List(4);`
3. `Error is in the statement add(jtf);`
4. `No errors in the code.`
173:Analyse the following code and find out the missing statement:
```java
import java.awt.;
import java.applet.;
Options:
1. `TextField jtf=new TextField(12);`
2. `TextField jtf;`
3. `TextField jtf=new TextField();`
4. `All of the above`
---
```java
import javax.swing.;
import javax.swing.border.;
import java.awt.;
Options:
1. `Two buttons displayed with the same border.`
2. `Two buttons displayed with different borders.`
3. `No buttons displayed.`
4. `One button displayed with the specified border.`
```java
import javax.swing.;
import java.awt.;
JButton("Java"));
add(new JButton("Java"));
add(new JButton("Java"));
add(new JButton("Java"));
}
Options:
1. `Four buttons are displayed with the same text "Java".`
2. `One button is displayed with the text "Java".`
3. `No buttons are displayed.`
4. `Two buttons are displayed with the text "Java".`
Answer: 1. `Four buttons are displayed with the same text "Java".`
---
Options:
1. `CheckBox`
2. `RadioButton`
3. `ToggleButton`
4. `ComboBox`
Answer: 2. `RadioButton`
177: Choose the correct missing statement from the given code:
```java
import java.awt.;
import java.applet.;
import java.awt.event.;
public class eventdemo2 extends Applet implements ActionListener {
TextField t1, t2;
Button b1;
Options:
1. `b1.addActionListener(this);`
2. `t1.addActionListener(this);`
3. `b1.addMouseListener(this);`
4. `t2.addActionListener(this);`
Answer: 1. `b1.addActionListener(this);`
---
```java
import java.awt.;
import java.awt.event.;
import java.applet.;
Options:
1. The program will display "Button clicked" message in the status bar on clicking the button with the
mouse.
2. The program will display "Mouse clicked" message in the status bar on clicking the button with the
mouse.
3. The program will display "Button pressed" message in the status bar on pressing the button.
4. The program will not display any message.
Answer: 1. The program will display "Button clicked" message in the status bar on clicking the
button with the mouse.
---
```java
/ <applet code="MyControl10" width=300 height=300> </applet> /
import java.applet.;
import java.awt.;
import java.awt.event.;
Options:
1. All of the above
2. Only "Red" checkbox selected will fill the rectangle with red color.
3. Only "Green" checkbox selected will fill the rectangle with green color.
4. Only "Blue" checkbox selected will fill the rectangle with blue color.
Answer: 2. Only "Red" checkbox selected will fill the rectangle with red color.
---
\
180: Choose the correct output from the following options when the key is pressed.
```java
/ <applet code="SimpleKey" width=300 height=100> </applet> /
import java.awt.;
import java.awt.event.;
import java.applet.;
Options:
1. Key Down msg shown at the status bar
2. Key Up msg shown at the status bar
3. Key Typed msg displayed at the specified position
4. No message is displayed