Chapter 3
Chapter 3
1) Which of these packages contains all the classes and methods required for even handling in Java?
a) java.applet
b) java.awt
c) java.event
d) java.awt.event
a) KeyListener()
b) addKistener()
c) addKeyListener()
d) eventKeyboardListener()
a) Exception Handling
b) String Handling
c) Event Handling
d) None of the above
a) Swing
b) AWT
c) Both A & B
d)None of the above
12) Which class header would be used to create an applet that accepts a click on a button?
a) ItemListener
b) ActionListener
c) MouseListener
d) WindowListener
14) Which of these methods will respond when you click any button by mouse?
a) mouseClicked()
b) mouseEntered()
c) mousePressed()
d) All of the mentioned
18) Consider the following output. Find the missing statement in the program.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
/* <applet code="SimpleKey1" width=300 height=100> </applet> */
public class SimpleKey1 extends JApplet implements KeyListener
{
String msg = "";
int X = 10, Y = 20;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key Down");
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Up");
public void keyTyped(KeyEvent ke)
{
msg += ke.getKeyChar();
repaint(); }
public void paint(Graphics g)
{
g.drawString(msg, X, Y);
}
}
a) Missing Semicolon
b) Missing {
c) Missing }
d) Missing ()
a) mousePressed(MouseEvent e){ }
b) MousePressed(MouseClick e){}
c) functionKey(KeyPress k){}
d) Added(ContainerEvent e){}
20) Which methods are used to close the window?
a) setVisible(false)
b) windowClosing()
c) stop()
d) Both a and b
21) To process scroll bar events, you need to implement ________________ interface.
a) adjustmentlistener
b) MouseListener
c)AdjustmentListener
d) KeyListener
24) ______________ method returns a value that indicates which modifier key were pressed when the
action event is generated.
a) getModifiers()
b) setModifiers()
c) getActionCommand()
d)none of these
28) Which of the following is the highest class in the event delegation model?
a)java.util.EventListener
b) java.util.EventObject
c) java.awt.AWTEvent
d) java.awt.event.AWTEvent
31) Which of these methods can be used to obtain the coordinates of a mouse?
a)getPoint()
b)getCoordinates()
c)getMouseXY()
d)getMouseCordinates()
32) Which of the following method is invoked when a window is changed from a normal to a minimized
state?
a) windowIconified()
b) windowClosed()
c)windowDeactivated
d) windowOpened()
34) Which of the following method does not belongs to WindowListerner interface?
a) windowActivated
b) windowReactivated
c) windowDeiconified
d) windowClosed()
35) Which event is generating when checkable menu item is selected or deselected?
a) KeyEvent
b) ListEvent
c) ItemEvent
d) MouseEvent
36) Which are two ways to create Java Anonymous inner class?
a) Class, Interface
b) Applet
c) Panel, Frame
d)Event
37) The ___________method returns a reference to the component that was added to or removed from the
container.
a)getParent()
b)getComponent( )
c)getChild( )
d)geoReferenced()
38) Inner class can access all the members of outer class including_____data members and methods.
a) public
b)Protected
c)friend
d)private
39) In case of______ we can implement only required methods of any interface.
a)adapter classes
b)Event
c)EventObject
d)Listener
44) If a class MyWindowAdapter extends WindowAdapter and implements the windowClosing() method.
How to register this class ?
a) this.addWindowListener(new MyWindowAdapter());
b) addWindowListener(new MyWindowAdapter());
c) this.addListener(new MyWindowAdapter());
d) addWindowListener(new MyAdapter());
45) In the following code, what is the name of the inner class?
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AdapterDemo" width=300 height=100> </applet>
*/
public class AdapterDemo extends Applet
{
public void init()
{
addMouseListener(new MyMouseAdapter(this));
}
}
class MyMouseAdapter extends MouseAdapter
{
AdapterDemo adapterDemo;
public MyMouseAdapter(AdapterDemo adapterDemo)
{
this.adapterDemo = adapterDemo;
} // Handle mouse clicked.
a) Mouse pressed
b) Mouse clicked
c) Mouse dragged
d) Mouse released
a)KeyListener, KeyEvent
b)MouseListener, MouseEvent
c)ActionListener, ActionEvent
d)WindowListener, WindowEvent
50) In the Delegation Event Model, a user interface element is able to delegate, the processing of an
event............................
a) class
b) a separate piece of code
c) interface
d) listener
51) Listeners are created by implementing one or more of the defined by the…………….
a) interfaces
b)class
c)listener
d)handler
53) The example of user interface elements that implement the ItemSelectable
interface-
a) checkbox and checkboxgroup
b) textfield and textarea
c) radiobutton and button
d) Lists and choices
68) Which method not use to obtain the coordinates of the mouse-
a) string getcode()
b) int GetKEY()
c) int getKeyCode( )
d) int getCode()
69) Which method used to capture ALT, CTRL, META OR SHIFT keys-
a) getModifiers( )
b) getMod( )
c) modifiers ( )
d) getAlt( )
70) Which top-level class provides methods to add and remove keyboard and mouse event
listeners-
a) Container
b) Applet
c) Window
d) Component
a) Frame
b) Window
c) WindowExample
d)Example
72) Assuming we have a class which implements the ActionListener interface, which method
should be used to register this with a Button?
a) EnterActionListener(*);
b) addListener(*);
c) ActionListener(*);
d)addActionListener(*);
81) Identify correct general form of MouseClicked( ) method of Mouse Listener interface.
a) void mouseClicked(me)
b) void Clicked(MouseEvent me)
c) void mouseClicked(MouseEvent me)
d) void mouseClick(MouseEvent me)
85) Which of these method are used to register a keyboard event Listener ?
a) addMouseListener()
b) addKeyListener()
c) KeyListener()
d) addKey()
86) Which of these methods are used to register a mouse motion listener?
a) addMouseMotionListner()
b) addMouseListner()
c) addMouseWheelListner()
d) MouseMotionListner()
96) Following are the integer constants which does not belong to ComponentEvent class .
a) COMPONENT_HIDDEN
b) COMPONENT_MOVED
c) COMPONENT_RESIZED
d) COMPONENT_ICONIFIED
import java.applet.*;
import java.awt.event.*;
/*
<applet code="AnonymousInnerClassDemo" width=200 height=100>
</applet>
*/
public class AnonymousInnerClassDemo extends Applet
{
public void init()
{
addMouseListener(new ______________()
{
public void mousePressed(MouseEvent me)
{
showStatus("Mouse Pressed");
}
});
}
}
a) MouseAdapter
b) MouseMotion Adapter
c) KeyAdapter
d)mousepressed
98) An adapter class provides an ……………..implementation of all methods in an event listener interface.
a) interface
b) abstract
c) empty
d)runnable
a) ScroolbarEvent
b) AdjustmentEvent
c) ActionEvent
d)TextAreaEvent
100) Identify correct name of event:
a) InputEvent
b) MouseMotionEvent
c) KeyEvent
d) MouseEvent
102) Listeners are created by implementing one or more of the interfaces defined by the___________
package.
a) java.awt.event
b) java.util.event
c) java.lang.event
d)java.awt.listener
105) In List event source, generates ___________ events when an item is selected or deselected.
a) action
b) item
c)text
d)window
106) In Menu Item, generates ____________ events when a menu item is selected.
a) action
b) item
c) text
d)window
107) In Menu Item, generates ________ events when a checkable menu item is selected or
deselected.
a) action
b) item
c) text
d) window
f.add(l);
f.add(area);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public void keyReleased(KeyEvent e) {
String text=area.getText();
String words[]=text.split("\\s");
l.setText("Words: "+words.length+" Characters:"+text.length());
}
a) KeyAdapterExample
b) KeyAdapter
c) KeyExample
d) AdapterExample
import java.awt.*;
import java.awt.event.*;
public class MouseAdapterExample extends MouseAdapter
{
Frame f;
MouseAdapterExample()
{
f=new Frame("Mouse Adapter");
f.addMouseListener(this);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
Graphics g=f.getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),30,30);
}
public static void main(String[] args) {
new _______________();
}
}
a) mousePressed
b) MouseAdapterExample
c) mouseClicked
d) MouseAdapter
a) Statement 2 is False
b) Statement 3 is False
c) Statement 1 is False
d) Statement 1 and 4 are False
import java.applet.*;
import java.awt.event.*;
/*
<applet code="InnerClassDemo" width=200 height=100>
</applet>
*/
public class InnerClassDemo extends Applet {
public void init() {
addMouseListener(new MyMouseAdapter());
}
class MyMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent me) {
showStatus("Mouse Pressed");
}
}
}
import java.applet.*;
import java.awt.event.*;
/*
<applet code="InnerClassDemo" width=200 height=100>
</applet>
*/
public class InnerClassDemo extends Applet {
public void init() {
__________ (new MyMouseAdapter());
}
class MyMouseAdapter extends _________________
{
public void mousePressed(MouseEvent me) {
showStatus("Mouse Pressed");
}
}
}
a) addMouseListener, MouseAdapter
b) addmouselistener, MouseAdapter
c) addMouseListener, mouseAdapter
d) addMouseListener, mouseadapter
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="AdapterDemo" width=300 height=100>
</applet>
*/
public class AdapterDemo extends Applet
{
public void init()
{
addMouseListener(new MyMouseAdapter(this));
}
}
class MyMouseAdapter extends MouseAdapter
{
_________________ adapterDemo;
public MyMouseAdapter(AdapterDemo adapterDemo)
{
this.adapterDemo = adapterDemo;
}
// Handle mouse clicked.
public void mouseClicked(MouseEvent me)
{
adapterDemo.showStatus("Mouse clicked");
}
}
a) AdapterDemo
b) adapterDemo
c) adapterdemo
d) Adapterdemo
122) The following code segment shows that a class MyWindowAdapter extends WindowAdapter and
implements the windowClosing() method.
Identify the correct adapter name and event name.
a) WindowAdapter, WindowEvent
b) MyWindowAdapter, WindowEvent
c) WindowAdapter, windowEvent
d) WindowEvent, WindowAdapter
a) FocusAdapter, FocusEvent
b) focusAdapter, FocusEvent
c) focusadapter, FocusEvent
d) FocusAdapter, Focusevent
a) ContainerEvent
b) ComponentEvent
c) FocusEvent
d) WindowEvent
import java.awt.*;
import java.awt.event.*;
public class KeyListenerExample extends Frame implements KeyListener { Label MyLAbel;
TextArea area;
KeyListenerExample()
{
MyLabel=new Label();
MyLabel.setBounds(20,50,100,20);
area=new TextArea();
area.setBounds(20,80,300, 300);
area.addKeyListener(this);
add(MyLabel);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e)
{
MyLabel.setText("Key Pressed");
}
public void keyReleased(KeyEvent e)
{
MyLabel.setText("Key Released");
}
public static void main(String[] args)
{
new KeyListenerExample();
}
}
class Outer
{
void outerMethod() {
System.out.println("inside outerMethod");
// Inner class is local to outerMethod()
class Inner
{
void innerMethod() {
System.out.println("inside innerMethod");
}
}
Inner y = new Inner();
y.innerMethod();
}
}
class MethodDemo
{
public static void main(String[] args) {
Outer x = new Outer();
x.outerMethod();
}
}
a)
inside outerMethod
inside innerMethod
b)
inside innerMethod
inside outerMethod
c)No output
d)inside innerMethod
133) Can outer Java classes access inner class private members?
a) Yes
b) No
134) An _______________ class listener interface and Event class Listener interface has
same name.
a) Main class
b) Adapter Class
c) Event Class
d) Inner Class
135) An adapter class provides an __________ implementation of all methods in an event listener
interface.
a) Complete
b) Multiple
c) Empty
d) None of above
136) Adapter classes are an _________ class for receiving various events.
a) Abstract class
b) Super class
c) Derived class
d) Base class
137) Which is the abstract adapter class for receiving keyboard focus events.
a) ComponentAdapter
b) KeyAdapter
c) FocusAdapter
d) MouseAdapter
144) Which method used to capture ALT, CTRL, META OR SHIFT keys-
a) getModifiers( )
b) getmodifiers( )
c) GetModifiers( )
d) Getmodifiers( )
145) Suppose that you want to have an object eh handle the TextEvent of a TextArea object t. How should
you add eh as the event handler for t?
a) t.addTextListener(eh);
b) eh.addTextListener(t);
c) addTextListener(eh.t);
d) addTextListener(t,eh);