250+ TOP MCQs on Finally & Built in Exceptions and Answers

Java MCQs on keyword finally and built in exceptions of Java Programming Language.

1. Which of these clause will be executed even if no exceptions are found?
a) throws
b) finally
c) throw
d) catch

Answer: b
Clarification: finally keyword is used to define a set of instructions that will be executed irrespective of the exception found or not.

2. A single try block must be followed by which of these?
a) finally
b) catch
c) finally & catch
d) none of the mentioned

Answer: c
Clarification: try block can be followed by any of finally or catch block, try block checks for exceptions and work is performed by finally and catch block as per the exception.

3. Which of these exceptions handles the divide by zero error?
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException

Answer: a
Clarification: None.

4. Which of these exceptions will occur if we try to access the index of an array beyond its length?
a) ArithmeticException
b) ArrayException
c) ArrayIndexException
d) ArrayIndexOutOfBoundsException

Answer: d
Clarification: ArrayIndexOutOfBoundsException is a built in exception that is caused when we try to access an index location which is beyond the length of an array.

5. 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 a = args.length;
  8.                 int b = 10 / a;
  9.                 System.out.print(a);
  10.             }
  11.             catch (ArithmeticException e) 
  12.             {
  13.                     System.out.println("1");
  14.             }
  15.         }
  16.     }

Note : Execution command line : $ java exception_handling
a) 0
b) 1
c) Compilation Error
d) Runtime Error

Answer: b
Clarification: None.
Output:

$ javac exception_handling.java
$ java exception_handling
1

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.                 throw new NullPointerException ("Hello");
  8.             }
  9.             catch(ArithmeticException e)
  10.             {
  11.         	System.out.print("B");        	
  12.             }
  13.         }
  14.     }

a) A
b) B
c) Compilation Error
d) Runtime Error

Answer: d
Clarification: Try block is throwing NullPointerException but the catch block is used to counter Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it, runtime error occurs.
Output:

$ javac exception_handling.java
$ java exception_handling
Exception in thread "main" java.lang.NullPointerException: Hello

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 a = 1;
  8.                     int b = 10 / a;
  9.                     try 
  10.                     {
  11.                          if (a == 1)
  12.                              a = a / a - a;
  13.                          if (a == 2) 
  14.                          {
  15.                              int c[] = {1};
  16.                              c[8] = 9;
  17.                          }
  18.                     }
  19.                     finally 
  20.                     {
  21.                         System.out.print("A");
  22.                     }
  23.                 }
  24.                 catch (Exception e) 
  25.                 {
  26.                         System.out.println("B");
  27.                 }
  28.             }
  29.         }

a) A
b) B
c) AB
d) BA

Answer: a
Clarification: The inner try block does not have a catch which can tackle ArrayIndexOutOfBoundException hence finally is executed which prints ‘A’ the outer try block does have catch for ArrayIndexOutOfBoundException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.
Output:

$ javac exception_handling.java
$ java exception_handling
A

8. 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 a = args.length;
  8.                 int b = 10 / a;
  9.                 System.out.print(a);
  10.                 try 
  11.                 {
  12.                      if (a == 1)
  13.                          a = a / a - a;
  14.                      if (a == 2) 
  15.                      {
  16.                          int []c = {1};
  17.                          c[8] = 9;
  18.                      }
  19.                 }
  20.                 catch (ArrayIndexOutOfBoundException e) 
  21.                 {
  22.                     System.out.println("TypeA");
  23.                 }
  24.             catch (ArithmeticException e) 
  25.             {
  26.                     System.out.println("TypeB");
  27.             }
  28.         }
  29.     }

Note: Execution command line: $ java exception_handling one two
a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error

Answer: c
Clarification: try without catch or finally
Output:

$ javac exception_handling.java
$ java exception_handling
Main.java:9: error: 'try' without 'catch', 'finally' or resource declarations

Leave a Reply

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