โ† All OCP Flashcard Decks

Exception Handling and Assertions Flashcards

7 cards from real OCP practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 7 Exception Handling and Assertions flashcards as text
  1. What happens when an exception is thrown inside a try block and no matching catch block exists in the current method?

    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.

  2. Which of the following is true about the `finally` block in Java?

    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.

  3. What is the output of: `try { throw new RuntimeException(); } catch (Exception e) { System.out.print("A"); } finally { System.out.print("B"); }`?

    Answer: AB

    The catch block prints 'A', then the finally block always executes and prints 'B', resulting in 'AB'.

  4. Which exception class must be the parent when creating a custom checked exception?

    Answer: Exception (but not RuntimeException)

    A custom checked exception must extend Exception or one of its subclasses that is not RuntimeException or Error.

  5. What does the `assert` statement do when assertions are disabled?

    Answer: Is completely skipped by the JVM

    When assertions are disabled (the default), the JVM completely skips the assert statement without evaluating its expression.

  6. Which of the following correctly uses a multi-catch block (Java 7+)?

    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.

  7. What is the effect of catching a checked exception that the try block cannot throw?

    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.