Multiple choice

What will be the output of the program?

public class X {

 public static void main(String[] args) {
  try {
   badMethod();

   System.out.print(A);
  } catch (Exception ex) {
   System.out.print(B);

  } finally {
   System.out.print(C);
  }
  System.out.print(D);
 }

 public static void badMethod() {
  throw new Error(); /* Line 18 */
 }
}

  1. ABCD.

  2. Compilation fails.

  3. C is printed before exiting with an error message.

  4. BC is printed before exiting with an error message.

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

Error is thrown but not recognised in line(18) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore, only the code in the finally statement can be run before exiting with a runtime error (Exception in thread main java.lang.Error).