What will be the output?
public class Test {
public static void main(String[] args) {
try {
throw new Exception("Test");
} catch (Exception e) {
System.out.println("1");
} catch (Exception e) {
System.out.println("2");
}
}
}
Answer: C
Multiple catch blocks with the same exception type cause compilation error. Java doesn't allow duplicate catch blocks for identical exception types.
Q.22Easy
Which method is used to print the stack trace of an exception?
Answer: D
printStackTrace() prints to System.err, while getStackTrace() returns an array of StackTraceElement objects. Both exist and serve different purposes.
Q.23Easy
Which of the following is NOT a subclass of Throwable?
Answer: D
Throwable is the superclass. Exception and Error are direct subclasses, and RuntimeException is a subclass of Exception.
Q.24Easy
In Java exception handling, which of the following statements is true about the finally block?
Answer: B
The finally block in Java always executes whether an exception is caught or not, except when System.exit() is called or JVM terminates abnormally.
Q.25Easy
What is the output of the following code?
int x = 10;
try {
x = x / 0;
} catch (ArithmeticException e) {
x = 20;
} finally {
x = 30;
}
System.out.println(x);
Answer: C
The finally block always executes last and sets x to 30. The output will be 30 because finally block assignments override catch block assignments.
Advertisement
Q.26Easy
Which exception is thrown when accessing an array element with an invalid index?
Answer: B
ArrayIndexOutOfBoundsException is thrown when trying to access an array element with an index that is out of the valid range (0 to length-1).
Q.27Easy
What is the correct syntax for declaring a method that throws a checked exception?
Answer: A
The 'throws' keyword is used in method declaration to indicate that the method might throw checked exceptions. Syntax: method() throws ExceptionType {}
Q.28Easy
What will happen when the following code is executed?
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught");
}
Answer: C
Accessing arr[5] when array has only 3 elements throws ArrayIndexOutOfBoundsException which is caught and 'Exception caught' is printed.
Q.29Easy
What is the output of the following code?
try {
int x = 010;
} catch(ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
System.out.println("After");
Answer: A
When ArithmeticException occurs, it's caught by the catch block printing 'Caught'. The finally block always executes, printing 'Finally'. Then normal program flow continues, printing 'After'.
Q.30Easy
What is the primary difference between throw and throws in Java exception handling?
Answer: B
'throw' is a statement that explicitly throws an exception object. 'throws' is a clause in method signature indicating the method may throw specific checked exceptions. Example: throw new IOException(); vs public void method() throws IOException {}