Multiple choice

What will be the output of the program?

public class X {

 public static void main(String[] args) {
  try {
   badMethod(); /* Line 5 */
   System.out.print("A");
  } catch (Exception ex) // Line 7
  {
   System.out.print("B"); // Line 9 
  } finally // Line 10 
  {
   System.out.print("C"); // Line 12
  }

  System.out.print("D"); // Line 15
 }
 public static void badMethod() {
  throw new RuntimeException();
 }
}

  1. AB

  2. BC

  3. ABC

  4. BCD

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

(1) A RuntimeException is thrown, this is a subclass of exception. (2) The exception causes the try to complete abruptly (line 5) therefore line 6 is never executed. (3) The exception is caught (line 7) and "B" is output (line 9) (4) The finally block (line 10) is always executed and "C" is output (line 12). (5) The exception was caught, so the program continues with line 15 and outputs "D".