Which statement is TRUE about exception propagation in Java?
Answer: A
When an exception is thrown and not caught, it propagates up the call stack until a matching catch block is found or the program terminates.
Q.22Medium
What will be the behavior if an exception is thrown in the finally block?
Answer: B
If an exception occurs in finally, it will propagate and override any previous exception from try/catch blocks. However, in try-with-resources, exceptions are suppressed.
Q.23Medium
Which statement about try-with-resources (introduced in Java 7) is correct?
Answer: B
Try-with-resources automatically invokes close() on any resource implementing AutoCloseable interface. Multiple resources can be declared with semicolon separation.
Q.24Medium
Consider this code snippet. What will happen?
try {
int x = 05;
} catch (NullPointerException e) {
System.out.println("Caught NPE");
}
Answer: B
05 throws ArithmeticException, not NullPointerException. The catch block only handles NPE, so ArithmeticException propagates to the caller.
Q.25Medium
What is the output of this code?
public class ExceptionDemo {
public static void main(String[] args) {
try {
int arr[] = new int[2];
arr[5] = 10;
} catch (Exception e) {
System.out.println("Exception caught");
} finally {
System.out.println("Finally block");
}
}
}
Answer: B
ArrayIndexOutOfBoundsException is caught by Exception catch block, printing 'Exception caught'. Finally block always executes regardless, printing 'Finally block'.
Advertisement
Q.26Medium
What happens if you use 'return' statement inside a finally block?
Answer: B
A return statement in finally will override and suppress any return value or exception from try/catch blocks. This is considered a bad practice.
Q.27Medium
What will be printed?
public class Test {
public static void main(String[] args) {
try {
System.out.println("In try");
return;
} catch (Exception e) {
System.out.println("In catch");
} finally {
System.out.println("In finally");
}
}
}
Answer: B
Try block executes printing 'In try', then return is encountered. Finally block still executes printing 'In finally' before method returns. Catch is skipped as no exception occurs.
Q.28Medium
What is the output of exception chaining in Java?
Answer: B
Exception chaining wraps a caught exception within another exception using initCause() or constructor, preserving the original exception for debugging and stack trace analysis.
Q.29Medium
Which method is used to get the exception that caused the current exception in Java?
Answer: B
The getCause() method returns the Throwable that caused the current exception, enabling exception chaining analysis and debugging.
Q.30Medium
Which exception is thrown when a thread tries to acquire a monitor that is already locked?
Answer: B
IllegalMonitorStateException is thrown when an invalid monitor state operation occurs, such as calling notify() on an object not owned by the current thread.
Q.31Medium
Consider the following code. What will be printed?
public class FinallyTest {
public static int getValue() {
try {
return 10;
} finally {
System.out.println("Finally");
}
}
public static void main(String[] args) {
System.out.println(getValue());
}
}
Answer: A
finally block executes before the return statement completes. System.out.println in finally executes first, then the return value 10 is printed.
Q.32Medium
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.33Medium
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.34Medium
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.35Medium
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.36Medium
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.37Medium
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.38Medium
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.39Medium
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.40Medium
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.