CHAPTER 6 Review Answers
CHAPTER 6 Review Answers
12. A, B, D, G. The main() method invokes run on a new Mouse object. Line 4 prints 1 and line 6 prints 2, so
options A and B are correct. Line 7 throws a NullPointerException, which causes line 8 to be skipped, so C is
incorrect. The exception is caught on line 9 and line 10 prints 4, so option D is correct. Line 11 throws the
exception again, which causes run() to immediately end, so line 13 doesn’t execute and option E is incorrect.
The main() method doesn’t catch the exception either, so line 18 doesn’t execute and option F is incorrect. The
uncaught NullPointerException causes the stack trace to be printed, so option G is correct.
13. A, B, C, E. Classes listed in the throws part of a method declaration must extend java.lang.Throwable. This
includes Error, Exception, and RuntimeException. Arbitrary classes such as String can’t go there. Any Java
type, including Exception, can be declared as the return type. However, this will simply return the object rather
than throw an exception.
14. A, C, D, E. A method that declares an exception isn’t required to throw one, making option A correct.
Runtime exceptions can be thrown in any method, making options C and E correct. Option D matches the
exception type declared and so is also correct. Option B is incorrect because a broader exception is not allowed.
15. A, B, D, E. ArrayIndexOutOfBoundsException, IllegalArgumentException, and NumberFormatException
are runtime exceptions. Sorry, you have to memorize them. Any class that extends RuntimeException is a
runtime (unchecked) exception. Classes that extend Exception but not RuntimeException are checked
exceptions.
16. B. IllegalArgumentException is used when an unexpected parameter is passed into a method. Option A is
incorrect because returning null or -1 is a common return value for this scenario. Option D is incorrect because a
for loop is typically used for this scenario. Option E is incorrect because you should find out how to code the
method and not leave it for the unsuspecting programmer who calls your method. Option C is incorrect because
you should run!
17. A, C, D, E. The method is allowed to throw no exceptions at all, making option A correct. It is also allowed
to throw runtime exceptions, making options D and E correct. Option C is also correct since it matches the
signature in the interface.
18. A, B, C, E. Checked exceptions are required to be handled or declared. Runtime exceptions are allowed to
be handled or declared. Errors are allowed to be handled or declared, but this is bad practice.
19. C, E. Option C is allowed because it is a more specific type than RuntimeException. Option E is allowed
because it isn’t in the same inheritance tree as RuntimeException. It’s not a good idea to catch either of these.
Option B is not allowed because the method called inside the try block doesn’t declare an IOException to be
thrown. The compiler realizes that IOException would be an unreachable catch block. Option D is not allowed
because the same exception can’t be specified in two different catch blocks. Finally, option A is not allowed
because it’s more general than RuntimeException and would make that block unreachable.
20. A, E. The code begins normally and prints a on line 13, followed by b on line 15. On line 16, it throws an
exception that’s caught on line 17. Remember, only the most specific matching catch is run. Line 18 prints c,
and then line 19 throws another exception. Regardless, the finally block runs, printing e. Since the finally block
also throws an exception, that’s the one printed.