Java MCQ Set 1
1. Which of these is a super class of wrappers Double and Float?
a) Long
b) Digits
c) Float
d) Number
Answer
Answer: d [Reason:] Number is an abstract class containing subclasses Double, Float, Byte, Short, Integer and Long.
2. Which of the following methods return the value as a double?
a) doubleValue()
b) converDouble()
c) getDouble()
d) getDoubleValue()
Answer
Answer: a [Reason:] None.
3. Which of these methods can be used to check whether the given value is a number or not?
a) isNaN()
b) isNumber()
c) checkNaN()
d) checkNumber()
Answer
Answer: a [Reason:] isNaN() methods returns true if num specified is not a number, otherwise it returns false.
4. Which of these method of Double wrapper can be used to check weather a given value is infinite or not?
a) Infinite()
b) isInfinite()
c) checkInfinite()
d) None of the mentioned
Answer
Answer: b [Reason:] isInfinite() methods returns true if specified value is an infinite value otherwise it returns false.
5. Which of these exceptions is thrown by compareTo() method defined in double wrapper?
a) IOException
b) SystemException
c) CastException
d) ClassCastException
Answer
Answer: d [Reason:] compareTo() methods compare the specified object to be double, if it is not then ClassCastException is thrown.
6. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Double i = new Double(257.5);
-
boolean x = i.isNaN();
-
System.out.print(x);
-
}
-
}
a) true
b) false
c) 0
d) 1
Answer
Answer: b [Reason:] i.isNaN() method returns returns true if i is not a number and false when i is a number. Here false is returned because i is a number i:e 257.5.
Output:
$ javac Output.java
$ java Output
false
7. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Integer i = new Integer(257);
-
byte x = i.byteValue();
-
System.out.print(x);
-
}
-
}
a) 0
b) 1
c) 256
d) 257
Answer
Answer: b [Reason:] i.byteValue() method returns the value of wrapper i as a byte value. i is 257, range of byte is 256 therefore i value exceeds byte range by 1 hence 1 is returned and stored in x.
Output:
$ javac Output.java
$ java Output
1
8. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Double i = new Double(257.578);
-
int x = i.intValue();
-
System.out.print(x);
-
}
-
}
a) 0
b) 1
c) 256
d) 257
Answer
Answer: d [Reason:] i.intValue() method returns the value of wrapper i as a Integer. i is 257.578 is double number when converted to an integer data type its value is 257.
Output:
$ javac Output.java
$ java Output
257
9. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Double i = new Double(257.578123456789);
-
float x = i.floatValue();
-
System.out.print(x);
-
}
-
}
a) 0
b) 257.0
c) 257.57812
d) 257.578123456789
Answer
Answer: c [Reason:] floatValue() converts the value of wrapper i into float, since float can measure till 5 places after decimal hence 257.57812 is stored in floating point variable x.
Output:
$ javac Output.java
$ java Output
257.57812
10. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Double y = new Double(257.57812);
-
Double i = new Double(257.578123456789);
-
try
-
{
-
int x = i.compareTo(y);
-
System.out.print(x);
-
}
-
catch(ClassCastException e)
-
{
-
System.out.print("Exception");
-
}
-
}
-
}
a) 0
b) 1
c) Exception
d) None of the mentioned
Answer
Answer: b [Reason:] i.compareTo() methods two double values, if they are equal then 0 is returned and if not equal then 1 is returned, here 257.57812 and 257.578123456789 are not equal hence 1 is returned and stored in x.
Output:
$ javac Output.java
$ java Output
1
Java MCQ Set 2
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
Answer
Answer: d [Reason:] Most of the event to which an applet responds is generated by user. Hence they are in Abstract Window Kit package, java.awt.event.
2. What is an event in delegation event model used by Java programming language?
a) An event is an object that describes a state change in a source
b) An event is an object that describes a state change in processing
c) An event is an object that describes any change by the user and system
d) An event is a class used for defining object, to create events
Answer
Answer: a [Reason:] An event is an object that describes a state change in a source.
3. Which of these methods are used to register a keyboard event listener?
a) KeyListener()
b) addKistener()
c) addKeyListener()
d) eventKeyboardListener()
Answer
Answer: c [Reason:] None.
4. Which of these methods are used to register a mouse motion listener?
a) addMouse()
b) addMouseListener()
c) addMouseMotionListner()
d) eventMouseMotionListener()
Answer
Answer: c [Reason:] None.
5. What is a listener in context to event handling?
a) A listener is a variable that is notified when an event occurs
b) A listener is a object that is notified when an event occurs
c) A listener is a method that is notified when an event occurs
d) None of the mentioned
Answer
Answer: b [Reason:] A listener is a object that is notified when an event occurs. It has two major requirements first, it must have been registered with one or more sources to receive notification about specific event types, and secondly it must implement methods to receive and process these notifications.
6. Event class is defined in which of these libraries?
a) java.io
b) java.lang
c) java.net
d) java.util
Answer
Answer: d [Reason:] None.
7. Which of these methods can be used to determine the type of event?
a) getID()
b) getSource()
c) getEvent()
d) getEventObject()
Answer
Answer: a [Reason:] getID() can be used to determine the type of an event.
8. Which of these class is super class of all the events?
a) EventObject
b) EventClass
c) ActionEvent
d) ItemEvent
Answer
Answer: a [Reason:] EventObject class is a super class of all the events and is defined in java.util package.
9. Which of these events will be notified if scroll bar is manipulated?
a) ActionEvent
b) ComponentEvent
c) AdjustmentEvent
d) WindowEvent
Answer
Answer: c [Reason:] AdjustmentEvent is generated when a scroll bar is manipulated.
10. Which of these events will be generated if we close an applet’s window?
a) ActionEvent
b) ComponentEvent
c) AdjustmentEvent
d) WindowEvent
Answer
Answer: d [Reason:] WindowEvent is generated when a window is activated, closed, deactivated, deiconfied, iconfied, opened or quit.
Java MCQ Set 3
1. Which of these packages contains all the event handling interfaces?
a) java.lang
b) java.awt
c) java.awt.event
d) java.event
Answer
Answer: c [Reason:] None.
2. Which of these interfaces handles the event when a component is added to a container?
a) ComponentListener
b) ContainerListener
c) FocusListener
d) InputListener
Answer
Answer: b [Reason:] The ContainerListener defines methods to recognize when a component is added to or removed from a container.
3. Which of these interfaces define a method actionPerformed()?
a) ComponentListener
b) ContainerListener
c) ActionListener
d) InputListener
Answer
Answer: c [Reason:] ActionListener defines the actionPerformed() method that is invoked when an adjustment event occurs.
4. Which of these interfaces define four methods?
a) ComponentListener
b) ContainerListener
c) ActionListener
d) InputListener
Answer
Answer: a [Reason:] ComponentListener defines four methods componentResized(), componentMoved(), componentShown() and componentHidden().
5. Which of these interfaces define a method itemStateChanged()?
a) ComponentListener
b) ContainerListener
c) ActionListener
d) ItemListener
Answer
Answer: d [Reason:] None.
6. Which of these methods will respond when you click any button by mouse?
a) mouseClicked()
b) mouseEntered()
c) mousePressed()
d) all of the mentioned
Answer
Answer: d [Reason:] when we click a button, first we enter the region of button hence mouseEntered() method responds then we press the button which leads to respond from mouseClicked() and mousePressed().
7. Which of these methods will be invoked if a character is entered?
a) keyPressed()
b) keyReleased()
c) keyTyped()
d) keyEntered()
Answer
Answer: c [Reason:] None.
8. Which of these methods is defined in MouseMotionAdapter class?
a) mouseDragged()
b) mousePressed()
c) mouseReleased()
d) mouseClicked()
Answer
Answer: a [Reason:] The MouseMotionAdapter class defines 2 methods – mouseDragged() and mouseMoved.
9. Which of these are constants defined in WindowEvent class?
a) WINDOW_ACTIVATED
b) WINDOW_CLOSED
c) WINDOW_DEICONIFIED
d) All of the mentioned
Answer
Answer: d [Reason:] WindowEvent class defines 7 constants – WINDOW_ACTIVATED, WINDOW_CLOSED, WINDOW_OPENED, WINDOW_DECONIFIED, WINDOW_CLOSING, WINDOW_DEACTIVATED, WINDOW_ICONIFIED.
10. Which of these is superclass of all Adapter classes?
a) Applet
b) ComponentEvent
c) Event
d) InputEvent
Answer
Answer: a [Reason:] All Adapter classes extend Applet class.
Java MCQ Set 4
1. Which of these is a super class of all exceptional type classes?
a) String
b) RuntimeExceptions
c) Throwable
d) Cachable
Answer
Answer: c [Reason:] All the exception types are subclasses of the built in class Throwable.
2. Which of these class is related to all the exceptions that can be caught by using catch?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
Answer
Answer: b [Reason:] Error class is related to java run time error that can’t be caught usually, RuntimeExecption is subclass of Exception class which contains all the exceptions that can be caught.
3. Which of these class is related to all the exceptions that cannot be caught?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
Answer
Answer: a [Reason:] Error class is related to java run time error that can’t be caught usually, RuntimeExecption is subclass of Exception class which contains all the exceptions that can be caught.
4. Which of these handles the exception when no catch is used?
a) Default handler
b) finally
c) throw handler
d) Java run time system
Answer
Answer: a [Reason:] None.
5. Which of these keywords is used to manually throw an exception?
a) try
b) finally
c) throw
d) catch
Answer
Answer: c [Reason:] None.
6. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
System.out.print("Hello" + " " + 1 / 0);
-
}
-
finally
-
{
-
System.out.print("World");
-
}
-
}
-
}
a) Hello
b) World
c) Compilation Error
d) First Exception then World
Answer
Answer: d [Reason:] None.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread “main” java.lang.ArithmeticException: / by zero
World
7. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a, b;
-
b = 0;
-
a = 5 / b;
-
System.out.print("A");
-
}
-
catch(ArithmeticException e)
-
{
-
System.out.print("B");
-
}
-
}
-
}
a) A
b) B
c) Compilation Error
d) Runtime Error
Answer
Answer: b [Reason:] None.
Output:
$ javac exception_handling.java
$ java exception_handling
B
8. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a[] = {1, 2,3 , 4, 5};
-
for (int i = 0; i < 7; ++i)
-
System.out.print(a[i]);
-
}
-
catch(ArrayIndexOutOfBoundsException e)
-
{
-
System.out.print("0");
-
}
-
}
-
}
a) 12345
b) 123450
c) 1234500
d) Compilation Error
Answer
Answer: b [Reason:] When array index goes out of bound then ArrayIndexOutOfBoundsException exceptio is thrown by the system.
Output:
$ javac exception_handling.java
$ java exception_handling
123450
9. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int i, sum;
-
sum = 10;
-
for (i = -1; i < 3 ;++i)
-
sum = (sum / i);
-
}
-
catch(ArithmeticException e)
-
{
-
System.out.print("0");
-
}
-
System.out.print(sum);
-
}
-
}
a) 0
b) 05
c) Compilation Error
d) Runtime Error
Answer
Answer: c [Reason:] Since sum is declared inside try block and we are trying to access it outside the try block it results in error.
Output:
$ javac exception_handling.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
sum cannot be resolved to a variable
10. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a[] = {1, 2,3 , 4, 5};
-
for (int i = 0; i < 5; ++i)
-
System.out.print(a[i]);
-
int x = 1/0;
-
}
-
catch(ArrayIndexOutOfBoundsException e)
-
{
-
System.out.print("A");
-
}
-
catch(ArithmeticException e)
-
{
-
System.out.print("B");
-
}
-
}
-
}
a) 12345
b) 12345A
c) 12345B
d) Comiplation Error
Answer
Answer: c [Reason:] There can be more than one catch for a single try block. Here Arithmetic exception(/ by 0) occurs instead of Array index out of bound exception, so 2nd catch block is executed.
Output:
$ javac exception_handling.java
$ java exception_handling
12345B
Java MCQ Set 5
1. When does Exceptions in Java arises in code sequence?
a) Run Time
b) Compilation Time
c) Can Occur Any Time
d) None of the mentioned
Answer
Answer: a [Reason:] Exceptions in java are run-time errors.
2. Which of these keywords is not a part of exception handling?
a) try
b) finally
c) thrown
d) catch
Answer
Answer: c [Reason:] Exceptional handling is managed via 5 keywords – try, catch, throws, throw and finally.
3. Which of these keywords must be used to monitor for exceptions?
a) try
b) finally
c) throw
d) catch
Answer
Answer: a [Reason:] None.
4. Which of these keywords must be used to handle the exception thrown by try block in some rational manner?
a) try
b) finally
c) throw
d) catch
Answer
Answer: d [Reason:] If an exception occurs within the try block, it is thrown and cached by catch block for processing.
5. Which of these keywords is used to manually throw an exception?
a) try
b) finally
c) throw
d) catch
Answer
Answer: c [Reason:] None.
6. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
System.out.print("Hello" + " " + 1 / 0);
-
}
-
catch(ArithmeticException e)
-
{
-
System.out.print("World");
-
}
-
}
-
}
a) Hello
b) World
c) HelloWorld
d) Hello World
Answer
Answer: b [Reason:] System.ou.print() function fist converts the whole parameters into string and then prints, before “Hello” goes to output stream 1 / 0 error is encountered which is cached by catch block printing just “World” .
Output:
$ javac exception_handling.java
$ java exception_handling
World
7. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a, b;
-
b = 0;
-
a = 5 / b;
-
System.out.print("A");
-
}
-
catch(ArithmeticException e)
-
{
-
System.out.print("B");
-
}
-
}
-
}
a) A
b) B
c) Compilation Error
d) Runtime Error
Answer
Answer: b [Reason:] None.
Output:
$ javac exception_handling.java
$ java exception_handling
B
8. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a, b;
-
b = 0;
-
a = 5 / b;
-
System.out.print("A");
-
}
-
catch(ArithmeticException e)
-
{
-
System.out.print("B");
-
}
-
finally
-
{
-
System.out.print("C");
-
}
-
}
-
}
a) A
b) B
c) AC
d) BC
Answer
Answer: d [Reason:] finally keyword is used to execute the code before try and catch block end.
Output:
$ javac exception_handling.java
$ java exception_handling
BC
9. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int i, sum;
-
sum = 10;
-
for (i = -1; i < 3 ;++i)
-
sum = (sum / i);
-
}
-
catch(ArithmeticException e)
-
{
-
System.out.print("0");
-
}
-
System.out.print(sum);
-
}
-
}
a) 0
b) 05
c) Compilation Error
d) Runtime Error
Answer
Answer: c [Reason:] Value of variable sum is printed outside of try block, sum is declared only in try block, outside try block it is undefined.
Output:
$ javac exception_handling.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
sum cannot be resolved to a variable
10. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int i, sum;
-
sum = 10;
-
for (i = -1; i < 3 ;++i)
-
{
-
sum = (sum / i);
-
System.out.print(i);
-
}
-
}
-
catch(ArithmeticException e)
-
{
-
System.out.print("0");
-
}
-
}
-
}
a) -1
b) 0
c) -10
d) -101
Answer
Answer: c [Reason:] For the 1st iteration -1 is displayed. The 2nd exception is caught in catch block and 0 is displayed.
Output:
$ javac exception_handling.java
$ java exception_handling
-10