What will be the result of executing this code?
public class MultiCatchTest {
public static void main(String[] args) {
try {
int[] arr = {1, 2};
int x = arr[5];
} catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
System.out.println("Array or Null Error");
}
}
}
Answer: A
Multi-catch feature (Java 7+) allows handling multiple exceptions. ArrayIndexOutOfBoundsException is caught by the multi-catch block.
Q.462Easy
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.463Easy
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.464Medium
Consider code that implements auto-closeable resources. What is the advantage of try-with-resources statement?
Answer: D
Try-with-resources (Java 7+) automatically closes resources, handles suppressed exceptions, and closes in LIFO order without explicit close() calls.
Q.465Hard
What will be printed for this code?
public class ExceptionOrder {
public static void main(String[] args) {
try {
try {
throw new RuntimeException("Inner");
} catch (Exception e) {
throw new IOException("Outer");
}
} catch (IOException e) {
System.out.println("Caught IOException");
} catch (RuntimeException e) {
System.out.println("Caught RuntimeException");
}
}
}
Answer: C
IOException is a checked exception and must be declared in method signature or caught. The inner catch throws IOException which isn't caught immediately.
Advertisement
Q.466Hard
Which of the following scenarios will NOT trigger a StackOverflowError?
Answer: D
StackOverflowError occurs from stack overflow due to excessive method calls. Local variables contribute to stack but won't cause overflow like recursion.
Q.467Medium
What is the correct way to create a custom exception with cause chaining?
Answer: B
Proper cause chaining calls super(msg, cause) in constructor, leveraging Exception's constructor that accepts both message and cause.
Q.468Hard
What happens when System.exit() is called inside try block?
Answer: B
System.exit() terminates the JVM immediately. The finally block does not execute as the program terminates before reaching it.
Q.469Medium
Which interface must a class implement to work with try-with-resources?
Answer: D
AutoCloseable (Java 7+) is the primary interface for try-with-resources. Closeable extends AutoCloseable. Both work with try-with-resources.
Q.470Easy
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.471Easy
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.
Q.472Medium
In multi-catch block (Java 7+), which operator is used to catch multiple exceptions in a single catch clause?
Answer: C
Java 7 introduced multi-catch using the pipe (|) operator: catch(IOException | SQLException e). This allows handling multiple exception types in one block.
Q.473Medium
What will be the output?
try {
throw new Exception("Test");
} catch(Exception e) {
System.out.println("Caught");
throw new RuntimeException("New");
} finally {
System.out.println("Finally");
}
Answer: A
The finally block executes after catch block. 'Caught' is printed, then finally block prints 'Finally', and then RuntimeException is thrown.
Q.474Easy
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.475Easy
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.476Medium
Consider the following exception hierarchy. Which statement will compile without error?
class MyException extends Exception {}
class MyRuntimeException extends RuntimeException {}
Answer: C
RuntimeException and its subclasses are unchecked, so they don't need to be caught or declared. Option A requires try-catch since MyException is checked.
Q.477Easy
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.478Medium
Which of the following will NOT result in a NullPointerException?
Answer: D
Comparing with null using == operator doesn't throw NPE. The other options call methods or unbox null values, which causes NullPointerException.
Q.479Medium
In Java, which method of Throwable class is used to obtain the cause of an exception?
Answer: B
The getCause() method returns the cause (underlying Throwable) of this Throwable. It's useful for exception chaining.
Q.480Medium
What will be printed when this code executes?
String str = "Java";
try {
int x = Integer.parseInt(str);
} catch(NumberFormatException e) {
System.out.println("Invalid number");
} catch(Exception e) {
System.out.println("General exception");
}
Answer: A
Integer.parseInt("Java") throws NumberFormatException, which is caught by the first catch block. More specific exceptions should be caught before general ones.