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());
}
}
AFinally 10
B10 Finally
CFinally
D10
Correct Answer:
A. Finally 10
EXPLANATION
finally block executes before the return statement completes. System.out.println in finally executes first, then the return value 10 is printed.
Which exception is thrown when a thread tries to acquire a monitor that is already locked?
AInterruptedException
BIllegalMonitorStateException
CThreadStateException
DThreads never throw exceptions during synchronization
Correct Answer:
B. IllegalMonitorStateException
EXPLANATION
IllegalMonitorStateException is thrown when an invalid monitor state operation occurs, such as calling notify() on an object not owned by the current thread.
What will be the output?
public class Test {
public static void main(String[] args) {
try {
throw new Exception("Test");
} catch (Exception e) {
System.out.println("1");
} catch (Exception e) {
System.out.println("2");
}
}
}
A1
B1 2
CCompilation error
D2
Correct Answer:
C. Compilation error
EXPLANATION
Multiple catch blocks with the same exception type cause compilation error. Java doesn't allow duplicate catch blocks for identical exception types.
What is the output of the following code?
public class ExceptionTest {
public static void main(String[] args) {
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
}
}
ACaught Finally
BFinally
CCaught
DProgram terminates with exception
Correct Answer:
A. Caught Finally
EXPLANATION
ArithmeticException is caught by the catch block printing 'Caught', then finally block executes printing 'Finally'.
Which of the following statements about try-catch-finally blocks is correct?
Afinally block is executed even if return statement is present in try block
Bfinally block is skipped if catch block throws an exception
Cfinally block cannot contain return statements
Dfinally block is optional only when catch block is present
Correct Answer:
A. finally block is executed even if return statement is present in try block
EXPLANATION
The finally block always executes regardless of whether a return, break, continue, or exception occurs in try or catch blocks, unless System.exit() is called.
What is suppressed exception in Java (introduced in Java 7)?
AAn exception that occurs but is hidden from the programmer
BExceptions thrown during resource closure in try-with-resources are added to suppressed list of primary exception
CAn exception that doesn't need to be caught
DAn exception that only occurs in finally blocks
Correct Answer:
B. Exceptions thrown during resource closure in try-with-resources are added to suppressed list of primary exception
EXPLANATION
In try-with-resources, if an exception occurs in try and another in resource close(), the second is added as suppressed exception to the first using addSuppressed(), preserving all exception information.
Consider a method signature: public void readFile() throws IOException. Which statement is correct?
AThis method will definitely throw IOException
BThis method may throw IOException that caller must handle
CIOException will never occur in this method
DThe caller can ignore IOException
Correct Answer:
B. This method may throw IOException that caller must handle
EXPLANATION
throws declaration indicates the method may throw IOException. The caller is responsible for handling it with try-catch or declaring throws. The method doesn't always throw it.
Nested exception handling follows inner-to-outer order. Inner try-catch-finally completes fully, then control passes to outer blocks. Finally blocks execute in their respective scope order.
Which exception is thrown when attempting to access an index outside the bounds of an array?
AIndexOutOfRangeException
BArrayIndexOutOfBoundsException
CArrayIndexException
DOutOfBoundsException
Correct Answer:
B. ArrayIndexOutOfBoundsException
EXPLANATION
ArrayIndexOutOfBoundsException is thrown when attempting to access array elements with invalid indices. It's an unchecked exception extending RuntimeException.
What happens when an unchecked exception is not caught?
ACompilation fails
BThe exception propagates up the call stack until caught or program terminates
CThe exception is automatically converted to checked exception
DThe finally block is skipped
Correct Answer:
B. The exception propagates up the call stack until caught or program terminates
EXPLANATION
Unchecked exceptions (RuntimeException subclasses) don't require explicit handling. If not caught, they propagate up the call stack, eventually terminating the thread if not caught anywhere.