Analyze the code:
public void process() throws IOException {
try {
readFile();
} catch (FileNotFoundException e) {
// handle
}
}
What is the issue here?
Answer: C
Since FileNotFoundException is a subclass of IOException, and readFile() can throw IOException, it should be caught in a separate catch block or the method signature should only throw FileNotFoundException.
Q.422Medium
What happens if you throw an exception inside the finally block?
Answer: A
If an exception is thrown in finally block, it replaces the original exception that was being propagated. The original exception is lost.
Q.423Medium
Consider the code:
try (FileReader fr = new FileReader("file.txt")) {
// use fr
} catch (IOException e) {
// handle
}
What will happen to 'fr' after the try block?
Answer: B
Try-with-resources automatically closes resources implementing AutoCloseable interface, regardless of normal or exceptional termination.
Q.424Medium
Which of these is NOT a correct way to handle checked exceptions?
Answer: D
Checked exceptions must be either caught or declared using throws. Ignoring them causes compilation error.
Q.425Medium
What is the output?
public class Test {
public static void main(String[] args) {
int x = 0;
try {
x = 5;
System.out.println(x / 0);
x = 10;
} catch (ArithmeticException e) {
x = 15;
} finally {
System.out.println(x);
}
}
}
Answer: C
x becomes 5, then ArithmeticException occurs. x becomes 15 in catch block. Finally block prints 15.
Advertisement
Q.426Medium
What will happen if a finally block contains a return statement?
Answer: A
Return in finally block overrides returns in try or catch blocks. The finally return value is what gets returned.
Q.427Medium
Analyze the code:
try {
// code
} catch (IOException | SQLException e) {
// handle
}
What is this syntax called?
Answer: A
This is called multi-catch or union catch syntax (Java 7+) allowing multiple exception types to be caught in one block using pipe operator.
Q.428Medium
What is the difference between throw and throws?
Answer: A
'throw' keyword actually throws an exception object. 'throws' keyword declares that a method throws a checked exception.
Q.429Medium
Consider this code:
public static void main(String[] args) {
try {
System.out.println("A");
return;
} finally {
System.out.println("B");
}
}
What is the output?
Answer: A
Even with return in try block, finally block executes. So 'A' prints first, then 'B' before returning.
Q.430Medium
Which of the following cannot be used with try-with-resources?
Answer: C
Try-with-resources requires resources to implement AutoCloseable interface. String doesn't implement it, so it cannot be used.
Q.431Hard
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");
}
}
}
Answer: A
Finally block executes first (prints 'Finally'), then the exception propagates to outer catch block which prints 'Caught'.
Q.432Medium
Which statement is TRUE about exception propagation in Java?
Answer: A
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.
Q.433Easy
Which of the following exceptions is a checked exception in Java?
Answer: B
SQLException is a checked exception that must be caught or declared. NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException are unchecked exceptions (RuntimeException subclasses).
Q.434Easy
What is the correct syntax for declaring multiple exceptions in a method signature?
Answer: A
Multiple exceptions are declared using comma-separated syntax in the throws clause. Parentheses and pipe operators are not valid syntax for this purpose.
Q.435Easy
In Java exception handling, what is the purpose of the finally block?
Answer: B
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.
Q.436Medium
What will be the behavior if an exception is thrown in the finally block?
Answer: B
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.
Q.437Medium
Which statement about try-with-resources (introduced in Java 7) is correct?
Answer: B
Try-with-resources automatically invokes close() on any resource implementing AutoCloseable interface. Multiple resources can be declared with semicolon separation.
Q.438Easy
What is the difference between 'throw' and 'throws' keywords in Java?
Answer: B
throw explicitly throws an exception object; throws declares in method signature that exceptions may be thrown. Example: throw new IOException() vs public void method() throws IOException.
Q.439Medium
Consider this code snippet. What will happen?
try {
int x = 05;
} catch (NullPointerException e) {
System.out.println("Caught NPE");
}
Answer: B
05 throws ArithmeticException, not NullPointerException. The catch block only handles NPE, so ArithmeticException propagates to the caller.
Q.440Medium
What is the output of this code?
public class ExceptionDemo {
public static void main(String[] args) {
try {
int arr[] = new int[2];
arr[5] = 10;
} catch (Exception e) {
System.out.println("Exception caught");
} finally {
System.out.println("Finally block");
}
}
}
Answer: B
ArrayIndexOutOfBoundsException is caught by Exception catch block, printing 'Exception caught'. Finally block always executes regardless, printing 'Finally block'.