Java Programming — Exception Handling
Java OOP, collections, multithreading
30 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 30 questions in Exception Handling
What is the primary difference between throw and throws in Java exception handling?
A throw is used in catch block, throws is used in method declaration
B throw explicitly raises an exception, throws declares that a method may throw exceptions
C throw handles exceptions, throws creates exceptions
D Both are identical; throws is just an older syntax
Correct Answer:  B. throw explicitly raises an exception, throws declares that a method may throw exceptions
EXPLANATION

'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 {}

Take Test
What is the output of the following code?
try {
int x = 10/0;
} catch(ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
System.out.println("After");
A Caught\nFinally\nAfter
B Caught\nFinally
C Finally\nAfter
D ArithmeticException is thrown, program terminates
Correct Answer:  A. Caught\nFinally\nAfter
EXPLANATION

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'.

Take Test
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
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
Advertisement
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
Which of the following is NOT a subclass of Throwable?
A Exception
B Error
C RuntimeException
D Throwable itself
Correct Answer:  D. Throwable itself
EXPLANATION

Throwable is the superclass. Exception and Error are direct subclasses, and RuntimeException is a subclass of Exception.

Take Test
Which method is used to print the stack trace of an exception?
A printStackTrace()
B getStackTrace()
C printStack()
D Both A and B are correct
Correct Answer:  D. Both A and B are correct
EXPLANATION

printStackTrace() prints to System.err, while getStackTrace() returns an array of StackTraceElement objects. Both exist and serve different purposes.

Take Test
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");
}
}
}
A 1
B 1 2
C Compilation error
D 2
Correct Answer:  C. Compilation error
EXPLANATION

Multiple catch blocks with the same exception type cause compilation error. Java doesn't allow duplicate catch blocks for identical exception types.

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