OCP Exception Handling and Assertions 3 — Questions and Answers
Question 1: When using try-with-resources, in what order are resources closed?
- In the order they were opened
- In reverse order of how they were opened (Correct answer)
- Simultaneously in parallel
- Undefined order
Correct answer: In reverse order of how they were opened
Resources in a try-with-resources statement are closed in the reverse order of their declaration.
Question 2: What interface must a class implement to be used in a try-with-resources statement?
- Closeable
- AutoCloseable
- Either Closeable or AutoCloseable (Correct answer)
- Serializable
Correct answer: Either Closeable or AutoCloseable
A class can be used in try-with-resources if it implements either AutoCloseable or its subinterface Closeable.
Question 3: What are 'suppressed exceptions' in Java?
- Exceptions thrown in catch blocks that are ignored
- Exceptions thrown during resource closing that are added to the primary exception (Correct answer)
- Exceptions from finally blocks that replace the original
- Exceptions silenced by the compiler
Correct answer: Exceptions thrown during resource closing that are added to the primary exception
When a resource's close() throws an exception while another exception is propagating, the close() exception is suppressed and attached to the primary exception.
Question 4: Which statement about `assert` with a detail message is correct?
- assert condition, message; — message only evaluated if assertion fails (Correct answer)
- assert condition, message; — message always evaluated
- assert(condition, message); — correct syntax with parentheses
- assert condition : message; — message is evaluated before the condition
Correct answer: assert condition, message; — message only evaluated if assertion fails
In `assert condition : message;`, the message expression is only evaluated when the assertion fails, making it lazy.
Question 5: What happens if both a catch block and a finally block throw exceptions?
- Both exceptions are propagated
- The catch block's exception is propagated
- The finally block's exception replaces the catch exception (Correct answer)
- A new combined exception is created
Correct answer: The finally block's exception replaces the catch exception
If the finally block throws an exception, it replaces any exception that was being propagated from the catch block.
Question 6: Which of the following is NOT a valid use of assertions according to Java best practices?
- Checking preconditions in private methods
- Validating postconditions
- Checking invariants
- Validating public method arguments from external callers (Correct answer)
Correct answer: Validating public method arguments from external callers
Assertions should not be used to validate public method arguments because assertions can be disabled, leaving public APIs unprotected.
Question 7: What is the result of calling `getCause()` on an exception that wraps another?
- The outermost exception
- The original underlying exception (Correct answer)
- null always
- The exception message string
Correct answer: The original underlying exception
getCause() returns the Throwable that caused this exception to be thrown, enabling exception chaining.
When using try-with-resources, in what order are resources closed?