In Java, when an exception is thrown in a try block and caught in a catch block, if the catch block also throws an exception, what happens to the original exception?
AThe original exception is lost and only the new exception propagates
BBoth exceptions are chained together automatically
CThe original exception is stored in the cause field if exception chaining is used
DThe JVM throws a CompoundException containing both
Correct Answer:
C. The original exception is stored in the cause field if exception chaining is used
EXPLANATION
When exception chaining is explicitly used with initCause() or constructor parameters, the original exception becomes the cause of the new exception. Without explicit chaining, the original exception is lost. Exception chaining is not automatic in Java.
Which scenario would cause a StackOverflowError in Java exception handling?
ACatching too many different exception types
BA method recursively catching and rethrowing the same exception infinitely
CHaving more than 10 nested try-catch blocks
DUsing throw keyword more than once in a method
Correct Answer:
B. A method recursively catching and rethrowing the same exception infinitely
EXPLANATION
StackOverflowError occurs when stack memory exhausts due to deep recursion. If a method catches an exception and immediately rethrows it without modification, calling itself, stack frames accumulate until overflow. This is a runtime error, not a checked exception.
What does the getSuppressed() method of Throwable class return in context of try-with-resources?
AThe primary exception thrown in try block
BArray of exceptions suppressed while closing resources
CThe exception thrown in finally block
DException from outermost try-catch block
Correct Answer:
B. Array of exceptions suppressed while closing resources
EXPLANATION
When try-with-resources encounters an exception while closing a resource, it's added to the suppressed exceptions list. If an exception was already thrown in try, the close exception is suppressed rather than replacing it. getSuppressed() returns array of these suppressed exceptions.
In Java 7+, exception handling introduced 'multi-catch' feature using the pipe (|) operator. What's a limitation of multi-catch blocks?
ACannot have multiple exception types
BException types must be unrelated (not in same inheritance hierarchy)
CCan only catch maximum of 2 exception types
DMulti-catch syntax is deprecated in Java 17+
Correct Answer:
B. Exception types must be unrelated (not in same inheritance hierarchy)
EXPLANATION
In multi-catch blocks like 'catch(IOException | SQLException e)', the exception types must not have an inheritance relationship. You cannot do 'catch(Exception | IOException e)' because IOException is a subclass of Exception.
DBoth are identical; throws is just an older syntax
Correct Answer:
B. throw explicitly raises an exception, throws declares that a method may throw exceptions
EXPLANATION
'throw' is a statement that explicitly throws an exception object. 'throws' is a clause in method signature indicating the method may throw specific checked exceptions. Example: throw new IOException(); vs public void method() throws IOException {}
Consider a scenario where you have nested try-catch blocks. If both inner and outer catch blocks match the thrown exception type, which one executes?
ABoth execute in sequence
BOnly the inner catch block executes
COnly the outer catch block executes
DThe JVM decides based on exception priority
Correct Answer:
B. Only the inner catch block executes
EXPLANATION
The innermost matching catch block executes first. If it doesn't rethrow the exception, the outer catch block won't execute. The exception is considered handled after the first matching catch block.
If a finally block contains a return statement, what happens to an exception thrown in the try block?
AThe exception is suppressed and the finally's return value is used
BThe exception is propagated after finally executes
CThe exception replaces the finally return value
DA compilation error occurs
Correct Answer:
A. The exception is suppressed and the finally's return value is used
EXPLANATION
If finally has a return statement, it suppresses any exception from try/catch blocks. The return value from finally overrides exception propagation. This is generally considered bad practice as it masks exceptions.
What is the output of the following code?
try {
int x = 10/0;
} catch(ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
System.out.println("After");
ACaught\nFinally\nAfter
BCaught\nFinally
CFinally\nAfter
DArithmeticException is thrown, program terminates
Correct Answer:
A. Caught\nFinally\nAfter
EXPLANATION
When ArithmeticException occurs, it's caught by the catch block printing 'Caught'. The finally block always executes, printing 'Finally'. Then normal program flow continues, printing 'After'.
Which of the following exceptions would NOT be caught by catching Exception class in Java?
AIOException
BNullPointerException
CError
DArithmeticException
Correct Answer:
C. Error
EXPLANATION
Error is not a subclass of Exception. Both Error and Exception extend Throwable. Errors like OutOfMemoryError, StackOverflowError cannot be caught by Exception handlers. They must be caught separately if needed.
In Java 8+, when using try-with-resources with multiple AutoCloseable resources, in what order are they closed?
AIn the order they were declared (first declared, first closed)
BIn reverse order of declaration (last declared, first closed)
CIn random order determined by JVM
DSimultaneously in parallel threads
Correct Answer:
B. In reverse order of declaration (last declared, first closed)
EXPLANATION
Try-with-resources closes resources in LIFO (Last In, First Out) order. If you declare Resource1, then Resource2, Resource2 closes first, then Resource1. This ensures dependencies are respected.