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.
What will be the output of this code?
try {
try {
throw new Exception("Inner");
} catch(Exception e) {
throw new RuntimeException("Outer");
}
} catch(RuntimeException e) {
System.out.println("Caught: " + e.getMessage());
}
ACaught: Inner
BCaught: Outer
CCompilation error
DNo output
Correct Answer:
B. Caught: Outer
EXPLANATION
The inner try catches Exception and throws RuntimeException. The outer catch catches RuntimeException and prints 'Caught: Outer'.
What will be printed when this code executes?
String str = "Java";
try {
int x = Integer.parseInt(str);
} catch(NumberFormatException e) {
System.out.println("Invalid number");
} catch(Exception e) {
System.out.println("General exception");
}
AInvalid number
BGeneral exception
CCompilation error
DNo output
Correct Answer:
A. Invalid number
EXPLANATION
Integer.parseInt("Java") throws NumberFormatException, which is caught by the first catch block. More specific exceptions should be caught before general ones.