Java Reference
In-Depth Information
18. E. The code does not compile, so A is incorrect. The readFromFile method compiles fi ne
because it properly declares the IOException that might be thrown on lines 5 and 7, so B,
C, and D are incorrect. The main method invokes readFromFile and needs to handle or
declare the IOException. Because it does neither, line 14 generates a compiler error and the
answer is E.
19. C. The main method invokes go and A is printed on line 3. The stop method is invoked
and E is printed on line 15. Line 17 throws a NullPointerException, so stop is imme-
diately popped off the method call stack and line 18 does not execute. The exception is
not caught in go, so the go method is popped off the call stack, but not before its finally
block executes and C is printed on line 9. Because main does not catch the exception, the
stack trace displays and no further output occurs, so “AEC” was the output printed before
the stack trace. Therefore, the answer is C.
20. E. The order of catch blocks is important because they are checked in the order
they appear after the try block. Because ArithmeticException is a child class of
RuntimeException, the catch block on line 7 is unreachable. (If an ArithmeticException
is thrown in the try block, it will be caught on line 5.) Line 7 generates a compiler error
because it is unreachable code, so the answer is E.
21. B. The code compiles fi ne, so E is incorrect. The while loop iterates through the String
one character at a time and increments count if the character is a vowel. Because the given
word has 16 vowels, the output is 16 and the answer is B.
22. B. The code compiles fi ne, so E is incorrect. The main method invokes start on a new
Laptop object. Line 4 prints Starting up, then line 5 throws an Exception. Line 6 catch-
es the exception, line 7 prints Problem, and then line 8 calls System.exit, which termi-
nates the JVM. The finally block does not execute because the JVM is no longer running,
so the answer is B.
23. C. The parseName method is invoked within main on a new Dog object. Line 5 prints 1.
The try block executes and 2 is printed. Line 8 throws a NumberFormatException, so
line 9 does not execute. The exception is caught on line 10 and line 11 prints 4. Because
the exception is handled, execution resumes normally. parseName runs to completion and
line 19 executes, printing 5. That is the end of the program, so the output is 1245 and the
answer is C.
24. D. The parseName method is invoked on a new Cat object. Line 5 prints 1. The try block
is entered and line 7 prints 2. Line 8 throws a NumberFormatException. It is not caught, so
parseName is popped off the method call stack. main does not catch the exception either,
so the program terminates and the stack trace for the NumberFormatException is printed.
Therefore, the answer is D.
25. E. Believe it or not, the code compiles fi ne, so F is incorrect. The best way to explain the
answer is by reformatting the code so the lines are indented properly, as follows:
3. int x = 10;
4. if(x < 0)
5. System.out.print(“anywhere”);
Search WWH ::




Custom Search