Java Programming — Exception Handling
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 31–40 of 100 questions in Exception Handling
Q.31 Medium Exception Handling
What is the correct way to create a custom exception with cause chaining?
A public MyException(String msg) throws Throwable {}
B public MyException(String msg, Throwable cause) { super(msg, cause); }
C public MyException(Throwable cause) { this.cause = cause; }
D Custom exceptions cannot support cause chaining
Correct Answer:  B. public MyException(String msg, Throwable cause) { super(msg, cause); }
EXPLANATION

Proper cause chaining calls super(msg, cause) in constructor, leveraging Exception's constructor that accepts both message and cause.

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
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");
}
}
}
A Caught IOException
B Caught RuntimeException
C Compilation error
D No output
Correct Answer:  C. Compilation error
EXPLANATION

IOException is a checked exception and must be declared in method signature or caught. The inner catch throws IOException which isn't caught immediately.

Take Test
Q.34 Medium Exception Handling
Consider code that implements auto-closeable resources. What is the advantage of try-with-resources statement?
A Automatic resource closure in order of declaration (LIFO)
B No need to call close() explicitly
C Suppressed exceptions are automatically added
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

Try-with-resources (Java 7+) automatically closes resources, handles suppressed exceptions, and closes in LIFO order without explicit close() calls.

Take Test
Which of the following is NOT a subclass of Throwable?
A Exception
B Error
C RuntimeException
D Throwable itself
Correct Answer:  D. Throwable itself
EXPLANATION

Throwable is the superclass. Exception and Error are direct subclasses, and RuntimeException is a subclass of Exception.

Take Test
Advertisement
Which method is used to print the stack trace of an exception?
A printStackTrace()
B getStackTrace()
C printStack()
D Both A and B are correct
Correct Answer:  D. Both A and B are correct
EXPLANATION

printStackTrace() prints to System.err, while getStackTrace() returns an array of StackTraceElement objects. Both exist and serve different purposes.

Take Test
Q.37 Medium Exception Handling
What will be the result of executing this code?
public class MultiCatchTest {
public static void main(String[] args) {
try {
int[] arr = {1, 2};
int x = arr[5];
} catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
System.out.println("Array or Null Error");
}
}
}
A Array or Null Error
B Compilation error - cannot use | in catch
C ArrayIndexOutOfBoundsException thrown
D No output - exception not caught
Correct Answer:  A. Array or Null Error
EXPLANATION

Multi-catch feature (Java 7+) allows handling multiple exceptions. ArrayIndexOutOfBoundsException is caught by the multi-catch block.

Take Test
Q.38 Medium Exception Handling
Which of the following best describes a custom exception in Java?
A Must extend RuntimeException directly
B Can extend Exception or RuntimeException based on whether it's checked or unchecked
C Must implement Serializable interface
D Cannot have any fields or methods
Correct Answer:  B. Can extend Exception or RuntimeException based on whether it's checked or unchecked
EXPLANATION

Custom exceptions should extend Exception (for checked) or RuntimeException (for unchecked) depending on usage requirements.

Take Test
Q.39 Medium Exception Handling
What is the output of multi-level exception chaining?
public class ChainTest {
public static void main(String[] args) {
try {
try {
throw new IOException("IO Error");
} catch (IOException e) {
throw new RuntimeException("Runtime Error", e);
}
} catch (RuntimeException e) {
System.out.println(e.getCause().getClass().getSimpleName());
}
}
}
A RuntimeException
B IOException
C Throwable
D Exception
Correct Answer:  B. IOException
EXPLANATION

getCause() returns the IOException that was wrapped. getClass().getSimpleName() returns 'IOException'.

Take Test
Q.40 Medium Exception Handling
Which of the following statements is true regarding exception constructors?
A Exception class has only no-argument constructor
B Exception class has constructor that accepts String message and Throwable cause
C Custom exceptions must override toString() method
D Exception constructors cannot call super() implicitly
Correct Answer:  B. Exception class has constructor that accepts String message and Throwable cause
EXPLANATION

Exception class provides multiple constructors including one with String message and another with String message and Throwable cause for exception chaining.

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