OCP Exception Handling and Assertions 2 — Questions and Answers
Question 1: What happens when an exception is thrown inside a try block and no matching catch block exists in the current method?
- The exception is silently ignored
- The JVM terminates immediately
- The exception propagates up the call stack (Correct answer)
- The finally block is skipped
Correct answer: The exception propagates up the call stack
When no matching catch block is found, the exception propagates to the calling method's exception handler.
Question 2: Which of the following is true about the `finally` block in Java?
- It only executes when an exception is thrown
- It executes even if a return statement is in the try block (Correct answer)
- It executes only if no exception is thrown
- It prevents exceptions from propagating
Correct answer: It executes even if a return statement is in the try block
The finally block always executes after try/catch completion, even if a return statement is encountered in the try block.
Question 3: What is the output of: `try { throw new RuntimeException(); } catch (Exception e) { System.out.print("A"); } finally { System.out.print("B"); }`?
- A
- B
- AB (Correct answer)
- BA
Correct answer: AB
The catch block prints 'A', then the finally block always executes and prints 'B', resulting in 'AB'.
Question 4: Which exception class must be the parent when creating a custom checked exception?
- RuntimeException
- Error
- Exception (but not RuntimeException) (Correct answer)
- Throwable directly
Correct answer: Exception (but not RuntimeException)
A custom checked exception must extend Exception or one of its subclasses that is not RuntimeException or Error.
Question 5: What does the `assert` statement do when assertions are disabled?
- Throws AssertionError
- Evaluates the expression but ignores the result
- Is completely skipped by the JVM (Correct answer)
- Throws IllegalStateException
Correct answer: Is completely skipped by the JVM
When assertions are disabled (the default), the JVM completely skips the assert statement without evaluating its expression.
Question 6: Which of the following correctly uses a multi-catch block (Java 7+)?
- catch (IOException | SQLException e) (Correct answer)
- catch (IOException, SQLException e)
- catch (IOException && SQLException e)
- catch (IOException e | SQLException e)
Correct answer: catch (IOException | SQLException e)
The multi-catch syntax uses a pipe `|` to separate exception types, with a single variable name after the last type.
Question 7: What is the effect of catching a checked exception that the try block cannot throw?
- It is silently ignored at runtime
- It causes a compile-time error (Correct answer)
- It causes a runtime warning
- It is allowed as defensive programming
Correct answer: It causes a compile-time error
Catching a checked exception that cannot be thrown by the try block results in a compile-time error in Java.
What happens when an exception is thrown inside a try block and no matching catch block exists in the current method?