Java Exception Handling MCQs

1 min read

Java exception handling is a powerful mechanism designed to manage runtime errors and maintain normal application flow. It relies on five core keywords try, catch, finally, throw, and throws to isolate risky code, catch specific error types, and guarantee resource cleanup. Exceptions are divided into checked exceptions (verified at compile-time) and unchecked exceptions (inheriting from RuntimeException), sitting beneath the universal Throwable superclass. Mastering exception hierarchy and proper propagation is essential for writing robust code and clearing technical certification exams.

1. Which of the following is the root class for all errors and exceptions in Java?

a) Exception
b) Throwable
c) Error
d) RuntimeException
Correct Answer: b) Throwable
Explanation:
The java.lang.Throwable class is the root class of the Java Exception hierarchy. Both Exception and Error classes inherit from it.

2. Which keyword is used to explicitly throw an exception in Java?

a) throws
b) throw
c) try
d) catch
Correct Answer: b) throw
Explanation:
The 'throw' keyword is used to explicitly throw a single exception from within a method or block of code.

3. Which of these keywords is used to declare that a method might throw an exception?

a) throws
b) throw
c) catch
d) finally
Correct Answer: a) throws
Explanation:
The 'throws' keyword is used in a method signature to declare the exceptions that can occur during the execution of the program.

4. What is the primary difference between checked and unchecked exceptions?

a) Checked exceptions are checked at runtime, unchecked at compile time.
b) Checked exceptions are checked at compile time, unchecked at runtime.
c) Checked exceptions extend RuntimeException, unchecked extend Exception.
d) There is no difference between them.
Correct Answer: b) Checked exceptions are checked at compile time, unchecked at runtime.
Explanation:
Checked exceptions must be handled or declared at compile time, whereas unchecked exceptions occur at runtime and do not require explicit handling.

5. Which of the following classes is considered an unchecked exception?

a) IOException
b) SQLException
c) NullPointerException
d) ClassNotFoundException
Correct Answer: c) NullPointerException
Explanation:
NullPointerException inherits from RuntimeException, making it an unchecked exception.

6. What will happen if a try block does not have a catch block or a finally block?

a) It will compile successfully but throw a runtime error.
b) It will cause a compile-time error.
c) It will execute normally ignoring exceptions.
d) It automatically generates a default catch block.
Correct Answer: b) It will cause a compile-time error.
Explanation:
A try block must be followed by at least one catch block or a finally block; otherwise, a compile-time error occurs.

7. Which block is always executed whether an exception occurs or not?

a) try
b) catch
c) finally
d) throw
Correct Answer: c) finally
Explanation:
The finally block is used to execute crucial code such as closing resources. It executes regardless of whether an exception is thrown or caught.

8. In which scenario will the finally block NOT execute?

a) When an exception is thrown but not caught.
b) When System.exit() is called in the try or catch block.
c) When a return statement is encountered in the try block.
d) When the catch block throws another exception.
Correct Answer: b) When System.exit() is called in the try or catch block.
Explanation:
Calling System.exit() terminates the JVM completely, preventing the finally block from executing.

9. When having multiple catch blocks, what must be the order of exception types?

a) Most general to most specific.
b) Most specific to most general.
c) Alphabetical order.
d) Any order is fine.
Correct Answer: b) Most specific to most general.
Explanation:
Multiple catch blocks must be ordered from the most specific subclass exception to the most general superclass exception; otherwise, it causes a compile-time error.

10. Which exception is thrown when an array is accessed with an illegal index?

a) ArrayIndexOutOfBoundsException
b) IndexOutOfBoundsException
c) NullPointerException
d) IllegalArgumentException
Correct Answer: a) ArrayIndexOutOfBoundsException
Explanation:
ArrayIndexOutOfBoundsException is thrown to indicate that an array has been accessed with an illegal index.

11. What is the purpose of the try-with-resources statement introduced in Java 7?

a) To optimize try block performance.
b) To automatically catch all exceptions.
c) To ensure that each resource is closed at the end of the statement.
d) To avoid writing catch blocks entirely.
Correct Answer: c) To ensure that each resource is closed at the end of the statement.
Explanation:
Try-with-resources ensures that any resource that implements java.lang.AutoCloseable is closed automatically when the try block finishes.

12. Which exception is thrown when a division by zero occurs in integer arithmetic?

a) NumberFormatException
b) ArithmeticException
c) DivideByZeroException
d) IllegalArgumentException
Correct Answer: b) ArithmeticException
Explanation:
ArithmeticException is thrown when an exceptional arithmetic condition has occurred, such as integer division by zero.

13. How can you create a custom exception in Java?

a) By implementing the Exception interface.
b) By extending the Exception or RuntimeException class.
c) By using the @Exception annotation.
d) By extending the Error class.
Correct Answer: b) By extending the Exception or RuntimeException class.
Explanation:
Custom exceptions are created by extending the existing java.lang.Exception class or java.lang.RuntimeException class.

14. Which method of the Throwable class is used to print the exception details along with line numbers?

a) getMessage()
b) toString()
c) printStackTrace()
d) getStackTrace()
Correct Answer: c) printStackTrace()
Explanation:
printStackTrace() prints the throwable and its backtrace to the standard error stream, showing exactly where the exception occurred.

15. Can we have an empty catch block?

a) Yes, but it is considered bad practice.
b) No, it causes a compile-time error.
c) No, it causes a runtime error.
d) Yes, and it is the recommended way to ignore exceptions.
Correct Answer: a) Yes, but it is considered bad practice.
Explanation:
Java allows empty catch blocks. However, swallowing exceptions without logging or handling them is a bad practice.

16. Which of these is not a standard exception in Java?

a) NullPointerException
b) StringIndexOutOfBoundsException
c) MemoryOutOfBoundsException
d) IllegalArgumentException
Correct Answer: c) MemoryOutOfBoundsException
Explanation:
MemoryOutOfBoundsException does not exist in standard Java. The closest standard error is OutOfMemoryError.

17. If a method throws a checked exception, what must the caller do?

a) Ignore it.
b) Handle it using try-catch or declare it using throws.
c) Convert it to an Error.
d) Rethrow it automatically.
Correct Answer: b) Handle it using try-catch or declare it using throws.
Explanation:
Checked exceptions enforce the handle-or-declare rule. The caller must either surround the call in a try-catch block or add a throws declaration.

18. Is it mandatory for a try block to have a catch block if it has a finally block?

a) Yes
b) No
c) Only for checked exceptions
d) Only for unchecked exceptions
Correct Answer: b) No
Explanation:
A try block can be followed by just a finally block. A catch block is not mandatory if finally is present.

19. Which of the following best describes an Error in Java?

a) A mild issue that can be easily recovered from.
b) A condition that a reasonable application should try to catch.
c) A severe problem that a reasonable application should not try to catch.
d) A bug in the programmer's code logic.
Correct Answer: c) A severe problem that a reasonable application should not try to catch.
Explanation:
Errors represent severe, generally unrecoverable conditions in the JVM environment.

20. Which Java 7 feature allows catching multiple exceptions in a single catch block?

a) Multi-catch using the comma operator.
b) Multi-catch using the pipe (|) operator.
c) The generic CatchAll block.
d) Multi-catch using the ampersand (&) operator.
Correct Answer: b) Multi-catch using the pipe (|) operator.
Explanation:
Java 7 introduced the multi-catch block feature where multiple exceptions can be caught using the pipe (|) operator.

21. What is exception propagation?

a) Automatically resolving an exception.
b) Forwarding an exception to the calling method if not handled locally.
c) Converting a checked exception into an unchecked exception.
d) Sending an exception object over a network.
Correct Answer: b) Forwarding an exception to the calling method if not handled locally.
Explanation:
Exception propagation is the process where an unhandled exception drops down the call stack until it finds an appropriate catch block.

22. Which types of exceptions are propagated by default?

a) Checked exceptions only.
b) Unchecked exceptions only.
c) Both checked and unchecked exceptions.
d) Neither checked nor unchecked exceptions.
Correct Answer: b) Unchecked exceptions only.
Explanation:
By default, unchecked exceptions are automatically propagated up the call stack. Checked exceptions must be explicitly propagated.

23. What happens if an exception is thrown in a finally block?

a) It is ignored.
b) It overrides any exception thrown in the try or catch block.
c) The program crashes immediately without propagation.
d) It gets logged but execution continues.
Correct Answer: b) It overrides any exception thrown in the try or catch block.
Explanation:
If a finally block throws an exception, it masks any exception that was previously thrown in the try or catch block.

24. Can a finally block contain a return statement?

a) Yes, and it overrides any return statement in the try or catch block.
b) No, it causes a compile-time error.
c) Yes, but it is ignored.
d) No, it causes a runtime error.
Correct Answer: a) Yes, and it overrides any return statement in the try or catch block.
Explanation:
Placing a return in a finally block will suppress any unhandled exceptions and override any previous return values.

25. Which of these exceptions is an example of an Error?

a) ClassNotFoundException
b) StackOverflowError
c) NullPointerException
d) ArithmeticException
Correct Answer: b) StackOverflowError
Explanation:
StackOverflowError extends VirtualMachineError, which extends Error. The others extend Exception.

26. What does the getMessage() method of Throwable return?

a) The full stack trace.
b) The name of the exception class.
c) The detailed string message of the throwable.
d) The line number where the exception occurred.
Correct Answer: c) The detailed string message of the throwable.
Explanation:
getMessage() returns the detail message string that was provided when the exception object was created.

27. If a subclass overrides a method that throws a checked exception, what rule applies to the overriding method?

a) It must throw the exact same exception.
b) It can throw any exception.
c) It cannot throw any new or broader checked exceptions.
d) It cannot throw any exceptions at all.
Correct Answer: c) It cannot throw any new or broader checked exceptions.
Explanation:
An overriding method can only throw the same checked exceptions, narrower checked exceptions, or unchecked exceptions.

28. Can we throw an exception manually using the 'throw' keyword in a catch block?

a) Yes, it is called exception rethrowing.
b) No, it causes a compile-time error.
c) No, throws must be used instead.
d) Yes, but only unchecked exceptions.
Correct Answer: a) Yes, it is called exception rethrowing.
Explanation:
You can use the 'throw' keyword inside a catch block to rethrow the caught exception or throw a completely new one.

29. Which of the following exceptions occurs when trying to parse an invalid string into a number?

a) IllegalFormatException
b) NumberFormatException
c) ArithmeticException
d) ClassCastException
Correct Answer: b) NumberFormatException
Explanation:
NumberFormatException is thrown by methods like Integer.parseInt() when the string does not have the appropriate format.

30. Which of these is a valid signature for a main method that doesn't handle its exceptions?

a) public static void main(String[] args) throw Exception
b) public static void main(String[] args) throws Exception
c) public void main(String[] args) throws Exception
d) public static void main(String[] args) catching Exception
Correct Answer: b) public static void main(String[] args) throws Exception
Explanation:
The 'throws' keyword must be used in the method signature to declare that the main method propagates exceptions.

31. What is the result of using a single multi-catch block for exceptions that have a parent-child relationship?

a) It compiles perfectly.
b) It causes a compile-time error.
c) It causes a runtime error.
d) Only the parent exception is caught.
Correct Answer: b) It causes a compile-time error.
Explanation:
In a multi-catch block, the exception alternatives cannot have a subclass-superclass relationship. Doing so results in a compilation error.

32. When does a ClassNotFoundException occur?

a) When attempting to use an object that is null.
b) When the JVM tries to load a class and cannot find its definition.
c) When casting an object to a class of which it is not an instance.
d) When a required import statement is missing at compile time.
Correct Answer: b) When the JVM tries to load a class and cannot find its definition.
Explanation:
ClassNotFoundException is thrown when an application tries to load in a class through its string name but no definition could be found.

33. Which exception is thrown when you try to cast an object to a subclass of which it is not an instance?

a) TypeCastException
b) ClassCastException
c) InvalidCastException
d) IllegalCastException
Correct Answer: b) ClassCastException
Explanation:
ClassCastException is a runtime exception thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

34. Can we define multiple finally blocks for a single try block?

a) Yes
b) No
c) Only if multiple catch blocks are present.
d) Only in nested try blocks.
Correct Answer: b) No
Explanation:
A single try block can have at most one finally block associated with it.

35. What happens if a variable is declared inside a try block?

a) It is accessible everywhere.
b) It is accessible in the catch block.
c) It is accessible only within the try block.
d) It causes a compile error.
Correct Answer: c) It is accessible only within the try block.
Explanation:
Variables declared inside a try block have local scope strictly limited to that try block.

36. Which interface must a resource implement to be used in a try-with-resources statement?

a) Serializable
b) Closeable or AutoCloseable
c) Cloneable
d) Flushable
Correct Answer: b) Closeable or AutoCloseable
Explanation:
Resources used in try-with-resources must implement java.lang.AutoCloseable (or java.io.Closeable).

37. What is an 'Exception Chaining' in Java?

a) Having multiple catch blocks.
b) Nesting try-catch blocks.
c) Associating one exception with another by passing it to the constructor.
d) Throwing an exception from a finally block.
Correct Answer: c) Associating one exception with another by passing it to the constructor.
Explanation:
Exception chaining relates one exception to another (the 'cause'). It is done by passing the original exception to the constructor of the new exception.

38. Which method retrieves the original cause of a chained exception?

a) getOriginal()
b) getCause()
c) getReason()
d) getRoot()
Correct Answer: b) getCause()
Explanation:
The getCause() method of Throwable returns the cause of this throwable or null if the cause is nonexistent or unknown.

39. Which exception is thrown when an application attempts to use null in a case where an object is required?

a) NullPointerException
b) NullReferenceException
c) IllegalArgumentException
d) ObjectNotFoundException
Correct Answer: a) NullPointerException
Explanation:
NullPointerException is a runtime exception thrown when attempting to invoke a method or access a field on a null reference.

40. What exception is thrown when a thread is interrupted while sleeping?

a) ThreadDeath
b) IllegalThreadStateException
c) InterruptedException
d) ThreadInterruptedException
Correct Answer: c) InterruptedException
Explanation:
InterruptedException is a checked exception thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted.

41. Is OutOfMemoryError a checked or unchecked type?

a) Checked exception
b) Unchecked exception
c) Error (which acts like unchecked)
d) It is neither, it is a hardware failure.
Correct Answer: c) Error (which acts like unchecked)
Explanation:
OutOfMemoryError extends Error. Errors are unchecked, meaning the compiler does not force you to handle or declare them.

42. Can you catch an Error in Java?

a) Yes, but it is highly discouraged.
b) No, it is impossible.
c) Yes, but only if it is a VirtualMachineError.
d) No, the compiler will prevent it.
Correct Answer: a) Yes, but it is highly discouraged.
Explanation:
Technically, because Error extends Throwable, you can catch it. However, it is bad practice because Errors represent unrecoverable states.

43. If you throw a checked exception from a method, you must either handle it or use the keyword ____.

a) throw
b) throws
c) finally
d) volatile
Correct Answer: b) throws
Explanation:
If a checked exception isn't handled within the method using try-catch, the method signature must include the 'throws' keyword.

44. Which of these statements about the throw keyword is true?

a) It can be used to throw multiple exceptions simultaneously.
b) It is followed by a class name, not an instance.
c) It is used to throw an exception instance explicitly.
d) It is only used in method signatures.
Correct Answer: c) It is used to throw an exception instance explicitly.
Explanation:
The 'throw' keyword is followed by an instance of Throwable. It cannot throw multiple exceptions at once.

45. What is a major advantage of Java Exception Handling?

a) It increases the execution speed of the program.
b) It separates error handling code from regular business logic.
c) It automatically fixes the errors.
d) It eliminates the need for testing.
Correct Answer: b) It separates error handling code from regular business logic.
Explanation:
Exception handling provides a clean separation between normal execution code and error-handling code.

46. Which runtime exception is thrown if a method receives an argument formatted differently than expected?

a) IllegalArgumentException
b) IllegalStateException
c) NumberFormatException
d) InvalidParameterException
Correct Answer: a) IllegalArgumentException
Explanation:
IllegalArgumentException is thrown to indicate that a method has been passed an illegal or inappropriate argument.

47. What exception is thrown by the charAt() method of the String class when the index is out of bounds?

a) ArrayIndexOutOfBoundsException
b) IndexOutOfBoundsException
c) StringIndexOutOfBoundsException
d) CharacterOutOfBoundsException
Correct Answer: c) StringIndexOutOfBoundsException
Explanation:
StringIndexOutOfBoundsException is specifically thrown by String methods to indicate an index is out of bounds.

48. Is it possible to have nested try-catch blocks?

a) Yes
b) No
c) Only if there is no finally block.
d) Only inside the catch block, not the try block.
Correct Answer: a) Yes
Explanation:
Java fully supports nesting try-catch blocks. A try block can be placed inside another try block, catch block, or finally block.

49. When overriding a method, if the superclass method does not declare an exception, can the subclass method declare a new checked exception?

a) Yes
b) No
c) Only if it is IOException.
d) Yes, but it must be handled inside.
Correct Answer: b) No
Explanation:
If the superclass method does not declare any checked exceptions, the overriding method in the subclass cannot declare any new checked exceptions.

50. What is the output of returning a value in the try block and a different value in the finally block?

a) The value from the try block is returned.
b) The value from the finally block is returned.
c) A compilation error occurs.
d) An exception is thrown.
Correct Answer: b) The value from the finally block is returned.
Explanation:
The finally block executes last before control is transferred. Its return statement will override any return statement executed in the try block.

51. Which of the following is thrown when a file requested by FileReader is not found?

a) FileNotFoundException
b) IOException
c) EOFException
d) FileMissingException
Correct Answer: a) FileNotFoundException
Explanation:
FileNotFoundException is thrown when an attempt to open the file denoted by a specified pathname has failed.

52. What is 'Exception Suppression' in the context of try-with-resources?

a) When the compiler ignores exceptions.
b) When an exception in the try block suppresses an exception thrown by the close() method.
c) When a finally block suppresses the try block's return.
d) When JVM stops logging exceptions.
Correct Answer: b) When an exception in the try block suppresses an exception thrown by the close() method.
Explanation:
In try-with-resources, if the try block throws an exception and the close() method also throws an exception, the latter is 'suppressed'.

53. How do you retrieve suppressed exceptions in a try-with-resources structure?

a) Using getCause()
b) Using getOriginal()
c) Using getSuppressed()
d) Using getHidden()
Correct Answer: c) Using getSuppressed()
Explanation:
The getSuppressed() method of Throwable returns an array containing all exceptions that were suppressed by the try-with-resources statement.

54. Which exception is thrown when an application attempts to perform a networking operation but it times out?

a) SocketTimeoutException
b) ConnectException
c) UnknownHostException
d) NetworkException
Correct Answer: a) SocketTimeoutException
Explanation:
SocketTimeoutException is a subclass of InterruptedIOException that signals a timeout occurred on a socket read or accept.

55. What type of exception is IllegalThreadStateException?

a) Checked Exception
b) Error
c) Unchecked Exception
d) Compilation Error
Correct Answer: c) Unchecked Exception
Explanation:
IllegalThreadStateException extends IllegalArgumentException, which extends RuntimeException, making it an unchecked exception.

56. Can we declare a method to throw multiple exceptions?

a) No, a method can only throw one exception type.
b) Yes, separated by commas.
c) Yes, separated by pipes (|).
d) Only if they belong to the same hierarchy.
Correct Answer: b) Yes, separated by commas.
Explanation:
A method signature can declare multiple exceptions using the 'throws' keyword by separating them with commas.

57. Which base class must a custom exception extend if you want the compiler to force the caller to handle it?

a) RuntimeException
b) Exception
c) Error
d) Throwable
Correct Answer: b) Exception
Explanation:
Extending 'Exception' (and not RuntimeException) creates a checked exception, which the compiler forces the programmer to handle or declare.

58. Which of these blocks cannot exist without a try block?

a) catch
b) finally
c) Both catch and finally
d) Neither, they can exist independently
Correct Answer: c) Both catch and finally
Explanation:
Both catch and finally blocks are explicitly tied to a try block and cannot stand alone in Java code.

59. If an unchecked exception occurs during object initialization, what typically happens if not handled?

a) The object is created but with null fields.
b) The thread executing the initialization terminates.
c) The garbage collector handles it silently.
d) The compiler ignores it.
Correct Answer: b) The thread executing the initialization terminates.
Explanation:
If an unhandled RuntimeException is thrown during execution, it propagates up the stack and terminates the executing thread.

60. What is the effect of throwing an exception in a constructor?

a) It causes a compilation error.
b) It prevents the object from being fully initialized.
c) The object is initialized but immediately garbage collected.
d) Constructors cannot throw exceptions.
Correct Answer: b) It prevents the object from being fully initialized.
Explanation:
If a constructor throws an exception, the object creation fails, and the reference to the partially constructed object is not returned.

61. Which error is thrown when the Java Virtual Machine runs out of memory?

a) MemoryLimitExceededException
b) OutOfMemoryError
c) StackOverflowError
d) HeapSpaceException
Correct Answer: b) OutOfMemoryError
Explanation:
OutOfMemoryError is thrown when the JVM cannot allocate an object because it is out of memory.

62. Is there a standard EmptyStringException in Java?

a) Yes, it extends RuntimeException.
b) Yes, it extends Exception.
c) No, IllegalArgumentException is typically used instead.
d) No, NullPointerException covers this.
Correct Answer: c) No, IllegalArgumentException is typically used instead.
Explanation:
Java does not have an EmptyStringException. Developers typically throw IllegalArgumentException if a string is unexpectedly empty.

63. How do you define a try-with-resources statement?

a) try { Resource r = new Resource(); }
b) try (Resource r = new Resource()) { }
c) try (Resource r = new Resource();) finally { }
d) try-with (Resource r = new Resource()) { }
Correct Answer: b) try (Resource r = new Resource()) { }
Explanation:
The syntax for try-with-resources uses parentheses directly after the 'try' keyword to declare the resources.

64. What will happen if a multi-catch block variable is reassigned inside the catch block?

a) It works normally.
b) It throws a runtime exception.
c) It causes a compile-time error.
d) It suppresses the original exception.
Correct Answer: c) It causes a compile-time error.
Explanation:
The catch parameter in a multi-catch block is implicitly final. Assigning a new value to it results in a compile-time error.

65. What is the purpose of the assert keyword regarding exception handling?

a) It throws an AssertionError if a boolean condition evaluates to false.
b) It asserts that an exception must be thrown.
c) It ignores all exceptions in the block.
d) It replaces the try-catch mechanism.
Correct Answer: a) It throws an AssertionError if a boolean condition evaluates to false.
Explanation:
The assert statement tests a condition; if the condition is false, it throws an AssertionError.

66. Which exception is thrown when an input/output operation fails or is interrupted?

a) IOException
b) FileSystemException
c) FileException
d) StreamException
Correct Answer: a) IOException
Explanation:
IOException is the base class for exceptions produced by failed or interrupted I/O operations.

67. Which class is thrown when a method is invoked at an illegal or inappropriate time?

a) IllegalArgumentException
b) IllegalStateException
c) InvalidTimeException
d) UnsupportedOperationException
Correct Answer: b) IllegalStateException
Explanation:
IllegalStateException signals that a method has been invoked at an illegal or inappropriate time.

68. Is it required to catch a RuntimeException?

a) Yes, the compiler enforces it.
b) No, they are unchecked exceptions.
c) Only in strictfp methods.
d) Only when working with file I/O.
Correct Answer: b) No, they are unchecked exceptions.
Explanation:
RuntimeExceptions and their subclasses are unchecked exceptions. The compiler does not require them to be caught or declared.

69. Can an abstract method declare a throws clause?

a) Yes
b) No
c) Only for unchecked exceptions.
d) Only in interfaces, not in abstract classes.
Correct Answer: a) Yes
Explanation:
Abstract methods can declare a 'throws' clause to enforce that implementations handle or declare specific exceptions.

70. Which exception is thrown by the JVM when a recursive method calls itself endlessly?

a) InfiniteLoopException
b) OutOfMemoryError
c) StackOverflowError
d) ThreadDeath
Correct Answer: c) StackOverflowError
Explanation:
Deep or infinite recursion causes the application to exhaust the call stack, leading the JVM to throw a StackOverflowError.

71. Which of these methods is defined in the Throwable class?

a) fillInStackTrace()
b) clearStackTrace()
c) printMessage()
d) getLineNumber()
Correct Answer: a) fillInStackTrace()
Explanation:
fillInStackTrace() is a method in Throwable that records the current state of the stack frames within the Throwable object.

72. If a method throws NullPointerException and ArithmeticException, what is the best way to catch both?

a) catch (Exception e)
b) catch (NullPointerException, ArithmeticException e)
c) catch (NullPointerException | ArithmeticException e)
d) catch (RuntimeException e)
Correct Answer: c) catch (NullPointerException | ArithmeticException e)
Explanation:
Using multi-catch (pipe operator |) is the best and most precise way to handle distinct exceptions in a single block.

73. Which of the following causes an UnsupportedOperationException?

a) Trying to divide by zero.
b) Adding an element to an unmodifiable collection.
c) Casting an Integer to a String.
d) Invoking a private method.
Correct Answer: b) Adding an element to an unmodifiable collection.
Explanation:
UnsupportedOperationException is often thrown by collections created via Collections.unmodifiableList() when mutative operations are attempted.

74. In try-with-resources, in what order are resources closed?

a) In the order they were declared.
b) In reverse order of their creation.
c) Random order depending on garbage collection.
d) Alphabetically by variable name.
Correct Answer: b) In reverse order of their creation.
Explanation:
Resources declared in a try-with-resources block are closed in the reverse order in which they were initialized.

75. What is the difference between throw and throws?

a) 'throw' is used for checked exceptions, 'throws' for unchecked.
b) 'throws' triggers the exception, 'throw' declares it.
c) 'throw' explicitly throws an exception instance, 'throws' declares that a method may throw exceptions.
d) There is no difference.
Correct Answer: c) 'throw' explicitly throws an exception instance, 'throws' declares that a method may throw exceptions.
Explanation:
'throw' is used to throw a specific exception object. 'throws' is used in the method signature to indicate propagated exceptions.

76. Which of these checked exceptions is related to Database operations?

a) SQLException
b) DataException
c) DatabaseError
d) IOException
Correct Answer: a) SQLException
Explanation:
SQLException is a checked exception that provides information on a database access error or other errors relating to JDBC.

77. Can you define a custom RuntimeException?

a) No, only standard ones are allowed.
b) Yes, by extending the RuntimeException class.
c) Yes, by extending the Exception class.
d) Yes, by extending the Error class.
Correct Answer: b) Yes, by extending the RuntimeException class.
Explanation:
Custom unchecked exceptions are created by subclassing RuntimeException.

78. When does NoSuchMethodError occur?

a) At compile time when a method does not exist.
b) At runtime when an application tries to call a method that no longer exists in the class definition.
c) When calling a private method.
d) When calling an overloaded method with wrong parameters.
Correct Answer: b) At runtime when an application tries to call a method that no longer exists in the class definition.
Explanation:
NoSuchMethodError is thrown if an application tries to call a specified method, but that method no longer exists in the linked class.

79. Which standard functional interface declares checked exceptions?

a) java.util.function.Function
b) java.util.function.Consumer
c) Runnable
d) None of them declare checked exceptions.
Correct Answer: d) None of them declare checked exceptions.
Explanation:
Standard functional interfaces in Java do not throw checked exceptions, requiring you to handle them internally.

80. What does a java.util.ConcurrentModificationException indicate?

a) Two threads modified a variable at the same time.
b) A collection was modified while iterating over it using a fail-fast iterator.
c) A database record was locked.
d) A class file was modified during execution.
Correct Answer: b) A collection was modified while iterating over it using a fail-fast iterator.
Explanation:
This exception is thrown when an object is modified concurrently while iterating over it, without using the iterator's own modification methods.

81. What is the return type of the getStackTrace() method in the Throwable class?

a) String
b) String[]
c) StackTraceElement[]
d) StackTrace
Correct Answer: c) StackTraceElement[]
Explanation:
getStackTrace() returns an array of StackTraceElement objects, each representing one stack frame.

82. Is it legal to put a try block inside a catch block?

a) Yes
b) No
c) Only if the catch block catches an Error.
d) Only if there is no finally block.
Correct Answer: a) Yes
Explanation:
Nesting try-catch blocks is valid in Java. You can place a try-catch block inside another try, catch, or finally block.

83. What is meant by the 'catch-or-declare' requirement in Java?

a) All exceptions must be caught.
b) Checked exceptions must be either caught in a catch block or declared in the throws clause.
c) Unchecked exceptions must be declared in the throws clause.
d) All errors must be caught.
Correct Answer: b) Checked exceptions must be either caught in a catch block or declared in the throws clause.
Explanation:
The Java compiler forces programmers to deal with checked exceptions by either handling them using try-catch or declaring them.

84. Which of the following statements is true for the 'finally' block?

a) It only executes if an exception is thrown.
b) It only executes if an exception is NOT thrown.
c) It executes regardless of whether an exception is thrown or caught.
d) It executes before the catch block.
Correct Answer: c) It executes regardless of whether an exception is thrown or caught.
Explanation:
The primary design of the finally block is to ensure that cleanup code runs unconditionally.

85. Which exception is thrown when an application tries to create an array with a negative size?

a) NegativeArraySizeException
b) IllegalArgumentException
c) ArrayIndexOutOfBoundsException
d) ArraySizeException
Correct Answer: a) NegativeArraySizeException
Explanation:
NegativeArraySizeException is thrown when an application attempts to instantiate an array with a negative size.

86. When overriding a method, can the subclass method drop the throws clause entirely if the superclass method declares a checked exception?

a) Yes
b) No
c) Only if it catches the exception internally.
d) Only for abstract methods.
Correct Answer: a) Yes
Explanation:
A subclass method is not required to throw the exceptions declared by the superclass method. It can handle them internally and drop the clause.

87. What occurs when you invoke a method on a variable that has a null value?

a) Compilation error
b) NullPointerException
c) NoClassDefFoundError
d) IllegalStateException
Correct Answer: b) NullPointerException
Explanation:
Invoking an instance method on a null reference throws a NullPointerException at runtime.

88. What is the output if you divide a double 5.0 by 0.0 in Java?

a) ArithmeticException
b) DoubleInfinityException
c) Infinity
d) 0.0
Correct Answer: c) Infinity
Explanation:
In Java, floating-point arithmetic handles division by zero by returning Infinity (or -Infinity, or NaN). ArithmeticException is only for integers.

89. Which class is the superclass of IOException?

a) RuntimeException
b) Exception
c) Error
d) Throwable
Correct Answer: b) Exception
Explanation:
IOException extends Exception, making it a checked exception.

90. Which of these keywords is completely invalid in Java exception handling?

a) catch
b) throw
c) thrown
d) throws
Correct Answer: c) thrown
Explanation:
'thrown' is not a keyword in Java. The valid keywords are try, catch, finally, throw, and throws.

91. What exception is thrown when the JVM is unable to find a class file during execution?

a) ClassNotFoundException
b) NoClassDefFoundError
c) LinkageError
d) ClassLoaderException
Correct Answer: b) NoClassDefFoundError
Explanation:
NoClassDefFoundError is an Error thrown if the JVM or ClassLoader tries to load in the definition of a class and no definition can be found.

92. How do you log an exception effectively in an enterprise Java application?

a) System.out.println(e);
b) e.printStackTrace();
c) Using a logging framework like Log4j or SLF4J.
d) Ignore it.
Correct Answer: c) Using a logging framework like Log4j or SLF4J.
Explanation:
Enterprise applications use logging frameworks to properly record, format, and route exception traces.

93. Can a single catch block handle exceptions that are not in the same inheritance tree?

a) Yes, using the multi-catch feature.
b) No, they must share a parent other than Exception.
c) Yes, but only if they are unchecked exceptions.
d) No, it is strictly forbidden.
Correct Answer: a) Yes, using the multi-catch feature.
Explanation:
The multi-catch feature was designed specifically to catch distinct exception types that do not have a subclass-superclass relationship.

94. What is the proper way to wrap a checked exception into an unchecked exception?

a) throw new RuntimeException(e);
b) throw (RuntimeException) e;
c) return new RuntimeException();
d) throws RuntimeException
Correct Answer: a) throw new RuntimeException(e);
Explanation:
To wrap a checked exception, you pass the caught checked exception as the 'cause' to the constructor of an unchecked exception.

95. Which exception is thrown when thread execution is paused using Thread.sleep() and another thread calls interrupt() on it?

a) IllegalMonitorStateException
b) ThreadDeath
c) InterruptedException
d) IllegalThreadStateException
Correct Answer: c) InterruptedException
Explanation:
Thread.sleep() declares that it throws InterruptedException, which is thrown if the sleeping thread's interrupt flag is set by another thread.

96. What does a FileNotFoundException inherit from?

a) RuntimeException
b) IOException
c) FileException
d) Error
Correct Answer: b) IOException
Explanation:
FileNotFoundException is a specific type of IOException, hence it inherits from it and is a checked exception.

97. If multiple catch blocks match an exception, which one gets executed?

a) The most general one.
b) The first one that matches the exception type.
c) All matching catch blocks execute.
d) The last matching catch block.
Correct Answer: b) The first one that matches the exception type.
Explanation:
Java checks catch blocks in the order they appear. The first catch block that matches the exception is executed, and the rest are ignored.

98. What is a 'resource' in the context of try-with-resources?

a) Any variable used inside the try block.
b) An object that must be closed after the program is finished with it.
c) CPU or memory usage.
d) A class extending the Resource interface.
Correct Answer: b) An object that must be closed after the program is finished with it.
Explanation:
A resource is an object (like a file stream or database connection) that needs explicit closing. It must implement AutoCloseable.

99. Can you cast an exception object to another exception type in a catch block?

a) Yes, if they are related in the inheritance hierarchy.
b) No, casting exceptions is not allowed.
c) Yes, any exception can be cast to any other exception.
d) Only unchecked exceptions can be cast.
Correct Answer: a) Yes, if they are related in the inheritance hierarchy.
Explanation:
Like any Java objects, exceptions can be cast to other types within their class hierarchy.

100. Which of these is a common strategy when catching an exception you cannot handle?

a) Print the stack trace and return null.
b) Leave the catch block empty.
c) Rethrow the exception or wrap it in a custom runtime exception.
d) Call System.exit(0).
Correct Answer: c) Rethrow the exception or wrap it in a custom runtime exception.
Explanation:
If a method cannot meaningfully resolve an error, the best practice is to propagate it upwards by rethrowing or wrapping it.
← Previous: Java Control Flow / If Else MCQs
Next →: Java Functions MCQs
Python Multithreading & Concurrency MCQs

Python Multithreading & Concurrency MCQs

Python multithreading is a core concurrency feature that enables execution of multiple threads within a single process to handle concurrent…

By MCQs Generator
Java Inheritance and Polymorphism MCQs

Java Inheritance and Polymorphism MCQs

Java inheritance and polymorphism form the bedrock of Object-Oriented Programming (OOP), enabling code reuse and flexible runtime behavior. Inheritance allows…

By MCQs Generator
JavaScript Closures MCQs

JavaScript Closures MCQs for Senior Developer Interviews

A closure in JavaScript is formed when an inner function retains access to the variables of its outer enclosing lexical…

By MCQs Generator