Java Programming — Exception Handling
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 81–90 of 100 questions in Exception Handling
In Java exception handling, what is the order of execution in try-finally-catch block?
A try → catch → finally
B try → finally → catch
C catch → try → finally
D finally → try → catch
Correct Answer:  A. try → catch → finally
EXPLANATION

The correct order is try block executes first, then catch block (if exception occurs), and finally block always executes at the end.

Take Test
What is the output of this code?
public class Test {
public static void main(String[] args) {
try {
int[] arr = {1, 2};
System.out.println(arr[5]);
} catch (Exception e) {
System.out.println("Caught");
}
}
}
A Caught
B ArrayIndexOutOfBoundsException
C Compilation error
D No output
Correct Answer:  A. Caught
EXPLANATION

ArrayIndexOutOfBoundsException is caught by the generic Exception catch block, printing 'Caught'.

Take Test
Analyze the nested try-catch:
try {
try {
int x = 5/0;
} catch(NullPointerException e) {
System.out.println("Inner");
}
} catch(ArithmeticException e) {
System.out.println("Outer");
}
A Inner
B Outer
C Inner Outer
D Neither prints
Correct Answer:  B. Outer
EXPLANATION

ArithmeticException is thrown by 5/0. Inner catch only catches NullPointerException, so it propagates to outer catch which handles ArithmeticException. Output: Outer

Take Test
Q.84 Medium Exception Handling
What will happen if you don't catch a checked exception?
A Compilation error
B Runtime error
C It will be automatically handled
D Program will run successfully
Correct Answer:  A. Compilation error
EXPLANATION

Checked exceptions must be either caught in a try-catch block or declared in the method signature with 'throws'. Failure to do so results in a compilation error.

Take Test
Q.85 Medium Exception Handling
Which exception hierarchy is correct in Java?
A Throwable → Error → Exception → RuntimeException
B Throwable → Exception → RuntimeException and Throwable → Error
C Exception → Throwable → Error → RuntimeException
D RuntimeException → Exception → Throwable → Error
Correct Answer:  B. Throwable → Exception → RuntimeException and Throwable → Error
EXPLANATION

Throwable is the root. It has two subclasses: Exception and Error. Exception has RuntimeException as a subclass. Checked exceptions extend Exception directly.

Take Test
Q.86 Medium Exception Handling
Analyze this code:
public void test() throws IOException {
// method body
}
What does 'throws' indicate?
A The method will definitely throw IOException
B The method might throw IOException and caller must handle it
C IOException is caught inside the method
D IOException is prevented from being thrown
Correct Answer:  B. The method might throw IOException and caller must handle it
EXPLANATION

The 'throws' keyword declares that a method might throw the specified checked exception. The responsibility of handling is passed to the caller of the method.

Take Test
Which exception is thrown when a string cannot be converted to a number?
A NumberFormatException
B ConversionException
C ParseException
D StringConversionException
Correct Answer:  A. NumberFormatException
EXPLANATION

NumberFormatException is thrown when attempting to convert a string to a numeric type but the string does not have an appropriate format. It's an unchecked exception.

Take Test
Q.88 Medium Exception Handling
What is the output?
try {
int x = 5/0;
} catch(Exception e) {
System.out.println("Caught");
} catch(ArithmeticException ae) {
System.out.println("Arithmetic");
}
A Caught
B Arithmetic
C Compilation Error
D Caught Arithmetic
Correct Answer:  C. Compilation Error
EXPLANATION

This is a compilation error. Catch blocks must be ordered from most specific to most general. ArithmeticException is more specific than Exception, so it should come first.

Take Test
Q.89 Medium Exception Handling
Which statement about try-with-resources is TRUE?
A It requires manual closing of resources
B It was introduced in Java 6
C Resources are automatically closed in reverse order of opening
D It cannot be used with multiple resources
Correct Answer:  C. Resources are automatically closed in reverse order of opening
EXPLANATION

Try-with-resources (Java 7+) automatically closes resources implementing AutoCloseable. Resources are closed in reverse order. Multiple resources can be declared with semicolon separation.

Take Test
What happens if an exception is thrown in the finally block?
A It suppresses the original exception
B It is ignored
C It replaces the original exception
D It causes a compilation error
Correct Answer:  C. It replaces the original exception
EXPLANATION

If an exception occurs in the finally block, it will replace any exception thrown in the try or catch block, and the new exception will be propagated.

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