Which of the following is a checked exception in Java?
Answer: B
IOException is a checked exception that must be caught or declared in the method signature. NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException are unchecked exceptions.
Q.2Medium
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.3Easy
Which exception is thrown when a method receives an illegal or inappropriate argument?
Answer: B
IllegalArgumentException is thrown to indicate that a method has been passed an illegal or inappropriate argument. It is an unchecked exception.
Q.4Easy
Can you have a try block without a catch block in Java?
Answer: B
A try block must be followed by either a catch block or a finally block (or both). A try block alone is not valid syntax.
Q.5Medium
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
Advertisement
Q.6Easy
Which of the following is NOT a subclass of Throwable in Java?
Answer: D
Runnable is a functional interface, not related to exception hierarchy. Error and Exception are direct subclasses of Throwable. RuntimeException is a subclass of Exception.
Q.7Medium
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.8Easy
Which exception is thrown when trying to access a method of a null object?
Answer: B
NullPointerException is thrown when attempting to call a method or access a property on a null object reference in Java.
Q.9Hard
Analyze the code:
try {
return 5;
} finally {
return 10;
}
What will be returned?
Answer: B
The finally block always executes, and if it contains a return statement, it overrides the return value from try or catch block. So 10 is returned.
Q.10Medium
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.11Hard
What happens if an exception is thrown in the finally block?
Answer: C
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.
Q.12Medium
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.
Q.13Medium
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.14Easy
Which exception is thrown when a string cannot be converted to a number?
Answer: A
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.
Q.15Medium
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.16Medium
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.17Medium
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.18Hard
Analyze the nested try-catch:
try {
try {
int x = 05;
} catch(NullPointerException e) {
System.out.println("Inner");
}
} catch(ArithmeticException e) {
System.out.println("Outer");
}
Answer: B
ArithmeticException is thrown by 05. Inner catch only catches NullPointerException, so it propagates to outer catch which handles ArithmeticException. Output: Outer
Q.19Easy
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");
}
}
}
Answer: A
ArrayIndexOutOfBoundsException is caught by the generic Exception catch block, printing 'Caught'.
Q.20Easy
In Java exception handling, what is the order of execution in try-finally-catch block?
Answer: A
The correct order is try block executes first, then catch block (if exception occurs), and finally block always executes at the end.