PASS - TAKER : Anonymos

Java SE 8 OCP - Assertions and Java 7 Exceptions

Correct : 10

90% Complete (success)

90 %

False : 1

9% Complete (success)

9 %



Anonymos 2019-05-24T23:05:26

Correct

1. Which are true? (Choose all that apply.)

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.

Not Correct

2. Given: {code}
Which are true? (Choose all that apply.)

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.

Correct

3. Given: {code}
What is the result?

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.

Correct

4. Given: {code}
Which inserted independently at // insert code here will compile and produce the output: b? (Choose all that apply.)

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.

Correct

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.)

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.

Correct

6. Given: {code}
What is the result?

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.

Correct

7. Given: {code}
What is the result?

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.

Correct

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.)

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.

Correct

9. Given: {code}
Which exceptions will the code throw?

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.

Correct

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

Correct

11. Given: {code}
What will this code print?

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.