What is the output of the following code?
int x = 10;
try {
x = x / 0;
} catch(ArithmeticException e) {
x = x + 5;
} finally {
x = x * 2;
}
System.out.println(x);
Answer: B
ArithmeticException is caught, x becomes 10+5=15. Finally block executes, x becomes 15*2=30. Finally block always executes regardless of exception.
Q.2Medium
What will be the output?
try {
throw new Exception("Test");
} catch(Exception e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
Answer: A
The exception is caught and prints "Caught". The finally block always executes after try-catch, printing "Finally". Output: Caught Finally
Q.3Medium
What is the output of multiple catch blocks?
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Index");
} catch(Exception e) {
System.out.println("Exception");
}
Answer: A
ArrayIndexOutOfBoundsException is thrown and caught by the first catch block. The second catch block is not executed. Only the most specific matching catch block executes.
Q.4Medium
Which of the following exceptions is a checked exception?
Answer: B
FileNotFoundException is a checked exception (extends IOException). Others are unchecked - ClassCastException and NullPointerException are RuntimeExceptions, StackOverflowError is an Error.
Q.5Medium
Which statement about try-with-resources is TRUE?
Answer: C
Try-with-resources (Java 7+) automatically closes resources implementing AutoCloseable. Resources are closed in reverse order. Multiple resources can be declared with semicolon separation.
Advertisement
Q.6Medium
What is the output?
try {
int x = 05;
} catch(Exception e) {
System.out.println("Caught");
} catch(ArithmeticException ae) {
System.out.println("Arithmetic");
}
Answer: C
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.
Q.7Medium
Analyze this code:
public void test() throws IOException {
// method body
}
What does 'throws' indicate?
Answer: B
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.
Q.8Medium
Which exception hierarchy is correct in Java?
Answer: B
Throwable is the root. It has two subclasses: Exception and Error. Exception has RuntimeException as a subclass. Checked exceptions extend Exception directly.
Q.9Medium
What will happen if you don't catch a checked exception?
Answer: A
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.
Q.10Medium
Which statement about multiple catch blocks is TRUE?
Answer: B
Only one catch block executes for a thrown exception. The first matching catch block is executed.
Q.11Medium
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.12Medium
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.13Medium
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.14Medium
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.15Medium
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.
Q.16Medium
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.17Medium
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.18Medium
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.19Medium
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.20Medium
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.