Analyze the code:
try {
return 5;
} finally {
return 10;
}
What will be returned?
Answer: B
The finally block always executes, and if it contains a return statement, it overrides the return value from try or catch block. So 10 is returned.
Q.2Hard
What happens if an exception is thrown in the finally block?
Answer: C
If an exception occurs in the finally block, it will replace any exception thrown in the try or catch block, and the new exception will be propagated.
Q.3Hard
Analyze the nested try-catch:
try {
try {
int x = 05;
} catch(NullPointerException e) {
System.out.println("Inner");
}
} catch(ArithmeticException e) {
System.out.println("Outer");
}
Answer: B
ArithmeticException is thrown by 05. Inner catch only catches NullPointerException, so it propagates to outer catch which handles ArithmeticException. Output: Outer
Q.4Hard
What will be printed?
public class ExceptionTest {
public static void main(String[] args) {
try {
testMethod();
} catch (Exception e) {
System.out.println("Caught");
}
}
public static void testMethod() {
try {
throw new RuntimeException("Error");
} finally {
System.out.println("Finally");
}
}
}
Answer: A
Finally block executes first (prints 'Finally'), then the exception propagates to outer catch block which prints 'Caught'.
Q.5Hard
Which of the following is NOT a characteristic of Exception class in Java?
Answer: B
Exception class has both checked subclasses (like IOException) and unchecked subclasses (like RuntimeException). Not all exceptions extending Exception are checked.
Advertisement
Q.6Hard
Which of the following cannot throw a checked exception without being declared in throws clause?
Answer: D
Checked exceptions in constructors, instance initializers, and static initializers must be handled with try-catch or declared in throws (constructors only). This is enforced by Java compiler.
Q.7Hard
What is the execution order in try-catch-finally for nested exception handling?
Answer: B
Nested exception handling follows inner-to-outer order. Inner try-catch-finally completes fully, then control passes to outer blocks. Finally blocks execute in their respective scope order.
Q.8Hard
What is suppressed exception in Java (introduced in Java 7)?
Answer: B
In try-with-resources, if an exception occurs in try and another in resource close(), the second is added as suppressed exception to the first using addSuppressed(), preserving all exception information.
Q.9Hard
What will be printed for this code?
public class ExceptionOrder {
public static void main(String[] args) {
try {
try {
throw new RuntimeException("Inner");
} catch (Exception e) {
throw new IOException("Outer");
}
} catch (IOException e) {
System.out.println("Caught IOException");
} catch (RuntimeException e) {
System.out.println("Caught RuntimeException");
}
}
}
Answer: C
IOException is a checked exception and must be declared in method signature or caught. The inner catch throws IOException which isn't caught immediately.
Q.10Hard
Which of the following scenarios will NOT trigger a StackOverflowError?
Answer: D
StackOverflowError occurs from stack overflow due to excessive method calls. Local variables contribute to stack but won't cause overflow like recursion.
Q.11Hard
What happens when System.exit() is called inside try block?
Answer: B
System.exit() terminates the JVM immediately. The finally block does not execute as the program terminates before reaching it.
Q.12Hard
Which exception is the parent class of all checked exceptions in Java (excluding RuntimeException)?
Answer: B
Exception class is the parent of all checked exceptions. RuntimeException extends Exception but is unchecked. Error and Throwable are higher in hierarchy.
Q.13Hard
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)
Answer: A
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.
Q.14Hard
What will this code output?
int result = 0;
try {
result = 010;
} catch(Exception e) {
result = 20;
return result;
} finally {
result = 30;
}
System.out.println(result);
Answer: A
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.
Q.15Hard
Which of the following custom exceptions would be inappropriate to extend from RuntimeException in a banking application?
Answer: D
DatabaseConnectionException should be checked (extends Exception) as it's a serious, recoverable error requiring explicit handling. Business logic exceptions can be unchecked.
Q.16Hard
In exception chaining, what is the primary benefit?
Answer: B
Exception chaining preserves the stack trace and cause information: throw new RuntimeException("msg", originalException). This helps in debugging by maintaining the complete exception history.
Q.17Hard
If a finally block contains a return statement, what happens to an exception thrown in the try block?
Answer: A
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.
Q.18Hard
In Java 7+, exception handling introduced 'multi-catch' feature using the pipe (|) operator. What's a limitation of multi-catch blocks?
Answer: B
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.
Q.19Hard
Which scenario would cause a StackOverflowError in Java exception handling?
Answer: B
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.