Java Programming — Exception Handling
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 100 questions in Exception Handling
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");
}
A Compilation error
B Runtime error without output
C Exception caught
D No output
Correct Answer:  C. Exception caught
EXPLANATION

Accessing arr[5] when array has only 3 elements throws ArrayIndexOutOfBoundsException which is caught and 'Exception caught' is printed.

Take Test
Q.22 Medium Exception Handling
Consider the following exception hierarchy. Which statement will compile without error?
class MyException extends Exception {}
class MyRuntimeException extends RuntimeException {}
A throw new MyException();
B int x = 10; x = x / 0; // No need to handle MyException
C Method m() throws MyRuntimeException { return null; }
D new MyException(); // Valid statement without try-catch
Correct Answer:  C. Method m() throws MyRuntimeException { return null; }
EXPLANATION

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.

Take Test
What is the correct syntax for declaring a method that throws a checked exception?
A public void method() throws IOException {}
B public void method() catch IOException {}
C public void method() try IOException {}
D public void method() exceptions IOException {}
Correct Answer:  A. public void method() throws IOException {}
EXPLANATION

The 'throws' keyword is used in method declaration to indicate that the method might throw checked exceptions. Syntax: method() throws ExceptionType {}

Take Test
Which exception is thrown when accessing an array element with an invalid index?
A IndexOutOfBoundsException
B ArrayIndexOutOfBoundsException
C ArrayAccessException
D IndexException
Correct Answer:  B. ArrayIndexOutOfBoundsException
EXPLANATION

ArrayIndexOutOfBoundsException is thrown when trying to access an array element with an index that is out of the valid range (0 to length-1).

Take Test
Q.25 Medium Exception Handling
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");
}
A Caught Finally followed by RuntimeException
B Caught RuntimeException Finally
C Finally Caught
D RuntimeException Caught Finally
Correct Answer:  A. Caught Finally followed by RuntimeException
EXPLANATION

The finally block executes after catch block. 'Caught' is printed, then finally block prints 'Finally', and then RuntimeException is thrown.

Take Test
Advertisement
Q.26 Medium Exception Handling
In multi-catch block (Java 7+), which operator is used to catch multiple exceptions in a single catch clause?
A &&
B ||
C |
D &
Correct Answer:  C. |
EXPLANATION

Java 7 introduced multi-catch using the pipe (|) operator: catch(IOException | SQLException e). This allows handling multiple exception types in one block.

Take Test
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);
A 10
B 20
C 30
D Error: compilation fails
Correct Answer:  C. 30
EXPLANATION

The finally block always executes last and sets x to 30. The output will be 30 because finally block assignments override catch block assignments.

Take Test
In Java exception handling, which of the following statements is true about the finally block?
A finally block executes only if an exception is thrown
B finally block executes regardless of whether an exception occurs or not
C finally block executes only if no exception is thrown
D finally block executes before the catch block
Correct Answer:  B. finally block executes regardless of whether an exception occurs or not
EXPLANATION

The finally block in Java always executes whether an exception is caught or not, except when System.exit() is called or JVM terminates abnormally.

Take Test
Q.29 Medium Exception Handling
Which interface must a class implement to work with try-with-resources?
A Closeable
B AutoCloseable
C Serializable
D Both A and B
Correct Answer:  D. Both A and B
EXPLANATION

AutoCloseable (Java 7+) is the primary interface for try-with-resources. Closeable extends AutoCloseable. Both work with try-with-resources.

Take Test
What happens when System.exit() is called inside try block?
A finally block still executes
B finally block does not execute
C catch block executes first
D Compilation error occurs
Correct Answer:  B. finally block does not execute
EXPLANATION

System.exit() terminates the JVM immediately. The finally block does not execute as the program terminates before reaching it.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips