Java OCA OCP Practice Question 277

Question

Given:

public class Main {
   static class MyException1 extends Exception {
   }/* ww  w  . java2s .c  om*/

   static class MyException2 extends Exception {
   }

   public static void main(String[] args) {
      try {
         new Main().listen();
         System.out.println("a");
      } catch (MyException1 | MyException2 e) {
         e = new MyException1();
         System.out.println("b");
      } finally {
         System.out.println("c");
      }
   }

   public void listen() throws MyException1, MyException2 {
   }
}

What will this code print?

  • A. a
  • B. ab
  • C. ac
  • D. abc
  • E. bc
  • F. Compilation fails


F is correct.

Note

The exception variable in a catch block may not be reassigned when using multi-catch.

It CAN be reassigned if we are only catching one exception.

C would have been correct if e = new MyException1(); were removed. A, B, D, and E are incorrect because of the above.




PreviousNext

Related