Java OCA OCP Practice Question 220

Question

Given:

class Main {/*  w w w . jav a2s .c o m*/
  static String s = "-";
  public static void main(String[] args) {
    new Main().method1();
    System.out.println(s);
  }
  void method1() {
    try { method2(); }
    catch (Exception e) { s += "c"; }
  }
  void method2() throws Exception  {
    method3();  s += "2";
    method3();  s += "2b";
  }
  void method3() throws Exception {
    throw new Exception();
  }
 }

What is the result?

  • A. -
  • B. -c
  • C. -c2
  • D. -2c
  • E. -c22b
  • F. -2c2b
  • G. -2c2bc
  • H. Compilation fails


B is correct.

Note

Once method3() throws the exception to method2(), method2() throws it to method1(), and no more of method2()'s code will be executed.




PreviousNext

Related