Java Programming — Exception Handling
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 61–70 of 100 questions in Exception Handling
Q.61 Medium Exception Handling
Which statement about try-with-resources (introduced in Java 7) is correct?
A It can only handle one resource at a time
B It automatically calls close() method on AutoCloseable resources
C It eliminates the need for finally blocks completely
D It 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.

Take Test
Q.62 Medium Exception Handling
What will be the behavior if an exception is thrown in the finally block?
A It will be silently ignored
B It will replace any exception thrown in try/catch blocks
C It will be added to the exception suppression list
D The 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.

Take Test
In Java exception handling, what is the purpose of the finally block?
A To catch exceptions that were not caught in catch blocks
B To execute code regardless of whether an exception occurs or not
C To declare new exceptions
D To 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.

Take Test
What is the correct syntax for declaring multiple exceptions in a method signature?
A public void method() throws IOException, SQLException { }
B public void method() throws (IOException, SQLException) { }
C public void method() throws IOException | SQLException { }
D public void method() throw IOException, SQLException { }
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.

Take Test
Which of the following exceptions is a checked exception in Java?
A NullPointerException
B SQLException
C ArrayIndexOutOfBoundsException
D ArithmeticException
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).

Take Test
Q.66 Medium Exception Handling
Which statement is TRUE about exception propagation in Java?
A Exceptions propagate only upward in call stack
B Exceptions propagate downward in call stack
C Exceptions don't propagate
D Exceptions propagate both ways
Correct Answer:  A. Exceptions propagate only upward in call stack
EXPLANATION

When an exception is thrown and not caught, it propagates up the call stack until a matching catch block is found or the program terminates.

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

Finally block executes first (prints 'Finally'), then the exception propagates to outer catch block which prints 'Caught'.

Take Test
Q.68 Medium Exception Handling
Which of the following cannot be used with try-with-resources?
A FileInputStream
B BufferedReader
C String
D Scanner
Correct Answer:  C. String
EXPLANATION

Try-with-resources requires resources to implement AutoCloseable interface. String doesn't implement it, so it cannot be used.

Take Test
Q.69 Medium Exception Handling
Consider this code:
public static void main(String[] args) {
try {
System.out.println("A");
return;
} finally {
System.out.println("B");
}
}
What is the output?
A A B
B A
C B A
D B
Correct Answer:  A. A B
EXPLANATION

Even with return in try block, finally block executes. So 'A' prints first, then 'B' before returning.

Take Test
Q.70 Medium Exception Handling
What is the difference between throw and throws?
A throw creates exception, throws declares it
B throws creates exception, throw declares it
C Both are same
D throw is for checked, throws for unchecked
Correct Answer:  A. throw creates exception, throws declares it
EXPLANATION

'throw' keyword actually throws an exception object. 'throws' keyword declares that a method throws a checked exception.

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