OCA Java SE 8 Mock Exam - OCA Mock Question 8








Question

What is the output of the following code snippet?

public class Main{
  public static void main(String[] argv){
     System.out.print("a"); 
     try { 
       System.out.print("b"); 
       throw new IllegalArgumentException(); 
     } catch (RuntimeException e) { 
       System.out.print("c"); 
     } finally { 
       System.out.print("d"); 
     } 
     System.out.print("e"); 
  }
}
  1. abe
  2. abce
  3. abde
  4. abcde
  5. The code does not compile.
  6. An uncaught exception is thrown.




Answer



D.

Note

The first System.out.print statement is executed and prints out a.

Then it enters the try statement and prints out b.

After that an IllegalArgumentException is thrown out and execution point starts at catch statement.

The catch statement catches the RuntimeException which prints out c them it falls the finally statement and print out d.

After the try catch finally statement. e is printed out.