Java MCQs – Exception Handling

21.) Which exception is thrown when a file is not found during input/output operations?

A) FileNotFoundException
B) FileNotFound
C) IOErrorException
D) FileException

Answer: Option A

Explanation: “FileNotFoundException” is thrown when a file is not found during input/output operations.

22.) What is the purpose of the “assert” statement in Java?

A) To handle exceptions
B) To specify that a method is deprecated
C) To check a condition and throw an exception if it is false
D) To catch exceptions

Answer: Option C

Explanation: The “assert” statement is used to check a condition and throw an exception if it is false.

23.) Which exception is thrown when a program tries to divide a number by zero?

A) DivideByZeroException
B) ArithmeticException
C) ZeroDivisionException
D) MathError

Answer: Option B

Explanation: “ArithmeticException” is thrown when a program tries to divide a number by zero.

24.) In a multi-catch block, what must be the relationship between the exception types?

A) They must all be checked exceptions.
B) They must all be unchecked exceptions.
C) They must be related by inheritance, with the most specific exception type first.
D) They must be unrelated and can be in any order.

Answer: Option C

Explanation: In a multi-catch block, the exception types must be related by inheritance, with the most specific exception type first.

25.) What is the purpose of the “try” block in exception handling?

A) To catch exceptions
B) To specify the code that may throw exceptions
C) To handle and process exceptions
D) To terminate the program

Answer: Option B

Explanation: The “try” block is used to specify the code that may throw exceptions.

26.) What happens if a “finally” block contains a “return” statement?

A) The program will not compile.
B) The “finally” block will be ignored.
C) The “return” statement in the “finally” block will override any “return” statements in the “try” or “catch” blocks.
D) The program will throw a runtime exception.

Answer: Option C

Explanation: If a “finally” block contains a “return” statement, it will override any “return” statements in the “try” or “catch” blocks.

27.) What is the purpose of the “Throwable” class in Java exception hierarchy?

A) It is the base class for all exceptions.
B) It is used to handle exceptions.
C) It is the root class for checked exceptions.
D) It is the root class for unchecked exceptions. A

Answer: Option A

Explanation: The “Throwable” class is the base class for all exceptions in Java.

28.) Which exception is thrown when a program exhausts its stack due to excessive method calls?

A) StackOverflowError
B) OutOfMemoryError
C) TooManyCallsException
D) RecursiveCallException

Answer: Option A

Explanation: “StackOverflowError” is thrown when a program exhausts its stack due to excessive method calls.

29.) What is the difference between checked and unchecked exceptions?

A) Checked exceptions are always caught by the compiler, while unchecked exceptions are not.
B) Checked exceptions must be caught or declared, while unchecked exceptions do not need to be.
C) Checked exceptions are more severe than unchecked exceptions.
D) Checked exceptions are related to syntax errors, while unchecked exceptions are related to logic errors. B

Answer: Option B

Explanation: Checked exceptions must be caught or declared, while unchecked exceptions do not need to be.

30.) Which exception is thrown when a method is called on an object that is not properly initialized?

A) InitializationException
B) optObjectNotInitializedExceptionion2
C) NullPointerException
D) UninitializedObjectException

Answer: Option B

Explanation: “ObjectNotInitializedException” is thrown when a method is called on an object that is not properly initialized.

Leave a Reply

Your email address will not be published. Required fields are marked *