Java Programming — Exception Handling
Java OOP, collections, multithreading
51 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 51 questions in Exception Handling
In Java, when an exception is thrown in a try block and caught in a catch block, if the catch block also throws an exception, what happens to the original exception?
A The original exception is lost and only the new exception propagates
B Both exceptions are chained together automatically
C The original exception is stored in the cause field if exception chaining is used
D The JVM throws a CompoundException containing both
Correct Answer:  C. The original exception is stored in the cause field if exception chaining is used
EXPLANATION

When exception chaining is explicitly used with initCause() or constructor parameters, the original exception becomes the cause of the new exception. Without explicit chaining, the original exception is lost. Exception chaining is not automatic in Java.

Take Test
What does the getSuppressed() method of Throwable class return in context of try-with-resources?
A The primary exception thrown in try block
B Array of exceptions suppressed while closing resources
C The exception thrown in finally block
D Exception from outermost try-catch block
Correct Answer:  B. Array of exceptions suppressed while closing resources
EXPLANATION

When try-with-resources encounters an exception while closing a resource, it's added to the suppressed exceptions list. If an exception was already thrown in try, the close exception is suppressed rather than replacing it. getSuppressed() returns array of these suppressed exceptions.

Take Test
Consider a scenario where you have nested try-catch blocks. If both inner and outer catch blocks match the thrown exception type, which one executes?
A Both execute in sequence
B Only the inner catch block executes
C Only the outer catch block executes
D The JVM decides based on exception priority
Correct Answer:  B. Only the inner catch block executes
EXPLANATION

The innermost matching catch block executes first. If it doesn't rethrow the exception, the outer catch block won't execute. The exception is considered handled after the first matching catch block.

Take Test
Which of the following exceptions would NOT be caught by catching Exception class in Java?
A IOException
B NullPointerException
C Error
D ArithmeticException
Correct Answer:  C. Error
EXPLANATION

Error is not a subclass of Exception. Both Error and Exception extend Throwable. Errors like OutOfMemoryError, StackOverflowError cannot be caught by Exception handlers. They must be caught separately if needed.

Take Test
In Java 8+, when using try-with-resources with multiple AutoCloseable resources, in what order are they closed?
A In the order they were declared (first declared, first closed)
B In reverse order of declaration (last declared, first closed)
C In random order determined by JVM
D Simultaneously in parallel threads
Correct Answer:  B. In reverse order of declaration (last declared, first closed)
EXPLANATION

Try-with-resources closes resources in LIFO (Last In, First Out) order. If you declare Resource1, then Resource2, Resource2 closes first, then Resource1. This ensures dependencies are respected.

Take Test
Advertisement
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
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
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
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.10 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