Home Subjects Java Programming Exception Handling

Java Programming
Exception Handling

Java OOP, collections, multithreading

51 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 51
Topics in Java Programming
Q.11 Medium Exception Handling
Consider the following exception hierarchy. Which statement will compile without error?
class MyException extends Exception {}
class MyRuntimeException extends RuntimeException {}
A throw new MyException();
B int x = 10; x = x / 0; // No need to handle MyException
C Method m() throws MyRuntimeException { return null; }
D new MyException(); // Valid statement without try-catch
Correct Answer:  C. Method m() throws MyRuntimeException { return null; }
EXPLANATION

RuntimeException and its subclasses are unchecked, so they don't need to be caught or declared. Option A requires try-catch since MyException is checked.

Test
Q.12 Medium Exception Handling
What will be the output?
try {
throw new Exception("Test");
} catch(Exception e) {
System.out.println("Caught");
throw new RuntimeException("New");
} finally {
System.out.println("Finally");
}
A Caught Finally followed by RuntimeException
B Caught RuntimeException Finally
C Finally Caught
D RuntimeException Caught Finally
Correct Answer:  A. Caught Finally followed by RuntimeException
EXPLANATION

The finally block executes after catch block. 'Caught' is printed, then finally block prints 'Finally', and then RuntimeException is thrown.

Test
Q.13 Medium Exception Handling
In multi-catch block (Java 7+), which operator is used to catch multiple exceptions in a single catch clause?
A &&
B ||
C |
D &
Correct Answer:  C. |
EXPLANATION

Java 7 introduced multi-catch using the pipe (|) operator: catch(IOException | SQLException e). This allows handling multiple exception types in one block.

Test
Q.14 Medium Exception Handling
Which interface must a class implement to work with try-with-resources?
A Closeable
B AutoCloseable
C Serializable
D Both A and B
Correct Answer:  D. Both A and B
EXPLANATION

AutoCloseable (Java 7+) is the primary interface for try-with-resources. Closeable extends AutoCloseable. Both work with try-with-resources.

Test
Q.15 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.

Test
Q.16 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.

Test
Q.17 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.

Test
Q.18 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.

Test
Q.19 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'.

Test
Q.20 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.

Test
IGET
IGET AI
Online · Exam prep assistant
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