OCA Java SE 8 Exception - OCA Mock Question Exception 6








Question

What is the output of the following program?

     1: public class Main { 
     2:   public void start() { 
     3:    try { 
     4:      System.out.print("AAA"); 
     5:      throw new Exception(); 
     6:    } catch (Exception e) { 
     7:       System.out.print("BBB"); 
     8:       System.exit(0); 
     9:    } finally { 
     10:      System.out.print("CCC"); 
     11:   }  
     12:  } 
     13:  public static void main(String[] args) { 
     14:    new Main().start(); 
     15:  } 
     16:} 
  1. AAA
  2. AAABBB
  3. AAABBBCCC
  4. AAACCC
  5. The code does not compile.
  6. An uncaught exception is thrown.




Answer



B.

Note

Line 4 prints AAA and line 5 throws an Exception.

Line 6 catches the exception, line 7 prints BBB and line 8 calls System.exit, which terminates the JVM.

The finally block does not execute.

public class Main {
  public void start() {
    try {/*from   w  ww .j  a v  a  2s . co m*/
      System.out.print("AAA");
      throw new Exception();
    } catch (Exception e) {
      System.out.print("BBB");
      System.exit(0);
    } finally {
      System.out.print("CCC");
    }
  }

  public static void main(String[] args) {
    new Main().start();
  }
}