Java Reference
In-Depth Information
6. else if(x < 5)
7. if(x == 10)
8. System.out.print(“here”);
9. else if(x >= 5)
10. System.out.print(“there”);
11. else
12. System.out.print(“somewhere”);
13. else
14. System.out.print(“nowhere”);
The x < 0 comparison on line 4 is false , as is x < 5 on line 6. The else on line 13
matches up with the if on line 5, so line 14 executes and nowhere is printed. Therefore,
the answer is E.
26. E. A finally block can only appear at the end of a try statement. Therefore, this code
does not compile and the answer is E.
27. A, B, D, and G. The main method invokes run on a new Mouse object. Line 5 prints 1 and
line 7 prints 2, so A and B are correct. Line 8 throws a NullPointerException which
causes line 9 to be skipped, so C is incorrect. The exception is caught on line 10 and line 11
prints 4, so D is correct. Line 12 throws the exception again, which causes run to immedi-
ately get popped off the method call stack, so line 14 does not execute and E is incorrect.
The main method does not catch the exception either, so line 20 does not execute and F is
incorrect. The uncaught NullPointerException causes the stack trace to be printed, so G
is correct. Therefore, the answers are A, B, D, and G.
Search WWH ::




Custom Search