Monday, May 27, 2019

Java program (Action Listener)


Java program to change text enter in textField into italic,bold,and  arial.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="magic3.java" width=100 height=1000>
</applet>
*/
public class magic3 extends Applet implements ActionListener
{
       TextField tf;
       Button arial,bold,italic;
       Label l;
       Font f;
       public void init()
       {
              setLayout(null);
              setBackground(Color.pink);
             
              tf=new TextField();
             
              tf.setBounds(100,100,250,30);
              add(tf);
             
              arial=new Button("Arial");
             
              arial.setBounds(120,150,50,20);
              add(arial);
                          
              bold=new Button("Bold");
      
              bold.setBounds(180,150,50,20);
                     add(bold);
                          
              italic=new Button("Italic");
              add(italic);
              italic.setBounds(240,150,50,20);
      
              l=new Label();
              l.setBounds(100,200,25,30);
              add(l);
             
              arial.addActionListener(this);
               bold.addActionListener(this);
                italic.addActionListener(this);
       }
       public void actionPerformed(ActionEvent ae)
       {
              if(ae.getSource()==arial)
              {
                     f=new Font("Arial Narrow",0,20);
                     l.setText(tf.getText());
                     l.setFont(f);
              }
              else if(ae.getSource()==bold)
              {      f=new Font("Arial Narrow",1,20);
                     l.setText(tf.getText());
                     l.setFont(f);
              }
              else if(ae.getSource()==italic)
              {      f=new Font("Arial Narrow",2,20);
                     l.setText(tf.getText());
                     l.setFont(f);
              }
       }
}
Java program to  perform operation such as Tan,sin,sqrt,in applet

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class sak1 extends Applet implements ActionListener
{
       TextField tf1,tf2;
       Button log,sin,sqrt,tan;
       public void init()
       {
              setLayout(null);
              setBackground(Color.gray);
      
              tf1=new TextField();
              tf1.setBounds(100,100,250,30);
              add(tf1);
      
              log=new Button("Log");
              log.setBounds(120,150,50,20);
              add(log);
             
              sin=new Button("Sin");
              sin.setBounds(180,150,50,20);
              add(sin);
             
              sqrt=new Button("Sqrt");
              sqrt.setBounds(240,150,50,20);
              add(log);

              tan=new Button("tan");
              tan.setBounds(300,150,50,20);
              add(log);

              tf2=new TextField();
              tf2.setBounds(100,200,250,30);
              add(tf2);
             
              log.addActionListener(this);
                     sin.addActionListener(this);
                     sqrt.addActionListener(this);
                     tan.addActionListener(this);
       }
       public void actionListener(ActionEvent ae)
       {
       if(ae.getSource()==log)
       {
              double d1=Double.parseDouble(tf1.getText());
              double ans=Math.log(d1);
              tf2.setText("Log("+d1+")="+ans);
       }
       else if(ae.getSource()==sin)
       {
             
              double d1=Double.parseDouble(tf1.getText());
              double ans=Math.sin(d1);
              tf2.setText("Sine("+d1+")="+ans);
       }
       else if(ae.getSource()==sqrt)
       {
             
              double d1=Double.parseDouble(tf1.getText());
              double ans=Math.sqrt(d1);
              tf2.setText("Sqrt("+d1+")="+ans);
       }
       else if(ae.getSource()==tan)
       {
      
              double d1=Double.parseDouble(tf1.getText());
              double ans=Math.tan(d1);
              tf2.setText("Tan("+d1+")="+ans);
       }
}
}
/*
<applet code="sak1.java" width=100 height=200>
</applet>
*/
     


java program to count number of vowel enter in textfield in applet


import java.applet.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.lang.*;

public class pst1 extends Applet implements ActionListener
{            
       TextField tf;
       TextArea ta;
       Button b1;
       int count=0;
       public void init()
       {
              setLayout(null);
             
              tf=new TextField();
              add(tf);
              tf.setBounds(20,20,120,30);
      
              ta=new TextArea();
              add(ta);
              ta.setBounds(20,60,120,30);

              b1=new Button("calculate");
              add(b1);
              b1.setBounds(20,120,100,40);
              b1.addActionListener(this);
       }
       public void actionPerformed(ActionEvent ae)
       {
              String str=ta.getText();
              int len=str.length();
              for(int i=0;i<len;i++)
              {
                     if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='u')
                     count++;
              }
       tf.setText(""+count);
       }
}

/*
<applet code="pst1.java" width=100 height=100>
</applet>
*/








 


Chapter 3(MCQ:51-100)

DatagramPacket
DatagramPacket uses different constructors depending on whether thepacket will be used to send data or to receive data. This is a little unusual.
Normally, constructors are overloaded to let us provide different kinds of
information when we create an object, not to create objects of the same class
that will be used in different contexts. In this case, all constructors take as
arguments a byte array that holds the datagram's data and the number of
bytes in that array to use for the datagram's data. When we want to receive a
datagram, these are the only arguments we provide; in addition, the array
should be empty. When the socket receives a datagram from the network, it
stores the datagram's data in the DatagramPacket object's buffer array, up to
the length we specified.

51 The valid sequence for URL format is ___________________ = protocol, Hostname, portnumber, filepath

52The ____________method in the InetAddress class returns the IP
address.
= getHostAddress()

53 The ______________class is used to create TCP server. = ServerSocket

54 To create an InputStream on a Socket s, you use __________. = InputStream in = s.getInputStream();

55 To obtain an ObjectOutputStream from a socket,use__________ = new ObjectOutputStream(socket.getOutputStream())

56 To return an instance of InetAddress class you have to use = Factory Methods of InetAddress class

57To return the currently intalled policy object______________method is
used.

= public static Policy getPolicy()

58 UDP is not __________oriented protocol. = Connection

59 UDP Packets are known as ___________________ = Datagram

60 UDP Protocol uses following classes for communication = DatagramPacket and DatagramSocket

61 UDP Stands for ________ = User Datagram Protocol

62URL class has several constructors; each can throw _____________________.
= MalformedURLException

63 What does the openConnection() method of java.net.* return? = Object of URLConnection class

64 What does URL stands for? = Uniform Resource Locator

65 What is return type of getAddress() method of InetAddress class? = byte[]

66 What is Second part of URL address = Hostname

67 What is Socket? = All of these

68 What is the first part of URL address? = Protocol

69 What is the optional part of URL Address = Port Number

70What is the return type of the method getAllByName( ) of InetAddressclass?
= InetAddress[ ]


71 What will be displayed in the output? import java.net.*; class
myAddress { public static void main (String args[]) { try { InetAddress
address = InetAddress.getLocalHost(); System.out.println(address); }
catch (UnknownHostException e) { System.out.println("Could not find
= The internet address of the host
this computer's address."); } } }

72 When a socket object is created __________ =
It implicitly establishes a connection between the client &amp;
server

73 When a URL object is created __________ = a connection is automatically established with that URL

74Which class defines following methods. 1. int getContent Length() 2.
long getDate() 3. long getExpiration()
= URLConnection

75Which class is used to access actual bits or content information of a
URL?
= URLConnection

76 Which class is used to create client in TCP/IP? = Socket
77Which class is used to designed to be a "listener" which waits for client
to connect before doing anything.
= ServerSocket

78 Which class is used to implement datagrams with UDP protocol. = DatagramSocket

79Which class of java.net package can be used to find the host name
and IP address of the client.
= The InetAddress class

80 Which classes are used for connection-oriented socket programming = Both A andB

81Which constructor of DatagramSocket class is used to create a
datagram socket and bind it with the given port number?
= DatagramSocket(int port)

82Which datagram method returns the byte array of data contained in
the datagram?= byte[] getData()

83Which exception indicate that the IP address of a host could not be
determined= UnknownHostException

84 Which is connection less protocol = UDP

85 Which is not a method of URL Connection? = Date getLastModified( )

86 Which is not valid method of URL class? = getUrl()

87 Which is the correct syntax from given? = byte[] getAd
dress()
88 Which is the reliable protocol of networking ? = TCP

89 Which method is used to know the full URL of an URL object? = toExternalForm()

90 Which method is used to retrieve the host name associated with URL = getHost()

91 Which method is used to return the IPAddress of local machine = static InetAddress getLocalHost( )

92Which method of ServerSocket will wait for a client to initiate
communications and then communicate with the client= accept()

93 Which method of URL class is used to create object of URLConnection? = openConnection

94 Which of the following are Factory Methods of InetAddress class = All the Above

95 Which of the following class defines accept() method? = ServerSocket

96 Which of the following constructor of InetAddress is generally used? = InetAddress class does not contain any visible constructor.

97 Which of the following is a connection oriented protocol? = TCP

98Which of the following is mediator between real web server and client
application = Proxy

99 Which of the following is not a constructor of DatagramSocket = DatagramSocket(InetAddress address)

100Which of the following is not a Factory method of an InetAddress
Class?= Sting getHostAddress()


Sunday, May 26, 2019

Chapter 3(MCQ:1-50)

Chapter 3
Socket Overview 
There is a meter some where between our
house and the rest of the network. For each kilowatt of power that goes
through that meter, we are billed. The bill comes to our address. So even
though the electricity flows freely around the power grid, all of the sockets in
our house have a particular address. The same idea applies to network sockets,
except we talk about TCP/IP packets and IP addresses rather than electronsand street addresses. Internet Protocol (IP) is a low-level routing protocol that
breaks data into small packets and sends them to an address across a network,
which does not guarantee to deliver said packets to the destination.
Transmission Control Protocol (TCP) is a higher-level protocol that manages to
robustly string together these packets, sorting and re-transmitting them as
necessary to reliably transmit our data. A third protocol, User Datagram
Protocol (UDP), sits next to TCP and can be used directly to support fast,
connectionless, unreliable transport of packets.


1 Calling Server Socket() constructor with port value 'zero'
means______________________.= use a port number that is automatically allocated.

2 TCP/IP reserves the ____________ ports for specific protocols = lower 1024

3Which of these package contains classes and interfaces for
networking?= java.net

4 A ServerSocket can connect to ________ clients. = multiple

5 A set of rules that governs data communication. = protocol

6 A socket identifies __________ in network. = a communication end point

7 A socket is combination of = Both A and B

8A socket is __________________ of a two-way communication link
between two programs running on two hosts in the network.

= one end-point

9 Access control does not deals with = Authenication

10A_______ is responsible for determining whether code executing in
the Java runtime environment has permission to perform a securitysensitive
operation.= policy object

11 byte[] getData() method of DatagramPacket class
returns______________________
= Byte array of data contained in datagram

12Class URL represents __________________________________, a
pointer to a "resource" on the World Wide Web
= Uniform Resource Locator

13 Communication using TCP protocol is __________and _________ = connection-oriented, concurrent

14Connection oriented communication is possible using
___________________classes of Java.
= Socket and ServerSocket

15 How many bits are in a single IPv4 address? = 32

16 How many bytes are required to represent an IPv4 address? = 4

17In InetAddress class which method returns the host name of the IP
Address? = public String getHostName()

18 In the format for defining the URL what is the last part? = File path
Welcome M3001 [117.239.186.68]
My Home Log Out
Maharashtra State Board of Technical Education
(A utonomous) (ISO 9001:2008) (ISO/IEC 27001:2005)
19 In the URL, http://www.osborne.com:80/index.htm, 80 represents = Port number

20 In UDP send() and receive() methods belong to which class? = DatagramSocket

21 In which class the constructors are not visible = Inetaddress class

22 Inet Address class encapsulates__________________ = Both A and B

23 IP Stands for ________ = Internet Protocol

24 Java.net package include following classes. = URLConne
ction, Socket, InetAddress
25Name the class which is used to create a port where the server will
listen?= ServerSocket
26 Permission class of java.security package is _______ class = abstract

27 Permission class belongs to which package? = java.security

28 Port Number for FTP is = 21

29 Port number of Email is ____________. = 25

30 Pretty Good Privacy (PGP) is used in security of = Email

31 Reserved port no of HTTP = 80

32 Resolver in DNS sysytem maps = Domain name to ip address

33 Select the proper constructor of ServerSocket class = All of above

34 Select the proper constructor of URL class = All of the above
35 TCP is Which Type of Protocol ? = Connection oriented protocol
36 TCP is _________oriented protocol = stream

37 TCP Stands for ________ = Transmission Control Protocol

38 TCP/IP sockets are used to impelement __________ connections. = All of the above

39The class which encapsulates both the numerical IP address and the
domain name for that address.= InetAddress

40The constructor which is used in ServerSocket may
be______________.= ServerSocket (int port).

41 The constructor which is used to create client socket is ____________. = Socket(InetAddress IPAddress, int port).

42The factory method for retrieving objects of InetAddress is
___________________.
= All of the mentioned
The factory method which returns an array of InetAddresses that

represent all of the addresses that a particular host name resolves to.= getAllByName( )
44The java.net.InetAddress class provides methods to get
the________________________
= IP of any host name

45 The method int getPort( ) of Socket class returns____________ = remote port to which this Socket object is connected.
46The number 80 in following URL specifies?

= Port number
http://www.rediff.com:80/index.htm/

47 The openConnection() is the method of which Class? = URLConnection

48The openConnection() method of URL class return_____________Object.
= URLConnection

49 The return type of getHostAddress() method is_____________ = string 

50The server listens for a connection request from a client using the
following statement:
= Socket s = ServerSocket.accept()

Chapter 2(MCQ:101-200)

Chapter 2

The modern approach to handling events is based on the delegation event
model, which defines standard and consistent mechanisms to generate and
process events. Its concept is quite simple: a source generates an event and
sends it to one or more listeners. In this scheme, the listener simply waits until
it receives an event. Once received, the listener processes the event and then returns. The advantage of this design is that the application logic that processes
events is cleanly separated from the user interface logic that generates those
events. A user interface element is able to “delegate” the processing of an
event to a separate piece of code.

 For MCQ 1-100 Chapter 2

101 Which event is generated by List = Both A and B

102 Which Event is generated by Scrollbar class? = AdjustmentEvent

103 Which is a method of the MouseMotionListener Interface? = public void mouseDragged(MouseEvent )

104 Which is not the correct listener = TextAreaListener

105 Which is the interface of MouseEvent class ? = MouseListener

106 Which Listener handles all List related Events = ItemListener

107Which method does display the message whenever there is an item selection or deselection of
the CheckboxMenuItem menu?= itemStateChanged method.

108 Which method is applicable if alphanumeric key is pressed. = keyTyped

109 Which method is defined when action event is occurs? = actionPerformed()

110 Which Method Is used to register a mouse Motion Listner = addMouseMotionListener()

111 Which method used for return for mouse co-ordinate ? = getX () ,getY()

112 Which methods are defined by ComponentListener = void componentHidden(ComponentEvent obj)

113 Which of the following are correct event handling methods = mousePressed(MouseEvent e){}

114 Which of the following are true? = Both A and B

115 Which of the following componant generate ActionEvent? = none

116 Which of the following component can generate ItemEvent? = CheckBoxMenuItem

117 Which of the following is not event class is Java? = ClickEvent

118 Which of the following is the highest class in the event delegation class hierarchy? = java.util.EventObject

119 Which of the following is the legal adapter classes in java. = MouseAdapter

120 Which of the following Listeners have their Adapter class = WindowListener

121 Which of the following method is used to register a listener to a button ? = addActionListener(ActionListener obj)
122 Which of these event is generated when a button is pressed? = ActionEvent

123Which of these methods can be used to obtain the command name for invoking ActionEvent
object?= getActionCommand()

124 Which of these a AdapterClass = KeyAdapter class

125 Which of these adapter class from follwing? = MouseAdapter

126 Which of these are constants defined in Window Event class = All the Above

127Which of these constant value will change when the button at the end of scroll bar was clicked
to increase its value?
= UNIT_INCREMENT

128 Which of these event is generated when computer gains or loses input focus? = FocusEvent

129 which of these event will be notified if scrollbar is manipulated? = AdjustmentEvent()

130 Which of these events is generated when a window is minimized? = Both a and b

131 Which of these events is generated when the component is added or removed? = ContainerEvent

132 Which of these events will be generated if we close an Frame's window? = WindowEvent

133 Which of these interfaces define a method actionPerformed()? = ActionListener

134 Which of these is super class of WindowEvent class? = ComponentEvent

135 Which of these is superclass of all Adapter classes? = Applet

136 Which of these methods are used to determine the type of adjustment event? = getAdjustmentType()

137 Which of these methods are used to register a mouse motion listener? = addMouseMotionListener().

138 Which of these methods can be used to know the degree of adjustment made by the user? = getValue()

139 Which of these methods is defined in MouseMotionAdapter class? = mouseDragged()

140 Which of these methods is used to get x coordinate of the mouse = getX()

141 Which of these methods is used to get Y coordinate of the mouse? = getY()

142 Which of these methods is used to know the full URL of an URL object? = getHOst()

143 Which of these methods is used to obtain the object that generated a ComponentEvent. = getContainer()

144 Which of these methods is used to obtain the object that generated a WindowEvent? = getWindow()

145 Which of these methods will be invoked if a character is generated? = keyTyped()

146 Which of these methods will respond when you click any button by mouse? = All of the mentioned

147 Which of these methods will respond when you click any button by mouse? = All of these

148 Which of these not a constants defined in ComponentEvent class? = COMPONENT_SIZED

149 Which two parameters are required for JTable constructor to create a table? = Data array and Column Headings

150 windowEvent is a subclass of ________________________ = ComponentEvent()

151 _______ generates action events when an item is double-clicked. = List

152__________is generated when checkbox is clicked, list item is clicked, choice selection is made
or checkable menu item is selected or deselected. = ItemEvent

153___________class provides an empty implementation of all methods in an Event Listener
interface?= Adapter
154 _________________generates an event and sends it to one or more listeners. = Event Source

155ActionEvent: It indicates the component-defined events occurred b)MouseEvent: Events
generated during the mouse operation for the object c) FocusEvent:This class indicates about
the focus where the focus has gained or lost by the object. d) KeyEvent: It is generated When
key is pressed
2 Y2 U 2 All a,b,c and d are correct

156For the following code select the method that can be used to handle event.
importjava.awt.event.*; import java.awt.*; importjava.applet.*; public class checkbackg extends
Applet implements ItemListener { Checkbox m1,m2,m3; public void init() { m1=new
Checkbox("A"); m2=new Checkbox("B"); m3=new Checkbox("C"); add(m1); add(m2); add(m3);
m1.addItemListener(this); m2.addItemListener(this); } public void
__________________(ItemEvent ie) { if(ie.getSource()==m1) setBackground(Color.red);
if(ie.getSource()==m2) setBackground(Color.green); } } /*<applet code=checkbackg.class
height=150 width=150> </applet>*/
= itemStateChanged(ItemEvent ie
)
157 1)For Key events, KeyEvent class is used 2)For Mouse motion events, MouseEvent class is used
3)For For Component events, CoumponentEvent class is used 4)For Window events, WindowE
class is used
= 1,2,3 are true
1. What will be the output of the following code? import java.awt.*; import java.applet.*; import

158java.awt.event.*; public class adm extends Applet { public void init() {
addMouseMotionListener(new mma1(this)); } } class mma1 extends MouseAdapter { adm a;
public mma1(adm a) { this.a=a; } public void mouseDragged(MouseEvent me) {
a.showStatus("mouse dragged"); } } /*<applet code="adm" width=500 height=500>
</applet>*/
2 Y2 U 2 S1-chapter2(Q1.C).jpg

159.Which line of code is missing in following code and error statement code line import java.awt.*;
import java.awt.event.*; import java.applet.*; /*<applet code="KeyEventDemo.class"
width=400 height=400> </applet>*/ public class KeyEventDemo extends Applet implements
_____________ { String msg=""; public void init() { addKeyListener(this); } public void
keyPressed(KeyEvent e) { showStatus("Key Down"); int key=e.getKeyCode(); repaint(); } public
void keyTyped(KeyEvent e) { msg+=e.getKeyChar(); repaint(); } public void paint(Graphics g) {
g.drawString(msg,10,20); } }
=
KeyListener and public void keyReleased(KeyEvent e) {
showStatus(&quot;Key up&quot;); }

160 What will happen when you attempt to compile and run the following code? import
java.awt.*; import java.awt.event.*; public class mouseClick extends Frame implements
MouseListener{ public static void main(String argv[]){ mouseClick s = new MClick(); } MClick(){
this.addMouseListener(this); } public void mouseClicked(MouseEvent e){
System.out.println(e.getWhen()); } }
= Error not override MouseListener methods ,class name not found

161. Which statement is incorrect or missing in the following code import java.awt.*; import
java.awt.event*; import javax.swing.*; public class radio extends JApplet implements
ActionListener { pulic void init() { c.setLayout(new FlowLayout()); JRadioButton b1=new
JRadioButton("Red"); b1.addActionListener(this); c.add(b1); JRadioButton b2=new
JRadioButton("Green"); b2.addActionListener(this); c.add(b2); JRadioButton b3=new
JRadioButton("Blue"); b3.addActionListener(this); c.add(b3); ButtonGroup bg=new
ButtonGroup(); bg.add(b1); bg.add(b2); bg.add(b3); } public void actionPerformed(ActionEvent
e){ setBackground(e.getActionCommand()); } }
= Container c=getContentPane();

162A Frame's background color is set to Color.Yellow, and a Button's background color is 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?
= Color.Yellow.
163 A Java exception is an instance of __________. 2 N U 2 Throwable

164 Abstract Methods of MouseMotion Listener Interface. = mouseMoved(MouseEvent) mouseDragged(MouseEvent)

165 Action event is applied on 2 N U 2 Button, TextField, List, Menu

166 Adapter classes are similar to EventListener interfaces? = True

167Add the missing statement.. import java.awt.*; import java.awt.event.*; import java.applet.*; /*
<applet code="ButtonDemo" width=250 height=150> </applet> */ public class ButtonDemo
extends Applet implements ActionListener { String msg = ""; Button yes, no, maybe; public void
init() { yes = new Button("Yes"); maybe = new Button("Undecided"); add(yes); add(no);
add(maybe); yes.addActionListener(this); no.addActionListener(this);
maybe.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str =
ae.getActionCommand(); if(str.equals("Yes")) { msg = "You pressed Yes."; } else
if(str.equals("No")) { msg = "You pressed No."; } else { msg = "You pressed Undecided."; }
repaint(); } public void paint(Graphics g) { g.drawString(msg, 6, 100); } }
= no=new Button(&quot;NO&quot;)

168After inserting which statement in following program, output window will be closed when close
button is clicked import java.awt.event.*; import java.awt.*; class Demo extends Frame {
Demo() { setSize(500,500); setVisible(true); setTitle("My Window"); } public static void
main(String args[]) { new Demo(); } } class AdapterDemo extends WindowAdapter { Demo d1;
AdapterDemo(Demo d) { d1=d; } public void windowClosing(WindowEvent we) { d1.dispose(); }
}
= addWindowListener(AdapterDemo(this));

169 An event adapter can be implemented using ___________. 2 N U 2 All of Above

170 An event is generated when internal stae of event source is________ = changed
Analyse the following code and Find out errors. import java.awt.*; import java.applet.*; import
java.awt.event.*; /*<applet code="Demo" width=200 height=80> </applet>*/ public class
Demo extends Applet implements ItemListener { public void init() { Label jlb1=new Label("

171 Select Color:"); Choice ch=new Choice(); ch.addItem("Red"); ch.addItem("Green");
ch.addItem("Blue"); TextField jtf1=new TextField(12); add(jlb1); add(ch); add(jtf1);
ch.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { String s=
(String)ie.getItem(); jtf1.setText(s); } }
= Error is in the statement String s=(String)ie.getItem();

172Analyse the following code and find out missing statement. import javax.swing.*; import
javax.swing.tree.*; public class TreeDemo extends JFrame { public static void main(String args[])
{ TreeDemo frame = new TreeDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new
DefaultMutableTreeNode("Root"); DefaultMutableTreeNode mercury = new
DefaultMutableTreeNode("Mercury"); root.add(mercury); DefaultMutableTreeNode venus = new
DefaultMutableTreeNode("Venus"); root.add(venus); DefaultMutableTreeNode mars = new
DefaultMutableTreeNode("Mars"); root.add(mars); JTree tree = new JTree(root); JScrollPane
scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150); frame.setVisible(true); } }
= missing package statement

173Analyse the following code and select missing statement. import java.awt.*; import
java.applet.*; /*<applet code="Demo" width=200 height=80> </applet>*/ public class Demo
extends Applet implements ItemListener { TextField jtf; List city; 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=city.getSelectedItem(); jtf.setText(s); } }
= All of the above

174Analyze the following code import javax.swing.*; import javax.swing.border.*; import
java.awt.*; public class BorderTest extends JFrame{ public BorderTest(){ Border border=new
TitledBorder("MyButton"); JButton b1=new JButton("Ok"); JButton b2=new JButton("Cancel");
b1.setBorder(border); b2.setBorder(border); add(b1,BorderLayout.NORTH);
add(b2,BorderLayout.SOUTH); } public static void main(String args[]){ JFrame f=new
BorderTest(); f.setSize(200,100); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); } } What will be the output of the above code?
= Two buttons displayed with the same border
.
175Analyze the following code: import javax.swing.*; import java.awt.*; public class Test extends
JFrame { public Test() { setLayout(new FlowLayout()); add(new JButton("Java")); add(new
JButton("Java")); add(new JButton("Java")); add(new JButton("Java")); } public static void
main(String[] args) { // Create a frame and set its properties JFrame frame = new Test();
frame.setSize(200, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); } }
2 N U 2 Four buttons are displayed with the same text &quot;Java&quot;.

176ButtonGroup bg = new ButtonGroup(); is used to create group of
_________________________
= RadioButton

177Choose correct fig. as ouput for given code import java.awt.*; import java.applet.*; import
java.awt.event.*; public class addition2 extends Applet implements ActionListener { TextField t1;
TextField t2; TextField t3; Button b; {Label num1=new Label ("Addition of two numbers"); Label
num2=new Label("Enter First no in text field"); Label num3=new Label("Enter Second no in text
field"); t1=new TextField(); t2=new TextField(); t3=new TextField(); b=new Button("add");
setLayout(new GridLayout(4,2)); add(num1); add(t1); add(num2); add(t2); add(num3); add(t3);
add(b); b.addActionListener(this); } public void actionPerformed(ActionEvent ae)
{if(ae.getSource()==b) { int n1=Integer.parseInt(t2.getText()); int
n2=Integer.parseInt(t3.getText()); int sum=n1+n2; t1.setText(Integer.toString(sum)); } } } /*
<applet code="addition1" height=200 width=300> </applet>*/
2 Y2 A 2

178Choose the correct missing statement from the given code import java.awt.*; import
java.applet.*; import java.awt.event.*; public class eventdemo2 extends Applet implements
ActionListener { TextField t1,t2; Button b1; public void init() { t1=new TextField(5); t2=new
TextField(5); b1=new Button("FACTORIAL"); add(t1); add(t2); add(b1); } public void
actionPerformed(ActionEvent a) { if(a.getSource()==b1) { int fact=1; int
n1=Integer.parseInt(t1.getText()); while(n1!=0) { fact=fact*(n1); n1=n1-1; }
t2.setText(Integer.toString(fact)); } } } /* <applet code=eventdemo2.class width=250
height=200> </applet> */
= b1.addActionListener(this);
Choose the correct option for the given code import java.awt.*; import java.awt.event.*; import
java.applet.*; public class DemoE extends Applet { public void init() { Button b=new The program will display &quot;Button clicked &quot; message in status
179 Button("Click"); b.addMouseListener(new MouseAdapter() { public void
mouseClicked(MouseEvent me) { showStatus("Button clicked"); } }); add(b); } }
= bar on clicking the button with mouse.

180 Choose the correct output for the given code /*<applet code="MyControl10" width=300
height=300> </applet>*/ import java.applet.*; import java.awt.*; import java.awt.event.*;
public class MyControl10 extends Applet implements ItemListener { CheckboxGroup g; Checkbox
c1,c2,c3; int red,green,blue; public void init() { g=new CheckboxGroup(); c1=new
Checkbox("Red",g,false); c2=new Checkbox("Green",g,false); c3=new Checkbox("Blue",g,true);
setLayout(null); c1.setBounds(20,20,70,20); c2.setBounds(20,45,70,20);
c3.setBounds(20,70,70,20); add(c1); add(c2); add(c3); c1.addItemListener(this);
c2.addItemListener(this); c3.addItemListener(this); red=blue=green=0; } public void
itemStateChanged(ItemEvent ie) { red=green=blue=0; if(c1.getState()) red=255;
if(c2.getState()) green=255; if(c3.getState()) blue=255; repaint(); } public void paint(Graphics g)
{ Color c=new Color(red,green,blue); g.setColor(c); g.fillRect(90,20,100,100); } }
2 Y2 A 2 All of the above

Saturday, May 25, 2019

Chapter-2 (MCQ:1-100)



Chapter:2
Event Handling
Applets are event-driven programs. Thus, event handling is at the core of
successful applet programming. Most events to which our applet will respond
are generated by the user. These events are passed to our applet in a variety ofways, with the specific method depending upon the actual event. There are
several types of events. The most commonly handled events are those
generated by the mouse, the keyboard, and various controls, such as a push
button. Events are supported by the java.awt.event package.


1 ----------------------Is super class of all the events = EventObject

2. Which of these packages contains all the classes and methods required for even handling in
Java?= java.awt.event

3 2. Which of these events is generated when the a window is closed? = WindowEvent

4 3 ________method are used to register a keyboard event listener = addKeyListener()

5 3. Which of these methods can be used to know the degree of adjustment made by the user? = getValue()

6 A MenuItem object can generate __________ events. = ActionEvent

7 A source generates an event and sends it to ___________ listeners that can handle the event=1 One or more

8 A ________________ is an object that is notified when an event occurs=Listener

9A ________________________ is generated when a component is added to or removed from a
container.= ContainerEvent

10 ActionEvent Class is used for Which Controll ?= Button , List ,MenuItem

11 Adapter class belongs to the package = java.awt.event

12 Adapter Class provides_____________________________ =Empty implementation of all methods of a listener

13 An event is generated when the internal state of the event source is ___________.= Changed

14 An object that would like to be notified of and respond to an event is = EventListener

15At the root of the Java event class hierarchy is ___________which is the superclass for all
events.= EventObject

16 Change in the state of an object is known as __________ = Event

17 CheckBox implements following Listener Interface = ItemListener

18 ComponenetEvent is the superclass of _______________. = All of the above

19 Event class is defined in which of these libraries? = java.awt.event

20 Event Listeners are____________________________________ = Interfaces

21 EventObject class present in__________package. = java.util

22 EventObject contains two important methods:________ and___________ = getSource() and toString()

23 Events are supported by the _____________ = java.awt.event

24 Focus events are fired whenever a component _____________________the focus = gains or loses

25 FocusEvent is subclass of which of these calsses ? = ComponenEvent

26Generated when a window is activated, de-activated, closed, de-activated,de-iconified, iconified,
opened or quit= WindowEvent

27 getID() method is provided by __________ class. = AWTEvent

28 getKeyChar( ) and getkeyCode( ) methods belongs to which event class? = KeyEvent

29 getSource() is method of which class = EventObject

30 How many types of component events are in Java? = 4

31 How to obtain the command name for invoking ActionEvent? = Using getActionCommand( ) method.

32 How to remove the event listener? = Using removeTypeListener( ) method.

33 If a class extends the ActionListener interface, it must contain a method called. __________. = actionPerformed()

34 If scroll bar is manipulated………….. event will be notified. = AdjustmentEvent

35 If we close an applet’s window _________ event will be generated. = WindowEvent

36 If _____________is manipulated AdjustmentEvent event will be notified . = ScrollBar

37 In EventObject, which method is used to determine the type of event? = getSource()

38 In java an event is an _________ which specifies the change of state in the source. = Object

39 In Java, events are all the activities that occur between = A and B

40 In which package class AWTEvent defined = java.awt package;

41 In which package the methods for receiving and processing events are defined = java.awt.event

42 In which places can put the event handling code = All mentioned above

43 Interface used to handle menu events is = ActionListner

44 ItemListener event defines this method… = itemStateChanged()

45 KeyEvent is a subclass of ________ = InputEvent

46 KeyListener interface has got _______________methods = 3

47 mouseDragged() method present in which listener = MouseMotionListener

48 MouseEvent is subclass of which of these classes? = InputEvent

49 Name the event that gets generated when a button is clicked. = ActionEvent

50Name the method defined in EventObject class that returns the Object generated from the
event.select the one correct answer.= getSource()

51 On which awt component event listener can not be aplied = Label

52 public void mouseMoved(MouseEvent me) is method of _______________ = MouseMotionListener

53 Scrollbar control generates ……………………………. Event. = AdjustmentEvent

54 Select the proper constructor of EventObject class = EventObject(Object src)

55 Some of the event listener interfaces are_____________ = All of these

56 Source object can register only one listener = False

57 Text changed ( ) method is used for______. = when there is changes in text occurs

58 TextEvent defines the following integer constatnt: = TEXT_VALUE_CHANGED

59 TextField generates which events = ActionEvent,TextEvent

60 The constructor which the Text Event class defines. = TextEvent(Object source, int event_type)

61 The default layout of the contentPane of a JApplet is __________. = BorderLayout

 62 The delegation Event model is based on the concept of = Both A and B

63 The delegation model is used as a standard for = Event Listeneing

64 The Following are event classes = Action Event,FocusEvent ,Container Event

65 The Following are method of mouse event class = getClickCount ( )

66 The following method is an abstract method of TextListener interface. = textChanged(TextEvent obj)

67 The following method must be overriden in order to handle KeyEvent. = All of the mentioned

68 The method in the ActionEvent ________ returns the action command of the button = getActionCommand()

69 The method that is used for registering keyboard event is knows as ____________________. = addKeyListener ()

70 The MouseEvent class does not defines the _____________integer constant. = MOUSE_WHEELMOVE

71 The MouseListener interface is used to make mouse handling = True


72 The MouseListener's _______________ method is called after mouse button is released = public void mouseClicked(MouseEvent e)

73 The MouseMotionListener Interface has __________________ method. = void mouseDragged(MouseEvent me)

74 The signature for the registration method for an ActionEvent should be = public void addActionListener(ActionListener l)

75 The superclass of ContainerEvent, FocusEvent and WindowEvent is _______________ . = ComponentEvent

76 The ________ interface is used to handle button events: = ActionListener r

78 The __________________interface handles choice events. = ItemListener

79 Till now two models have been introduced in java for: = Receiving and pro
cessing events

80 To write event driven programs using AWT or Swings, Use? = Option A and B

81 What is a listener in context to event handling? = A listener is a object that is notified when an event occurs.

82 What is a listener in the context to event handling? = A listener is an object that notified when event had occurred.

83 What is an event in Delegation Event model ? = An event is an object that describes a state change in a source.

84 What is event handling? = Controling Event

85 What is use of MouseMotion Listener Interface? = mouseDragged()

86 What kind of event is fired when the user selects an item from a menu? = an ActionEvent

87 When a component obtains keyboard focus, which method is invoked? = void focusGained(FocusEvent fe)

88 When component is added or a removed from container,…………… generated? = ContainerEvent

89 When key is pressed which event is occurred = Both A and B


90 When list item is double clicked, which event is generated? = ActionEvent

91 When the size of component is changed ,----------------------- event is generated = ComponentEvent

92When two or more objects are added as listeners for the same event, which listener is first
invoked to handle the event?
= There is no way to determine which listener will be invoked first.
93 when we need to use check boxes or item from the list or use a checkable menu,an
_________________is generated.
= ItemEvent
94 Which of the following are true =The event - inheritance model is more efficient than the event -
delegation model.

95 Which among the following is not an AWT Event = AdjustEvent

96 Which among the following is true for Adaptor Classes? = Adaptor classes reduce Complexity of event Listener.

97 Which are various methods of WindowListener interface from following? = All above

98 Which are WindowEvent class defines integer constants ? = All The Above

99 Which class is at the root in Java event class hierarchy? = EventObject

java program (Tree)