Which of the following statements is true regarding exception constructors?
Answer: B
Exception class provides multiple constructors including one with String message and another with String message and Throwable cause for exception chaining.
Q.62Medium
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());
}
}
}
Answer: B
getCause() returns the IOException that was wrapped. getClass().getSimpleName() returns 'IOException'.
Q.63Medium
Which of the following best describes a custom exception in Java?
Answer: B
Custom exceptions should extend Exception (for checked) or RuntimeException (for unchecked) depending on usage requirements.
Q.64Medium
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");
}
}
}
Answer: A
Multi-catch feature (Java 7+) allows handling multiple exceptions. ArrayIndexOutOfBoundsException is caught by the multi-catch block.
Q.65Easy
Which method is used to print the stack trace of an exception?
Answer: D
printStackTrace() prints to System.err, while getStackTrace() returns an array of StackTraceElement objects. Both exist and serve different purposes.
Advertisement
Q.66Easy
Which of the following is NOT a subclass of Throwable?
Answer: D
Throwable is the superclass. Exception and Error are direct subclasses, and RuntimeException is a subclass of Exception.
Q.67Medium
Consider code that implements auto-closeable resources. What is the advantage of try-with-resources statement?
Answer: D
Try-with-resources (Java 7+) automatically closes resources, handles suppressed exceptions, and closes in LIFO order without explicit close() calls.
Q.68Hard
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");
}
}
}
Answer: C
IOException is a checked exception and must be declared in method signature or caught. The inner catch throws IOException which isn't caught immediately.
Q.69Hard
Which of the following scenarios will NOT trigger a StackOverflowError?
Answer: D
StackOverflowError occurs from stack overflow due to excessive method calls. Local variables contribute to stack but won't cause overflow like recursion.
Q.70Medium
What is the correct way to create a custom exception with cause chaining?
Answer: B
Proper cause chaining calls super(msg, cause) in constructor, leveraging Exception's constructor that accepts both message and cause.
Q.71Hard
What happens when System.exit() is called inside try block?
Answer: B
System.exit() terminates the JVM immediately. The finally block does not execute as the program terminates before reaching it.
Q.72Medium
Which interface must a class implement to work with try-with-resources?
Answer: D
AutoCloseable (Java 7+) is the primary interface for try-with-resources. Closeable extends AutoCloseable. Both work with try-with-resources.
Q.73Easy
In Java exception handling, which of the following statements is true about the finally block?
Answer: B
The finally block in Java always executes whether an exception is caught or not, except when System.exit() is called or JVM terminates abnormally.
Q.74Easy
What is the output of the following code?
int x = 10;
try {
x = x / 0;
} catch (ArithmeticException e) {
x = 20;
} finally {
x = 30;
}
System.out.println(x);
Answer: C
The finally block always executes last and sets x to 30. The output will be 30 because finally block assignments override catch block assignments.
Q.75Medium
In multi-catch block (Java 7+), which operator is used to catch multiple exceptions in a single catch clause?
Answer: C
Java 7 introduced multi-catch using the pipe (|) operator: catch(IOException | SQLException e). This allows handling multiple exception types in one block.
Q.76Medium
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");
}
Answer: A
The finally block executes after catch block. 'Caught' is printed, then finally block prints 'Finally', and then RuntimeException is thrown.
Q.77Easy
Which exception is thrown when accessing an array element with an invalid index?
Answer: B
ArrayIndexOutOfBoundsException is thrown when trying to access an array element with an index that is out of the valid range (0 to length-1).
Q.78Easy
What is the correct syntax for declaring a method that throws a checked exception?
Answer: A
The 'throws' keyword is used in method declaration to indicate that the method might throw checked exceptions. Syntax: method() throws ExceptionType {}
Q.79Medium
Consider the following exception hierarchy. Which statement will compile without error?
class MyException extends Exception {}
class MyRuntimeException extends RuntimeException {}
Answer: C
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.
Q.80Easy
What will happen when the following code is executed?
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught");
}
Answer: C
Accessing arr[5] when array has only 3 elements throws ArrayIndexOutOfBoundsException which is caught and 'Exception caught' is printed.