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
Which of the following correctly describes the difference between Error and Exception in Java?
Answer: Error represents serious JVM problems not meant to be caught; Exception represents recoverable conditions
Error represents serious problems like OutOfMemoryError that applications typically cannot recover from, while Exception represents conditions applications should handle.
In a multi-catch block `catch (IOException | SQLException e)`, what is the type of `e`?
Answer: The common supertype of both exceptions
In a multi-catch, the variable type is the most specific common supertype of all the listed exception types.
What happens when an assert statement's boolean expression is `false` and assertions are enabled?
Answer: It throws AssertionError
When an assertion fails (expression is false) and assertions are enabled, the JVM throws an AssertionError.
Which statement about try-with-resources and explicit finally blocks is correct in Java 9+?
Answer: finally blocks can coexist with try-with-resources and run after resources are closed
A try-with-resources can have an explicit finally block, which runs after all resources have been automatically closed.
What is printed by: `int x = 0; try { x = 1; return x; } finally { x = 2; System.out.print(x); }`?
Answer: 2
The finally block executes before the return completes, so x=2 is assigned and printed as '2', though the method returns 1.
Which of the following is true about declaring assertions in class static initializers?
Answer: Assertions in static initializers use system assertions flags (-esa)
Assertions in static initializers of system classes are controlled by the -esa (enable system assertions) flag, separate from -ea.
What is the result when a variable declared in try-with-resources is referenced after the try block?
Answer: Compilation error — scope is limited to the try block
Variables declared in the resource specification of try-with-resources are scoped to the try block and cannot be accessed outside it.
How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. Exam facts are checked against the official sources listed below. Not reviewed by a subject-matter expert. Editorial policy
Sources & references
Exam facts on this page (fees, format, dates, passing scores) are checked against these official sources; the date is our last check. Editorial policy
- oracle.com — Test dates, score release, retake policy · verified Oct 2026
- mylearn.oracle.com — Test delivery, exam format, passing score, time limit · verified Oct 2026