Java OCA OCP Practice Question 238

Question

Given:

3. public class Main {
4.   static String s = "";
5.   public static void main(String[] args) {
6.     try {/*from w ww  . ja v  a  2s .co  m*/
7.         s += "1";
8.         throw new Exception();
9.     } catch (Exception e) { s += "2";
10.    } finally { s += "3"; doStuff(); s += "4";
11.    }
12.    System.out.println(s);
13.   }
14.   static void doStuff() { int x = 0; int y = 7/x; }
15. }

What is the result?

  • A. 12
  • B. 13
  • C. 123
  • D. 1234
  • E. Compilation fails
  • F. 123 followed by an exception
  • G. 1234 followed by an exception
  • H. An exception is thrown with no other output


H is correct.

Note

The value of String s is 123 at the time that the divide-by-zero exception is thrown.

finally() is not guaranteed to complete.

In this case finally() never completes, so the System.out.println never executes.




PreviousNext

Related