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 exceptions is a checked exception?
Answer: FileNotFoundException
FileNotFoundException extends IOException which extends Exception (not RuntimeException), making it a checked exception.
What is the correct way to re-throw the current exception inside a catch block without losing the stack trace?
Answer: throw e;
Using `throw e;` re-throws the original exception object, preserving the original stack trace.
What does the `throws` clause in a method declaration indicate?
Answer: The method may throw that checked exception and callers must handle it
The throws clause declares that a method may propagate checked exceptions, requiring callers to catch or declare them.
Can a subclass method declare more checked exceptions in its throws clause than the overridden parent method?
Answer: No, it can only declare the same or fewer checked exceptions
An overriding method cannot throw new or broader checked exceptions than those declared by the overridden method.
What is printed by: `try { System.exit(0); } finally { System.out.print("finally"); }`?
Answer: Nothing
System.exit() terminates the JVM immediately, so the finally block never executes.
Which JVM flag enables assertions for a specific class named `com.example.MyClass`?
Answer: Both A and B are correct
Both -ea and -enableassertions are equivalent flags and can be used with a class name to enable assertions for that specific class.
What is the purpose of exception chaining introduced in Java 1.4?
Answer: To preserve the original cause when wrapping exceptions
Exception chaining allows you to wrap a low-level exception in a higher-level one while preserving the original cause via initCause() or constructor.