250+ TOP MCQs on Exceptions Types and Answers

Java MCQs on Exceptions types in Java Programming Language.

1. Which of these is a super class of all exceptional type classes?
a) String
b) RuntimeExceptions
c) Throwable
d) Cacheable

Answer: c
Clarification: 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: b
Clarification: 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: a
Clarification: 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: a
Clarification: None.

5. What exception thrown by parseInt() method?
a) ArithmeticException
b) ClassNotFoundException
c) NullPointerException
d) NumberFormatException

Answer: d
Clarification: parseInt() method parses input into integer. The exception thrown by this method is NumberFormatException.

6. What will be the output of the following Java code?

  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.                 System.out.print("Hello" + " " + 1 / 0);
  8.             }
  9.             finally 
  10.             {
  11.         	System.out.print("World");        	
  12.             }
  13.         }
  14.     }

a) Hello
b) World
c) Compilation Error
d) First Exception then World

Answer: d
Clarification: None.
Output:

$ javac exception_handling.java
$ java exception_handling
Exception in thread "main" java.lang.ArithmeticException: / by zero
World

7. What will be the output of the following Java code?

  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.                 int i, sum;
  8.                 sum = 10;
  9.                 for (i = -1; i < 3 ;++i) 
  10.                 {
  11.                     sum = (sum / i);
  12.                 System.out.print(i);
  13.                 }
  14.             }
  15.             catch(ArithmeticException e) 
  16.             {     	
  17.                 System.out.print("0");
  18.             }
  19.         }
  20.     }

a) -1
b) 0
c) -10
d) -101

Answer: c
Clarification: 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

contest

Leave a Reply

Your email address will not be published. Required fields are marked *