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.482Medium
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.483Hard
Which exception is the parent class of all checked exceptions in Java (excluding RuntimeException)?
Answer: B
Exception class is the parent of all checked exceptions. RuntimeException extends Exception but is unchecked. Error and Throwable are higher in hierarchy.
Q.484Hard
Consider a scenario where multiple catch blocks are written. What is the correct order?
I. catch(FileNotFoundException e)
II. catch(IOException e)
III. catch(Exception e)
Answer: A
Catch blocks should be ordered from most specific to most general exception types. FileNotFoundException is more specific than IOException, which is more specific than Exception.
Q.485Hard
What will this code output?
int result = 0;
try {
result = 010;
} catch(Exception e) {
result = 20;
return result;
} finally {
result = 30;
}
System.out.println(result);
Answer: A
Return statement in catch block prepares the return value (20). Finally block executes but return happens after finally, so 20 is returned even though finally sets result to 30.
Advertisement
Q.486Hard
Which of the following custom exceptions would be inappropriate to extend from RuntimeException in a banking application?
Answer: D
DatabaseConnectionException should be checked (extends Exception) as it's a serious, recoverable error requiring explicit handling. Business logic exceptions can be unchecked.
Q.487Hard
In exception chaining, what is the primary benefit?
Answer: B
Exception chaining preserves the stack trace and cause information: throw new RuntimeException("msg", originalException). This helps in debugging by maintaining the complete exception history.
Q.488Medium
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.489Medium
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.490Easy
What is the output of the following code?
try {
int x = 010;
} catch(ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
System.out.println("After");
Answer: A
When ArithmeticException occurs, it's caught by the catch block printing 'Caught'. The finally block always executes, printing 'Finally'. Then normal program flow continues, printing 'After'.
Q.491Hard
If a finally block contains a return statement, what happens to an exception thrown in the try block?
Answer: A
If finally has a return statement, it suppresses any exception from try/catch blocks. The return value from finally overrides exception propagation. This is generally considered bad practice as it masks exceptions.
Q.492Medium
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.493Easy
What is the primary difference between throw and throws in Java exception handling?
Answer: B
'throw' is a statement that explicitly throws an exception object. 'throws' is a clause in method signature indicating the method may throw specific checked exceptions. Example: throw new IOException(); vs public void method() throws IOException {}
Q.494Hard
In Java 7+, exception handling introduced 'multi-catch' feature using the pipe (|) operator. What's a limitation of multi-catch blocks?
Answer: B
In multi-catch blocks like 'catch(IOException | SQLException e)', the exception types must not have an inheritance relationship. You cannot do 'catch(Exception | IOException e)' because IOException is a subclass of Exception.
Q.495Medium
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.496Hard
Which scenario would cause a StackOverflowError in Java exception handling?
Answer: B
StackOverflowError occurs when stack memory exhausts due to deep recursion. If a method catches an exception and immediately rethrows it without modification, calling itself, stack frames accumulate until overflow. This is a runtime error, not a checked exception.
Q.497Medium
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.
Q.498Easy
Which of the following is a character stream class in Java?
Answer: A
FileReader is a character stream class that reads characters from a file. FileInputStream, DataInputStream, and BufferedInputStream are byte stream classes.
Q.499Medium
What is the default buffer size of BufferedReader in Java?
Answer: C
BufferedReader has a default buffer size of 8192 characters, but commonly 2048 bytes is used. The exact implementation may vary, but 8192 is the default character buffer.
Q.500Easy
Which method is used to read a single byte from a FileInputStream?
Answer: A
The read() method reads a single byte and returns it as an integer. Returns -1 if end of stream is reached.