Java OCA OCP Practice Question 228

Question

Given:

public class Main {
  static String s = "-";
  public static void main(String[] args) {
    try {/*www. ja  v a2s .  c  o m*/
      throw new Exception();
    } catch (Exception e) {
        try {
          try { throw new Exception();
          } catch (Exception ex) { s += "ic "; }
          throw new Exception(); }
        catch (Exception x) { s += "mc "; }
        finally { s += "mf "; }
    } finally { s += "of "; }
    System.out.println(s);
} }

What is the result?

  • A. -ic of
  • B. -mf of
  • C. -mc mf
  • D. -ic mf of
  • E. -ic mc mf of
  • F. -ic mc of mf
  • G. Compilation fails


E is correct.

Note

There is no problem nesting try/catch blocks.

When an exception is thrown, the code in the catch block runs, and then the code in the finally block runs.




PreviousNext

Related