Java Exception Handling MCQ - Multiple Choice Questions And Answers
Q. What is an exception in Java?
A. An error in the codeB. A runtime issue
C. An unexpected event
D. All of the above
Q. Which keyword is used to handle exceptions in Java?
A. tryB. catch
C. throw
D. All of the above
Q. What does the finally block do in exception handling?
A. Handles the exceptionB. Always executes whether an exception occurs or not
C. Follows the try block
D. All of the above
Q. What is the purpose of the try-with-resources statement in Java?
A. To catch exceptionsB. To release resources automatically
C. To handle multiple exceptions
D. To create checked exceptions
Q. Which keyword is used to explicitly throw an exception in Java?
A. tryB. catch
C. throw
D. finally
Q. Can multiple catch blocks be used for a single try block in Java?
A. No, only one catch block is allowedB. Yes, but only for checked exceptions
C. Yes, for different types of exceptions
D. No, try block cannot catch exceptions
Q. What is the difference between checked and unchecked exceptions in Java?
A. Checked exceptions are caught at compile-time, while unchecked exceptions are caught at runtimeB. Checked exceptions are caught at runtime, while unchecked exceptions are caught at compile-time
C. Checked exceptions are explicitly declared in the code, while unchecked exceptions are not
D. Checked exceptions are related to I/O operations, while unchecked exceptions are related to logic errors
Q. When does a finally block not execute in Java?
A. When an exception occursB. When a catch block executes
C. When a return statement is encountered
D. Finally block always executes
Q. What is the purpose of the `throws` keyword in Java?
A. To catch exceptionsB. To declare checked exceptions
C. To throw exceptions
D. To handle exceptions
Q. Which exception class is the base class for all exceptions in Java?
A. RuntimeExceptionB. Exception
C. Throwable
D. Error
Q. Which of the following code snippets demonstrates the correct syntax for exception handling in Java?
A.try { // code that may throw an exception } catch (Exception e) { // catch the exception and handle it }B.
try { // code that may throw an exception } catch (error e) { // catch the error and handle it }C.
try { // code that may throw an exception } catch (Throwable t) { // catch any throwable and handle it }D.
try { // code that may throw an exception } catch (RuntimeException e) { // catch only runtime exceptions and handle them }