Java Questions & Answers – Finally & Built in Exceptions

This section of our 1000+ Java MCQs focuses 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

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
View Answer

Answer: d
Explanation: 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?

Note: Join free Sanfoundry classes at Telegram or Youtube
  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
View Answer

Answer: b
Explanation: None.
Output:

advertisement
$ javac exception_handling.java
$ java exception_handling
1

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

advertisement
  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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

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

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

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.