Java Programming — Exception Handling
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 11–20 of 100 questions in Exception Handling
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
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
Advertisement
Q.16 Medium Exception Handling
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());
}
A Caught: Inner
B Caught: Outer
C Compilation error
D No output
Correct Answer:  B. Caught: Outer
EXPLANATION

The inner try catches Exception and throws RuntimeException. The outer catch catches RuntimeException and prints 'Caught: Outer'.

Take Test
Q.17 Medium Exception Handling
Which of the following is true about try-with-resources (Java 7+)?
A It automatically closes resources that implement Closeable or AutoCloseable
B It eliminates the need for finally blocks completely
C It can only be used with file streams
D It requires explicit close() method calls
Correct Answer:  A. It automatically closes resources that implement Closeable or AutoCloseable
EXPLANATION

Try-with-resources automatically invokes close() on resources implementing AutoCloseable/Closeable. Syntax: try(Resource r = new Resource()) {...}

Take Test
Q.18 Medium Exception Handling
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");
}
A Invalid number
B General exception
C Compilation error
D No 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.

Take Test
Q.19 Medium Exception Handling
In Java, which method of Throwable class is used to obtain the cause of an exception?
A getReason()
B getCause()
C getException()
D getError()
Correct Answer:  B. getCause()
EXPLANATION

The getCause() method returns the cause (underlying Throwable) of this Throwable. It's useful for exception chaining.

Take Test
Q.20 Medium Exception Handling
Which of the following will NOT result in a NullPointerException?
A String s = null; System.out.println(s.length());
B Object obj = null; obj.toString();
C Integer i = null; int x = i;
D String s = null; if(s == null) { System.out.println("Null"); }
Correct Answer:  D. String s = null; if(s == null) { System.out.println("Null"); }
EXPLANATION

Comparing with null using == operator doesn't throw NPE. The other options call methods or unbox null values, which causes NullPointerException.

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