1. Which are true? (Choose all that apply.)
A. It is appropriate to use assertions to validate arguments to methods marked public
B. It is appropriate to catch and handle assertion errors
C. It is NOT appropriate to use assertions to validate command-line arguments
D. It is appropriate to use assertions to generate alerts when you reach code that should not be reachable
E. It is NOT appropriate for assertions to change a program's state
A is incorrect. It is acceptable to use assertions to test the arguments of private methods. B is incorrect. While assertion errors can be caught, Oracle discourages you from doing so.
2. Given: {code} Which are true? (Choose all that apply.)
A. Compilation succeeds
B. Compilation fails due to an error on line 6
C. Compilation fails due to an error on line 7
D. Compilation fails due to an error on line 8
E. Compilation fails due to an error on line 9
F. Compilation fails due to an error on line 10
When an assert statement has two expressions, the second expression must return a value. The only two-expression assert statement that doesn't return a value is on line 9.
3. Given: {code} What is the result?
A. ad
B. acd
C. cd
D. d
E. Compilation fails
F. An exception is thrown at runtime
catch (IOException e | SQLException e) doesn't compile. While multiple exception types can be specified in the multi-catch, only one variable name is allowed. The correct syntax is catch (IOException | SQLException e). Other than this, the code is valid. Note that it is legal for blowUp() to have IOException in its signature even though that Exception can't be thrown.
4. Given: {code} Which inserted independently at // insert code here will compile and produce the output: b? (Choose all that apply.)
A. catch(Exception e) {
B. catch(FileNotFoundException e) {
C. catch(IOException e) {
D. catch(IOException | SQLException e) {
E. catch(IOException e | SQLException e) {
F. catch(SQLException e) {
G. catch(SQLException | IOException e) {
H. catch(SQLException e | IOException e) {
Since order doesn't matter, both D and G show correct use of the multi-catch block. And C catches the IOException from fileBlowUp() directly. Note that databaseBlowUp() is never called at runtime. However, if you remove the call, the compiler won't let you catch the SQLException since it would be impossible to be thrown.
5. Given: {code} Which inserted independently at // insert code here will compile and produce the output error driving before throwing an exception? (Choose all that apply.)
A. catch(AnotherTrainComing e) {
B. catch(AnotherTrainComing | RanOutOfTrack e) {
C. catch(AnotherTrainComing e | RanOutOfTrack e) {
D. catch(Exception e) {
E. catch(IllegalArgumentException e) {
F. catch(RanOutOfTrack e) {
G. None of the above—code fails to compile for another reason
B uses multi-catch to identify both exceptions drive() may throw. D still compiles since it uses the new enhanced exception typing to recognize that Exception may only refer to AnotherTrainComing and RanOutOfTrack. F is the simple case that catches a single exception. Since main throws AnotherTrainComing, the catch block doesn't need to handle it.
6. Given: {code} What is the result?
A. -t123t
B. -t12c3
C. -t123
D. -t1c3
E. -t1c23
F. None of the above; main() throws an exception
G. Compilation fails
After the exception is thrown, Automatic Resource Management calls close() before completing the try block. From that point, catch and finally execute in the normal order. F is incorrect because the catch block catches the exception and does not rethrow it.
7. Given: {code} What is the result?
A. 2glf
B. 2lgf
C. tglf
D. t2lgf
E. t2lgf
System.out.println cannot be in the declaration clause of a try-with-resources block because it does not declare a variable. If the println was removed, the answer would be A because resources are closed in the opposite order they are created.
8. Given: {code} And the following possible changes: C1. Replace line 2 with class Lamb implements AutoCloseable { C2. Replace line 2 with class Lamb implements Closeable { C3. Replace line 11 with } finally {} What change(s) allow the code to compile? (Choose all that apply.)
A. Just C1 is sufficient
B. Just C2 is sufficient
C. Just C3 is sufficient
D. Both C1 and C3
E. Both C2 and C3
F. The code compiles without any changes
If the code is left with no changes, it will not compile because try-with-resources requires Lamb to implement AutoCloseable or a subinterface. If C2 is implemented, the code will not compile because close() throws Exception instead of IOException. Unlike the traditional try, try-with-resources does not require catch or finally to present. So the code works equally well with or without C3.
9. Given: {code} Which exceptions will the code throw?
A. IOException with suppressed RuntimeException a
B. IOException with suppressed RuntimeException c
C. RuntimeException a with no suppressed exception
D. RuntimeException c with no suppressed exception
E. RuntimeException a with suppressed RuntimeException c
F. RuntimeException c with suppressed RuntimeException a
While the exception caught by the catch block matches choice A, it is ignored by the catch block. The catch block just throws RuntimeException c without any suppressed exceptions.
10. Given: {code} Which exceptions will the code throw?
After the try block throws an IOException, Automatic Resource Management calls close() to clean up the resources. Since an exception was already thrown in the try block, RuntimeException a gets added to it as a suppressed exception. The catch block merely rethrows the caught exception. The code does compile even though the catch block catches an Exception
11. Given: {code} What will this code print?
A. a
B. ab
C. ac
D. abc
E. bc
F. Compilation fails
The exception variable in a catch block may not be reassigned when using multi-catch. It CAN be reassigned if we are only catching one exception. C would have been correct if e = new PowerOutage(); were removed.