Java Exception Handling MCQ Questions and Answers

1. Which keyword is used to handle an exception in Java?

a) throw
b) throws
c) try
d) catch

Answer:

d) catch

Explanation:

The 'catch' block is used to handle the exception. It contains the code to handle the exception that might have occurred in the try block.

2. Which of these is a superclass of all exception types in Java?

a) RuntimeException
b) Throwable
c) Error
d) Exception

Answer:

b) Throwable

Explanation:

Throwable is the superclass of all errors and exceptions in the Java language.

3. What is the purpose of the finally block in exception handling?

a) To handle the exception
b) To declare an exception
c) To execute code, regardless of whether an exception is caught
d) To throw an exception

Answer:

c) To execute code, regardless of whether an exception is caught

Explanation:

The finally block is executed after the try and catch blocks, regardless of whether an exception was caught or not.

4. Which of these is not a checked exception?

a) IOException
b) SQLException
c) ArithmeticException
d) ClassNotFoundException

Answer:

c) ArithmeticException

Explanation:

ArithmeticException is an unchecked exception, as it is derived from RuntimeException.

5. Which statement is true about checked exceptions?

a) They are checked at compile-time
b) They are not checked at runtime
c) They can be ignored during compilation
d) They are only thrown by the JVM

Answer:

a) They are checked at compile-time

Explanation:

Checked exceptions are checked at compile-time. The compiler ensures that such exceptions are either caught or declared in the method signature.

6. What is the purpose of the throw keyword in Java?

a) To handle an exception
b) To declare an exception
c) To recover from an exception
d) To explicitly throw an exception

Answer:

d) To explicitly throw an exception

Explanation:

The throw keyword is used to explicitly throw an exception, either a newly instantiated one or an exception that was caught.

7. What does the throws keyword do in a method signature?

a) Catches the exception thrown by the method
b) Declares an exception that might be thrown by the method
c) Throws an exception explicitly
d) Handles multiple exceptions

Answer:

b) Declares an exception that might be thrown by the method

Explanation:

The throws keyword in a method signature is used to declare one or more exceptions that might be thrown by the method.

8. Which of these exception types is used to indicate a problem with code logic?

a) IOException
b) SQLException
c) NullPointerException
d) ArrayIndexOutOfBoundsException

Answer:

c) NullPointerException

Explanation:

NullPointerException is thrown when an application attempts to use null where an object is required, indicating a logic error or a bug.

9. What is a custom exception in Java?

a) An existing exception provided by the Java API
b) An exception that is thrown automatically by the JVM
c) An exception defined by the user
d) An unchecked exception

Answer:

c) An exception defined by the user

Explanation:

A custom exception is an exception defined by the user for specific application needs, typically extending Exception or RuntimeException.

10. Which method is commonly used to print the stack trace of an exception?

a) printStackTrace()
b) getMessage()
c) getCause()
d) toString()

Answer:

a) printStackTrace()

Explanation:

printStackTrace() prints the stack trace of the Throwable object to the standard error stream, which is helpful for debugging.

11. What happens if an exception is not caught in the catch block?

a) The finally block handles it
b) The exception is ignored
c) The exception is thrown to the caller method
d) The program terminates immediately

Answer:

c) The exception is thrown to the caller method

Explanation:

If an exception is not caught in the catch block, it is propagated back to the caller method.

12. Which of these is a runtime exception?

a) IOException
b) InterruptedException
c) NumberFormatException
d) ReflectiveOperationException

Answer:

c) NumberFormatException

Explanation:

NumberFormatException is an unchecked exception and is a subclass of RuntimeException.

13. What is the correct way to declare a method that might throw an IOException?

a) public void readFile() throws IOException
b) public void readFile() catch IOException
c) public void readFile() throw IOException
d) public void readFile() exception IOException

Answer:

a) public void readFile() throws IOException

Explanation:

The correct syntax to declare a method that might throw an IOException is to use the throws keyword followed by the exception type.

14. Can a try block exist without a catch block?

a) No, a try block must be followed by a catch block
b) Yes, but only if followed by a finally block
c) Yes, a try block can exist independently
d) Yes, but only if the exceptions are handled elsewhere

Answer:

b) Yes, but only if followed by a finally block

Explanation:

A try block can exist without a catch block only if it is followed by a finally block.

15. What type of exceptions do Errors represent?

a) Syntax errors
b) Runtime errors that are recoverable
c) Problems that are external to the application
d) Severe problems that are not expected to be caught under normal circumstances

Answer:

d) Severe problems that are not expected to be caught under normal circumstances

Explanation:

Errors represent severe problems like hardware failure that applications should not attempt to catch.

16. What is the main reason to use a finally block in Java?

a) To catch exceptions
b) To throw exceptions
c) To run cleanup code regardless of exception occurrence
d) To log the occurrence of an exception

Answer:

c) To run cleanup code regardless of exception occurrence

Explanation:

The finally block is used for cleanup code that needs to run regardless of whether an exception occurred or not.

17. What will happen if both try and catch blocks have return statements in Java?

a) Compilation error
b) The method will always return the value from the try block
c) The method will always return the value from the catch block
d) The return statement in the finally block, if present, will override the others

Answer:

d) The return statement in the finally block, if present, will override the others

Explanation:

If the finally block has a return statement, it will override the return statements from both the try and catch blocks.

18. How can multiple exceptions be caught in a single catch block in Java?

a) By using the pipe character (|)
b) By separating exception types with a comma
c) By listing all exceptions in parentheses
d) It is not possible to catch multiple exceptions in a single catch block

Answer:

a) By using the pipe character (|)

Explanation:

Multiple exceptions can be caught in a single catch block using the pipe character (|) to separate the exception types.

19. What is exception chaining in Java?

a) Throwing multiple exceptions one after another
b) Combining several catch blocks into one
c) Setting a cause for an exception
d) Handling exceptions in a parent-child hierarchy

Answer:

c) Setting a cause for an exception

Explanation:

Exception chaining in Java is a technique where one exception causes another exception. It can be implemented by setting a cause in an exception.

20. Which keyword is used to manually throw an exception in Java?

a) throw
b) throws
c) trigger
d) raise

Answer:

a) throw

Explanation:

The throw keyword is used to manually throw an exception in Java.

21. What is the difference between final, finally, and finalize in Java?

a) There is no significant difference
b) final is a keyword, finally is a block, and finalize is a method
c) final and finally are keywords, finalize is a block
d) final and finalize are methods, finally is a keyword

Answer:

b) final is a keyword, finally is a block, and finalize is a method

Explanation:

'final' is a keyword used to apply restrictions, 'finally' is a block that executes code regardless of exceptions, and 'finalize' is a method called by the garbage collector before an object is destroyed.

22. In which scenario is a ClassNotFoundException thrown?

a) When a class is not found in the classpath at runtime
b) When a class cannot be instantiated
c) When a method in a class cannot be found
d) When a file containing the class definition cannot be found

Answer:

a) When a class is not found in the classpath at runtime

Explanation:

ClassNotFoundException is thrown when an application tries to load a class through its string name but no definition for the class with the specified name could be found.

23. Which exception is thrown when an attempt is made to access an illegal array index?

a) NullPointerException
b) ArrayIndexOutOfBoundsException
c) IllegalArgumentException
d) IndexOutOfBoundsException

Answer:

b) ArrayIndexOutOfBoundsException

Explanation:

ArrayIndexOutOfBoundsException is thrown to indicate that an array has been accessed with an illegal index.

24. What does the term "exception propagation" refer to in Java?

a) Spreading an exception to different classes
b) Forwarding a caught exception to a higher method in the call stack
c) The process of creating custom exceptions
d) Distributing exceptions among multiple catch blocks

Answer:

b) Forwarding a caught exception to a higher method in the call stack

Explanation:

Exception propagation refers to the process where an exception is automatically forwarded up the call stack if not caught in the current method.

25. What is the result of compiling and running a Java program with a try block having only a return statement and no catch or finally block?

a) Compilation error
b) Runtime exception
c) The return statement in the try block is executed
d) The program does nothing

Answer:

a) Compilation error

Explanation:

A try block without a catch or finally block is not valid in Java and will result in a compilation error.

26. Which exception is thrown when an application tries to create an instance of a class using the newInstance method of class Class, but the specified class object cannot be instantiated?

a) ClassNotFoundException
b) InstantiationException
c) IllegalAccessException
d) NoSuchMethodException

Answer:

b) InstantiationException

Explanation:

InstantiationException is thrown to indicate that the application tried to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated because it is an interface or is an abstract class.

27. What is the significance of the InterruptedException in Java?

a) It indicates that a thread is waiting, sleeping, or otherwise occupied
b) It is thrown when a thread is interrupted either before or during its activity
c) It signals that a method has been interrupted by a timeout
d) It is used to indicate that a thread has been paused

Answer:

b) It is thrown when a thread is interrupted either before or during its activity

Explanation:

InterruptedException is thrown when a thread that is sleeping, waiting, or otherwise paused is interrupted.

28. In Java, under which circumstance will the catch block for ArithmeticException catch a NullPointerException?

a) Always
b) Never, because NullPointerException is a different type of exception
c) When NullPointerException is thrown as a cause of ArithmeticException
d) When both exceptions have the same root cause

Answer:

b) Never, because NullPointerException is a different type of exception

Explanation:

Catch blocks are structured to catch specific exception types. A catch block for ArithmeticException will not catch NullPointerException, as they are different types of exceptions.

29. Which of these is a direct subclass of the Exception class (not RuntimeException)?

a) ArithmeticException
b) NullPointerException
c) IOException
d) IndexOutOfBoundsException

Answer:

c) IOException

Explanation:

IOException is a subclass of Exception and is not a RuntimeException. It's a checked exception that must be either caught or declared in the method signature.

30. What is the output of a program that catches an IOException, but an ArithmeticException is thrown?

a) The IOException catch block will handle it
b) The ArithmeticException will not be caught and will propagate up the call stack
c) A runtime error will occur
d) The program will terminate immediately

Answer:

b) The ArithmeticException will not be caught and will propagate up the call stack

Explanation:

If an exception is thrown that does not match the type caught by the catch block, the catch block will not execute, and the exception will propagate up the call stack.

31. How does the try-with-resources statement benefit exception handling in Java?

a) It automatically handles all exceptions
b) It provides a simpler syntax for handling multiple exceptions
c) It automatically closes resources after execution
d) It reduces the need for a finally block

Answer:

c) It automatically closes resources after execution

Explanation:

The try-with-resources statement automatically closes the resources (like files, database connections) used within the try block, eliminating the need for a finally block to close these resources.

32. What will be the result of compiling and running a Java program that has a try block with an unreachable catch block for ArithmeticException?

a) Compilation error due to unreachable code
b) Runtime exception
c) The catch block for ArithmeticException will be ignored
d) The program will run without any errors

Answer:

d) The program will run without any errors

Explanation:

An unreachable catch block (e.g., when there's no code in the try block that can throw the caught exception) will not cause a compilation error. The program will compile and run normally, just not entering the catch block.

33. Which statement is true about the Error class in Java?

a) It is a subclass of Exception
b) It is meant to be caught and handled by the application
c) It derives from the Throwable class
d) It is used for compile-time errors

Answer:

c) It derives from the Throwable class

Explanation:

Error is a subclass of Throwable, like Exception. Errors indicate serious problems and are not meant to be caught and handled by applications.

34. What is the effect of declaring an exception in the throws clause of a method signature?

a) It will handle the exception
b) It forces the calling method to handle the exception
c) It prevents the exception from being thrown
d) It automatically catches the exception within the method

Answer:

b) It forces the calling method to handle the exception

Explanation:

Declaring an exception in the throws clause of a method signature does not handle the exception within the method. Instead, it indicates that the method may throw this exception, and the responsibility to handle it is passed to the method that calls this method.

35. Which among the following best describes a checked exception?

a) An exception that is checked at compile-time for a smooth runtime
b) An exception that occurs at runtime and is not checked by the compiler
c) Any instance of Throwable that is not an error
d) An exception explicitly thrown using the throw keyword

Answer:

a) An exception that is checked at compile-time for a smooth runtime

Explanation:

Checked exceptions are exceptions that are checked at compile-time, ensuring that they are either caught or declared in the method signature. This helps in reducing runtime errors.

36. In a method declaration, what does it mean to use 'throws Exception'?

a) The method is capable of causing any Exception
b) The method will definitely throw an Exception
c) The method will handle all Exceptions
d) The method might throw any checked exception

Answer:

d) The method might throw any checked exception

Explanation:

Using 'throws Exception' in a method declaration signals that the method might throw any checked exception during its execution.

37. Which method is used to retrieve the detailed message of an exception?

a) printStackTrace()
b) getMessage()
c) getCause()
d) toString()

Answer:

b) getMessage()

Explanation:

The getMessage() method is used to retrieve the detailed message of the Throwable object, which can provide more information about the exception.

38. What is the primary difference between throw and throws in Java?

a) throw is a keyword, throws is a method
b) throw throws an exception, throws declares an exception
c) throw is used in method definition, throws in method declaration
d) There is no difference

Answer:

b) throw throws an exception, throws declares an exception

Explanation:

The throw keyword is used to actually throw an exception, whereas throws is used in a method declaration to indicate that the method might throw the specified exceptions.

39. What will happen if a try block is followed by multiple catch blocks?

a) Compilation error
b) Only the first catch block is executed
c) Each catch block is checked in order and the first matching catch block is executed
d) All matching catch blocks are executed

Answer:

c) Each catch block is checked in order and the first matching catch block is executed

Explanation:

When a try block is followed by multiple catch blocks, each catch block is checked in the order they appear. The first catch block with a matching exception type will handle the exception, and the rest will be ignored.

40. What kind of exception is thrown by the JVM when a method call is made on a null reference?

a) NullPointerException
b) ArithmeticException
c) ClassCastException
d) ArrayIndexOutOfBoundsException

Answer:

a) NullPointerException

Explanation:

When a method call is made on a null reference, the JVM throws a NullPointerException.

41. What is the result of trying to compile a program with an empty try block and no catch or finally blocks?

a) Compilation error
b) Runtime exception
c) The program compiles and runs without error
d) The program compiles but throws an exception at runtime

Answer:

a) Compilation error

Explanation:

A try block without either a catch or finally block is not valid in Java and will result in a compilation error.

42. When should the 'throws' keyword be used in a method signature?

a) When the method definitely throws an exception
b) When the method could potentially throw a checked exception
c) When the method handles an exception
d) When the method rethrows a caught exception

Answer:

b) When the method could potentially throw a checked exception

Explanation:

The 'throws' keyword should be used in a method signature when the method might throw a checked exception. This informs the caller of the method that they need to handle or declare the exception.

43. What is the correct way to handle multiple exceptions of the same type in Java?

a) Using separate catch blocks for each exception
b) Using a single catch block with multiple exception types separated by commas
c) Using a single catch block with multiple exception types separated by the pipe (|) character
d) It is not possible to handle multiple exceptions of the same type

Answer:

c) Using a single catch block with multiple exception types separated by the pipe (|) character

Explanation:

Java 7 introduced the ability to catch multiple exceptions in a single catch block using the pipe (|) character to separate the exception types.

44. Which exception is thrown when an illegal operation is performed on an empty container, such as a Queue or a Stack?

a) EmptyStackException
b) NoSuchElementException
c) UnsupportedOperationException
d) IllegalStateException

Answer:

b) NoSuchElementException

Explanation:

NoSuchElementException is thrown when one tries to access or remove an element from an empty container like Queue or Stack.

45. What is the correct syntax to create a custom exception in Java?

a) class MyException extends RuntimeException {}
b) class MyException implements Exception {}
c) class MyException extends Exception {}
d) class MyException throws Exception {}

Answer:

c) class MyException extends Exception {}

Explanation:

To create a custom exception in Java, you extend either Exception (for checked exceptions) or RuntimeException (for unchecked exceptions).

46. What is the effect of an uncaught exception in the main thread of a Java application?

a) The application continues to run
b) Only the main thread terminates
c) The entire application shuts down
d) The exception is ignored

Answer:

c) The entire application shuts down

Explanation:

An uncaught exception in the main thread of a Java application causes the main thread to terminate, which generally results in the termination of the application.

47. Which of the following is not a purpose of the finally block in Java exception handling?

a) Cleaning up resources
b) Catching exceptions
c) Executing code regardless of whether an exception occurs
d) Closing database connections

Answer:

b) Catching exceptions

Explanation:

The finally block is not for catching exceptions but for executing code regardless of whether an exception occurs, typically used for cleaning up resources like closing file streams or database connections.

34. What is the effect of declaring an exception in the throws clause of a method signature?

a) It will handle the exception
b) It forces the calling method to handle the exception
c) It prevents the exception from being thrown
d) It automatically catches the exception within the method

Answer:

b) It forces the calling method to handle the exception

Explanation:

Declaring an exception in the throws clause of a method signature does not handle the exception within the method. Instead, it indicates that the method may throw this exception, and the responsibility to handle it is passed to the method that calls this method.

35. Which among the following best describes a checked exception?

a) An exception that is checked at compile-time for a smooth runtime
b) An exception that occurs at runtime and is not checked by the compiler
c) Any instance of Throwable that is not an error
d) An exception explicitly thrown using the throw keyword

Answer:

a) An exception that is checked at compile-time for a smooth runtime

Explanation:

Checked exceptions are exceptions that are checked at compile-time, ensuring that they are either caught or declared in the method signature. This helps in reducing runtime errors.

36. In a method declaration, what does it mean to use 'throws Exception'?

a) The method is capable of causing any Exception
b) The method will definitely throw an Exception
c) The method will handle all Exceptions
d) The method might throw any checked exception

Answer:

d) The method might throw any checked exception

Explanation:

Using 'throws Exception' in a method declaration signals that the method might throw any checked exception during its execution.

37. Which method is used to retrieve the detailed message of an exception?

a) printStackTrace()
b) getMessage()
c) getCause()
d) toString()

Answer:

b) getMessage()

Explanation:

The getMessage() method is used to retrieve the detailed message of the Throwable object, which can provide more information about the exception.

38. What is the primary difference between throw and throws in Java?

a) throw is a keyword, throws is a method
b) throw throws an exception, throws declares an exception
c) throw is used in method definition, throws in method declaration
d) There is no difference

Answer:

b) throw throws an exception, throws declares an exception

Explanation:

The throw keyword is used to actually throw an exception, whereas throws is used in a method declaration to indicate that the method might throw the specified exceptions.

39. What will happen if a try block is followed by multiple catch blocks?

a) Compilation error
b) Only the first catch block is executed
c) Each catch block is checked in order and the first matching catch block is executed
d) All matching catch blocks are executed

Answer:

c) Each catch block is checked in order and the first matching catch block is executed

Explanation:

When a try block is followed by multiple catch blocks, each catch block is checked in the order they appear. The first catch block with a matching exception type will handle the exception, and the rest will be ignored.

40. What kind of exception is thrown by the JVM when a method call is made on a null reference?

a) NullPointerException
b) ArithmeticException
c) ClassCastException
d) ArrayIndexOutOfBoundsException

Answer:

a) NullPointerException

Explanation:

When a method call is made on a null reference, the JVM throws a NullPointerException.

41. What is the result of trying to compile a program with an empty try block and no catch or finally blocks?

a) Compilation error
b) Runtime exception
c) The program compiles and runs without error
d) The program compiles but throws an exception at runtime

Answer:

a) Compilation error

Explanation:

A try block without either a catch or finally block is not valid in Java and will result in a compilation error.

42. When should the 'throws' keyword be used in a method signature?

a) When the method definitely throws an exception
b) When the method could potentially throw a checked exception
c) When the method handles an exception
d) When the method rethrows a caught exception

Answer:

b) When the method could potentially throw a checked exception

Explanation:

The 'throws' keyword should be used in a method signature when the method might throw a checked exception. This informs the caller of the method that they need to handle or declare the exception.

43. What is the correct way to handle multiple exceptions of the same type in Java?

a) Using separate catch blocks for each exception
b) Using a single catch block with multiple exception types separated by commas
c) Using a single catch block with multiple exception types separated by the pipe (|) character
d) It is not possible to handle multiple exceptions of the same type

Answer:

c) Using a single catch block with multiple exception types separated by the pipe (|) character

Explanation:

Java 7 introduced the ability to catch multiple exceptions in a single catch block using the pipe (|) character to separate the exception types.

44. Which exception is thrown when an illegal operation is performed on an empty container, such as a Queue or a Stack?

a) EmptyStackException
b) NoSuchElementException
c) UnsupportedOperationException
d) IllegalStateException

Answer:

b) NoSuchElementException

Explanation:

NoSuchElementException is thrown when one tries to access or remove an element from an empty container like Queue or Stack.

45. What is the correct syntax to create a custom exception in Java?

a) class MyException extends RuntimeException {}
b) class MyException implements Exception {}
c) class MyException extends Exception {}
d) class MyException throws Exception {}

Answer:

c) class MyException extends Exception {}

Explanation:

To create a custom exception in Java, you extend either Exception (for checked exceptions) or RuntimeException (for unchecked exceptions).

46. What is the effect of an uncaught exception in the main thread of a Java application?

a) The application continues to run
b) Only the main thread terminates
c) The entire application shuts down
d) The exception is ignored

Answer:

c) The entire application shuts down

Explanation:

An uncaught exception in the main thread of a Java application causes the main thread to terminate, which generally results in the termination of the application.

47. Which of the following is not a purpose of the finally block in Java exception handling?

a) Cleaning up resources
b) Catching exceptions
c) Executing code regardless of whether an exception occurs
d) Closing database connections

Answer:

b) Catching exceptions

Explanation:

The finally block is not for catching exceptions but for executing code regardless of whether an exception occurs, typically used for cleaning up resources like closing file streams or database connections.

48. What is the result of catching an Exception type before catching its subclass type?

a) Compilation error due to unreachable code
b) The subclass exception will be caught first
c) The Exception type catch block will catch all exceptions
d) A runtime error will occur

Answer:

a) Compilation error due to unreachable code

Explanation:

If an Exception type (superclass) is caught before its subclass, the subclass catch block becomes unreachable, leading to a compilation error.

49. What kind of exceptions are subclasses of RuntimeException in Java?

a) Checked exceptions
b) Unchecked exceptions
c) Compile-time exceptions
d) None of the above

Answer:

b) Unchecked exceptions

Explanation:

Subclasses of RuntimeException are unchecked exceptions, meaning the compiler does not require them to be declared or caught.

50. Which method in Java is used to manually cause an exception during testing?

a) System.throwException()
b) Thread.throwException()
c) throw new Exception()
d) Exception.throw()

Answer:

c) throw new Exception()

Explanation:

The throw keyword followed by a new exception instance (e.g., 'throw new Exception()') is used to manually trigger an exception, often used in testing or to indicate a special error condition.

Comments