Java OCA OCP Practice Question 1353

Question

What is the output of the following application?

package mypkg; //w  ww . j a  va  2  s.c o  m
public class Main { 
   public static void main(String officials[]) { 
      try { 
         System.out.print('A'); 
         throw new RuntimeException("Out of bounds!"); 
      } catch (ArrayIndexOutOfBoundsException aioobe) { 
         System.out.print('B'); 
         throw t; 
      } finally { 
         System.out.print('C'); 
      } 
   } 
} 
  • A. ABC
  • B. ABC, followed by a stack trace for a RuntimeException
  • C. AC, followed by a stack trace for a RuntimeException
  • D. None of the above


C.

Note

The application first enters the try block and outputs A.

It then throws a RuntimeException, but the exception is not caught by the catch block since RuntimeException is not a subclass of ArrayIndexOutOfBoundsException.

Next, the finally block is called and C is output.

Finally, the RuntimeException is thrown by the main() method and a stack trace is printed.

Option C is correct.




PreviousNext

Related