Java OCA OCP Practice Question 317

Question

Consider the following class hierarchy and code fragment:

 1. try { //from   www .  j ava  2 s.  c o m
 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 constructor at line 3 throws a MalformedURLException? (Choose all that apply.)

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


B, E, F.

Note

The exception causes a jump out of the try block, so the message Success from line 6 is not printed.

The first applicable catch is at line 8, which is an exact match for the thrown exception.

This results in the message at line 9 being printed, so B is one of the required answers.

Only one catch block is ever executed, so control passes to the finally block, which results in the message at line 18 being output; so E is part of the correct answer.

Execution continues after the finally block.

This results in the output of the message at line 20, so F is also part of the correct answer.




PreviousNext

Related