Java OCA OCP Practice Question 243

Question

Given:

3. public class Main {
4.   public static void main(String[] args) {
5.     // insert code here
6.
7.   }
8.   void go() {
9.     go();
10.   }
11. }

And given the following three code fragments:

I.   new Main().go();

II.  try { new Main().go(); }
     catch (Error e) { System.out.println("ouch"); }

III. try { new Main().go(); }
     catch (Exception e) { System.out.println("ouch"); }

When fragments I-III are added, independently, at line 5, which are true?

Choose all that apply.

  • A. Some will not compile
  • B. They will all compile
  • C. All will complete normally
  • D. None will complete normally
  • E. Only one will complete normally
  • F. Two of them will complete normally


B and E are correct.

Note

go() is a recursive method which is guaranteed to cause a StackOverflowError.

Since Exception is not a superclass of Error, catching an Exception will not help handle an Error, so fragment III will not complete normally.

Only fragment II will catch the Error.




PreviousNext

Related