Multiple choice general knowledge science & technology

What will be the result of compiling and running the given program? Select one correct answer. 1 public class exception 2 { 3 public static void main(String args[]) 4 { 5 System.out.println("A"); 6 try 7 { 8 System.out.println("B"); 9 System.exit(0); 9 } 10 catch(Exception e) 11 { 12 System.out.println("C"); 13 } 14 finally 15 { 16 System.out.println("D"); 17 } 18 } 19 }

  1. Program compiles correctly and prints "A" and "C" when executed

  2. Program compiles correctly and prints "A" when executed

  3. Program compiles correctly and prints "A","B" and "C" when executed

  4. Program compiles correctly and prints "A" and "B" when executed

  5. compile-time error

  6. run-time error

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

When System.exit(0) is called, the JVM terminates immediately without executing the finally block. The program prints A (before try), then B (inside try), then exits. The catch block is not executed because no exception is thrown, and finally is not reached because System.exit() terminates the program.