Java OCA OCP Practice Question 370

Question

Consider the following class hierarchy and code fragment:

 1. try { //from   w ww.  jav a  2s.  c  om
 2.     // assume s is previously defined 
 3.   URL u = new URL(s); 
 4.     // in is an ObjectInputStream 
 5.   Object o = in.readObject(); 
 6.   System.out.println("Success"); 
 7. } 
 8. catch (MalformedURLException e) { 
 9.   System.out.println("Bad URL"); 
10. } 
11. catch (StreamCorruptedException e) { 
12.   System.out.println("Bad file contents"); 
13. } 
14. catch (Exception e) { 
15.   System.out.println("General exception"); 
16. } 
17. finally { 
18.   System.out.println("Doing finally part"); 
19. } 
20. System.out.println("Carrying on"); 

What lines are output if the method at line 5 throws an OutOfMemoryError? (Choose all that apply.)

  • A. Success
  • B. Bad URL
  • C. Bad file contents
  • D. General exception
  • E. Doing finally part
  • F. Carrying on


E.

Note

The thrown error prevents completion of the try block, so the message Success from line 6 is not printed.

No catch is appropriate, so B, C, and D are incorrect.

Control then passes to the finally block, which results in the message at line 18 being output.

E is part of the correct answer.

Because the error was not caught, execution exits the method and the error is rethrown in the caller of this method.

F is not part of the correct answer.




PreviousNext

Related