Java Programming — Exception Handling
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 41–50 of 100 questions in Exception Handling
Q.41 Medium Exception Handling
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());
}
}
A Finally 10
B 10 Finally
C Finally
D 10
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.

Take Test
Q.42 Medium Exception Handling
Which exception is thrown when a thread tries to acquire a monitor that is already locked?
A InterruptedException
B IllegalMonitorStateException
C ThreadStateException
D Threads 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.

Take Test
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");
}
}
}
A 1
B 1 2
C Compilation error
D 2
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.

Take Test
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");
}
}
}
A Caught Finally
B Finally
C Caught
D Program terminates with exception
Correct Answer:  A. Caught Finally
EXPLANATION

ArithmeticException is caught by the catch block printing 'Caught', then finally block executes printing 'Finally'.

Take Test
Which of the following statements about try-catch-finally blocks is correct?
A finally block is executed even if return statement is present in try block
B finally block is skipped if catch block throws an exception
C finally block cannot contain return statements
D finally 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.

Take Test
Advertisement
What is suppressed exception in Java (introduced in Java 7)?
A An exception that occurs but is hidden from the programmer
B Exceptions thrown during resource closure in try-with-resources are added to suppressed list of primary exception
C An exception that doesn't need to be caught
D An 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.

Take Test
Consider a method signature: public void readFile() throws IOException. Which statement is correct?
A This method will definitely throw IOException
B This method may throw IOException that caller must handle
C IOException will never occur in this method
D The 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.

Take Test
What is the execution order in try-catch-finally for nested exception handling?
A Inner try → Inner catch → Inner finally → Outer try → Outer catch → Outer finally
B Outer try → Outer catch → Inner try → Inner catch → Inner finally → Outer finally
C All finally blocks execute before any catch blocks
D Execution order depends on compiler optimization
Correct Answer:  B. Outer try → Outer catch → Inner try → Inner catch → Inner finally → Outer finally
EXPLANATION

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.

Take Test
Which exception is thrown when attempting to access an index outside the bounds of an array?
A IndexOutOfRangeException
B ArrayIndexOutOfBoundsException
C ArrayIndexException
D OutOfBoundsException
Correct Answer:  B. ArrayIndexOutOfBoundsException
EXPLANATION

ArrayIndexOutOfBoundsException is thrown when attempting to access array elements with invalid indices. It's an unchecked exception extending RuntimeException.

Take Test
What happens when an unchecked exception is not caught?
A Compilation fails
B The exception propagates up the call stack until caught or program terminates
C The exception is automatically converted to checked exception
D The 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.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips