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.42Medium
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.43Medium
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.44Medium
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.45Medium
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()) {...}
Advertisement
Q.46Medium
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'.
Q.47Medium
In Java 8+, when using try-with-resources with multiple AutoCloseable resources, in what order are they closed?
Answer: B
Try-with-resources closes resources in LIFO (Last In, First Out) order. If you declare Resource1, then Resource2, Resource2 closes first, then Resource1. This ensures dependencies are respected.
Q.48Medium
Which of the following exceptions would NOT be caught by catching Exception class in Java?
Answer: C
Error is not a subclass of Exception. Both Error and Exception extend Throwable. Errors like OutOfMemoryError, StackOverflowError cannot be caught by Exception handlers. They must be caught separately if needed.
Q.49Medium
Consider a scenario where you have nested try-catch blocks. If both inner and outer catch blocks match the thrown exception type, which one executes?
Answer: B
The innermost matching catch block executes first. If it doesn't rethrow the exception, the outer catch block won't execute. The exception is considered handled after the first matching catch block.
Q.50Medium
What does the getSuppressed() method of Throwable class return in context of try-with-resources?
Answer: B
When try-with-resources encounters an exception while closing a resource, it's added to the suppressed exceptions list. If an exception was already thrown in try, the close exception is suppressed rather than replacing it. getSuppressed() returns array of these suppressed exceptions.
Q.51Medium
In Java, when an exception is thrown in a try block and caught in a catch block, if the catch block also throws an exception, what happens to the original exception?
Answer: C
When exception chaining is explicitly used with initCause() or constructor parameters, the original exception becomes the cause of the new exception. Without explicit chaining, the original exception is lost. Exception chaining is not automatic in Java.