Which statement about try-with-resources (introduced in Java 7) is correct?
AIt can only handle one resource at a time
BIt automatically calls close() method on AutoCloseable resources
CIt eliminates the need for finally blocks completely
DIt requires explicit resource declaration in finally block
Correct Answer:
B. It automatically calls close() method on AutoCloseable resources
EXPLANATION
Try-with-resources automatically invokes close() on any resource implementing AutoCloseable interface. Multiple resources can be declared with semicolon separation.
What will be the behavior if an exception is thrown in the finally block?
AIt will be silently ignored
BIt will replace any exception thrown in try/catch blocks
CIt will be added to the exception suppression list
DThe program will definitely crash
Correct Answer:
B. It will replace any exception thrown in try/catch blocks
EXPLANATION
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.
In Java exception handling, what is the purpose of the finally block?
ATo catch exceptions that were not caught in catch blocks
BTo execute code regardless of whether an exception occurs or not
CTo declare new exceptions
DTo rethrow exceptions
Correct Answer:
B. To execute code regardless of whether an exception occurs or not
EXPLANATION
The finally block executes unconditionally after try and/or catch blocks complete, making it ideal for resource cleanup. It runs even if try or catch blocks return.
Correct Answer:
A. public void method() throws IOException, SQLException { }
EXPLANATION
Multiple exceptions are declared using comma-separated syntax in the throws clause. Parentheses and pipe operators are not valid syntax for this purpose.
Which of the following exceptions is a checked exception in Java?
ANullPointerException
BSQLException
CArrayIndexOutOfBoundsException
DArithmeticException
Correct Answer:
B. SQLException
EXPLANATION
SQLException is a checked exception that must be caught or declared. NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException are unchecked exceptions (RuntimeException subclasses).
What will be printed?
public class ExceptionTest {
public static void main(String[] args) {
try {
testMethod();
} catch (Exception e) {
System.out.println("Caught");
}
}
public static void testMethod() {
try {
throw new RuntimeException("Error");
} finally {
System.out.println("Finally");
}
}
}
AFinally Caught
BCaught Finally
CFinally
DCaught
Correct Answer:
A. Finally Caught
EXPLANATION
Finally block executes first (prints 'Finally'), then the exception propagates to outer catch block which prints 'Caught'.
Consider this code:
public static void main(String[] args) {
try {
System.out.println("A");
return;
} finally {
System.out.println("B");
}
}
What is the output?
AA B
BA
CB A
DB
Correct Answer:
A. A B
EXPLANATION
Even with return in try block, finally block executes. So 'A' prints first, then 'B' before returning.