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.222Medium
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.223Medium
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.224Medium
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.225Medium
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.
Advertisement
Q.226Medium
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.227Medium
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.228Medium
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.229Medium
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.230Medium
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.231Medium
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.232Medium
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.233Medium
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.234Medium
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.235Medium
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.236Medium
Which of the following will NOT result in a NullPointerException?
Answer: D
Comparing with null using == operator doesn't throw NPE. The other options call methods or unbox null values, which causes NullPointerException.
Q.237Medium
In Java, which method of Throwable class is used to obtain the cause of an exception?
Answer: B
The getCause() method returns the cause (underlying Throwable) of this Throwable. It's useful for exception chaining.
Q.238Medium
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");
}
Answer: A
Integer.parseInt("Java") throws NumberFormatException, which is caught by the first catch block. More specific exceptions should be caught before general ones.
Q.239Medium
Which of the following is true about try-with-resources (Java 7+)?
Answer: A
Try-with-resources automatically invokes close() on resources implementing AutoCloseable/Closeable. Syntax: try(Resource r = new Resource()) {...}
Q.240Medium
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());
}
Answer: B
The inner try catches Exception and throws RuntimeException. The outer catch catches RuntimeException and prints 'Caught: Outer'.