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.
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.
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.
In exception chaining, what is the primary benefit?
AIt reduces code size
BIt preserves the original exception context while throwing a new exception
CIt eliminates the need for catch blocks
DIt automatically fixes errors
Correct Answer:
B. It preserves the original exception context while throwing a new exception
EXPLANATION
Exception chaining preserves the stack trace and cause information: throw new RuntimeException("msg", originalException). This helps in debugging by maintaining the complete exception history.
Which of the following custom exceptions would be inappropriate to extend from RuntimeException in a banking application?
AInvalidAccountException
BInsufficientFundsException
CNegativeAmountException
DDatabaseConnectionException
Correct Answer:
D. DatabaseConnectionException
EXPLANATION
DatabaseConnectionException should be checked (extends Exception) as it's a serious, recoverable error requiring explicit handling. Business logic exceptions can be unchecked.
What will this code output?
int result = 0;
try {
result = 10 / 0;
} catch(Exception e) {
result = 20;
return result;
} finally {
result = 30;
}
System.out.println(result);
A20
B30
C10
DCompilation error
Correct Answer:
A. 20
EXPLANATION
Return statement in catch block prepares the return value (20). Finally block executes but return happens after finally, so 20 is returned even though finally sets result to 30.
Consider a scenario where multiple catch blocks are written. What is the correct order?
I. catch(FileNotFoundException e)
II. catch(IOException e)
III. catch(Exception e)
AI, II, III
BIII, II, I
CII, I, III
DI, III, II
Correct Answer:
A. I, II, III
EXPLANATION
Catch blocks should be ordered from most specific to most general exception types. FileNotFoundException is more specific than IOException, which is more specific than Exception.
Which exception is the parent class of all checked exceptions in Java (excluding RuntimeException)?
AThrowable
BException
CError
DChecked
Correct Answer:
B. Exception
EXPLANATION
Exception class is the parent of all checked exceptions. RuntimeException extends Exception but is unchecked. Error and Throwable are higher in hierarchy.
Which of the following scenarios will NOT trigger a StackOverflowError?
AInfinite recursive function calls without base case
BVery deep try-catch nesting (theoretically)
CMutual recursion between multiple methods
DCreating too many local variables in a method
Correct Answer:
D. Creating too many local variables in a method
EXPLANATION
StackOverflowError occurs from stack overflow due to excessive method calls. Local variables contribute to stack but won't cause overflow like recursion.