Java Programming — Exception Handling
Java OOP, collections, multithreading
19 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 19 questions in Exception Handling
Which scenario would cause a StackOverflowError in Java exception handling?
A Catching too many different exception types
B A method recursively catching and rethrowing the same exception infinitely
C Having more than 10 nested try-catch blocks
D Using 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.

Take Test
In Java 7+, exception handling introduced 'multi-catch' feature using the pipe (|) operator. What's a limitation of multi-catch blocks?
A Cannot have multiple exception types
B Exception types must be unrelated (not in same inheritance hierarchy)
C Can only catch maximum of 2 exception types
D Multi-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.

Take Test
If a finally block contains a return statement, what happens to an exception thrown in the try block?
A The exception is suppressed and the finally's return value is used
B The exception is propagated after finally executes
C The exception replaces the finally return value
D A 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.

Take Test
In exception chaining, what is the primary benefit?
A It reduces code size
B It preserves the original exception context while throwing a new exception
C It eliminates the need for catch blocks
D It 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.

Take Test
Which of the following custom exceptions would be inappropriate to extend from RuntimeException in a banking application?
A InvalidAccountException
B InsufficientFundsException
C NegativeAmountException
D DatabaseConnectionException
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.

Take Test
Advertisement
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);
A 20
B 30
C 10
D Compilation 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.

Take Test
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)
A I, II, III
B III, II, I
C II, I, III
D I, 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.

Take Test
Which exception is the parent class of all checked exceptions in Java (excluding RuntimeException)?
A Throwable
B Exception
C Error
D Checked
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.

Take Test
What happens when System.exit() is called inside try block?
A finally block still executes
B finally block does not execute
C catch block executes first
D Compilation error occurs
Correct Answer:  B. finally block does not execute
EXPLANATION

System.exit() terminates the JVM immediately. The finally block does not execute as the program terminates before reaching it.

Take Test
Which of the following scenarios will NOT trigger a StackOverflowError?
A Infinite recursive function calls without base case
B Very deep try-catch nesting (theoretically)
C Mutual recursion between multiple methods
D Creating 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.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips