Java Exceptions MCQ Questions and Answers

1. What is an exception in Java?

a) An error in the code syntax
b) A runtime error disrupting the normal flow of the program
c) A compile-time error
d) A warning during program execution

Answer:

b) A runtime error disrupting the normal flow of the program

Explanation:

An exception in Java is a runtime error that disrupts the normal flow of the program's instructions.

2. Which keyword is used to handle exceptions in Java?

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

Answer:

c) try

Explanation:

The 'try' keyword is used to specify a block where exceptions can occur and to initiate exception handling.

3. What is the purpose of the 'catch' block in Java?

a) To declare an exception
b) To throw an exception
c) To handle an exception
d) To test a block of code for errors

Answer:

c) To handle an exception

Explanation:

The 'catch' block in Java is used to handle the exception that might be thrown in the 'try' block.

4. What is a checked exception in Java?

a) An exception that is checked at compile time
b) An exception that occurs at runtime
c) An exception that can be ignored
d) An exception that must be caught using a try-catch block

Answer:

a) An exception that is checked at compile time

Explanation:

Checked exceptions are exceptions that are checked at compile time, and the programmer is required to handle them.

5. Which of these is a checked exception?

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

Answer:

c) IOException

Explanation:

IOException is a checked exception, as it must be either caught or declared in the method signature.

6. What is an unchecked exception in Java?

a) An exception that is checked at compile time
b) An exception that occurs due to logical errors
c) An exception that is not checked at compile time
d) An exception that can be ignored

Answer:

c) An exception that is not checked at compile time

Explanation:

Unchecked exceptions are runtime exceptions that are not checked at compile time, such as NullPointerException.

7. How can a method declare an exception?

a) Using the 'throw' keyword
b) Using the 'throws' keyword
c) Using the 'exception' keyword
d) By including it in the method body

Answer:

b) Using the 'throws' keyword

Explanation:

A method can declare an exception in its signature using the 'throws' keyword followed by the exception type.

8. What is the 'finally' block used for in Java?

a) To finally execute the code after several tries
b) To execute code, regardless of whether an exception was thrown or not
c) To declare final variables
d) To finalize object creation

Answer:

b) To execute code, regardless of whether an exception was thrown or not

Explanation:

The 'finally' block in Java is used to execute code regardless of whether or not an exception occurs in the try-catch block.

9. What does the 'throw' keyword do in Java?

a) Catches an exception
b) Declares an exception
c) Manually throws an exception
d) Tests a block of code for errors

Answer:

c) Manually throws an exception

Explanation:

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

10. What is the base class for all exceptions in Java?

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

Answer:

c) Throwable

Explanation:

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

11. Can multiple catch blocks be used with a single try block?

a) Yes
b) No
c) Only if they catch the same type of exception
d) Only in a nested try block

Answer:

a) Yes

Explanation:

Multiple catch blocks can be associated with a single try block to handle different types of exceptions.

12. What is the correct order of catch blocks when handling multiple exceptions?

a) From more specific to more general exceptions
b) From more general to more specific exceptions
c) Alphabetical order
d) Order does not matter

Answer:

a) From more specific to more general exceptions

Explanation:

When using multiple catch blocks, they should be ordered from most specific to most general to avoid unreachable code.

13. What happens if an exception is not caught?

a) The program will re-try the code block
b) The program will continue execution
c) The exception is ignored
d) The program terminates abnormally

Answer:

d) The program terminates abnormally

Explanation:

If an exception is not caught, it causes the program to terminate abruptly.

14. Can the 'finally' block be used without a 'catch' block?

a) Yes
b) No
c) Only in combination with a 'throw' block
d) Only if there is no exception

Answer:

a) Yes

Explanation:

The 'finally' block can be used after a 'try' block without a 'catch' block. It is typically used to execute important code such as closing resources, regardless of whether an exception occurs.

15. What is exception propagation in Java?

a) Throwing an exception from a method to its caller
b) Generating exceptions randomly
c) Spreading an exception to multiple classes
d) Ignoring an exception

Answer:

a) Throwing an exception from a method to its caller

Explanation:

Exception propagation in Java refers to the process where an exception is thrown from the method where it occurred to the method that called it.

16. What is a custom exception in Java?

a) An exception provided by Java
b) A user-defined exception class
c) A built-in runtime exception
d) An error during execution

Answer:

b) A user-defined exception class

Explanation:

A custom exception in Java is an exception defined by the user, created by extending the Exception class.

17. What is a stack trace in the context of exceptions?

a) A memory allocation method
b) A list of methods that were called before an exception occurred
c) A type of unchecked exception
d) A data structure for storing exceptions

Answer:

b) A list of methods that were called before an exception occurred

Explanation:

A stack trace is a report that provides information about the methods that were invoked before an exception was thrown.

18. What keyword is used to create a custom exception in Java?

a) throw
b) throws
c) exception
d) extend

Answer:

d) extend

Explanation:

To create a custom exception, the 'extend' keyword is used to make a new class that extends either Exception or RuntimeException.

19. How can you handle multiple types of exceptions in a single catch block?

a) By using the '|' operator
b) By separating exception types with commas
c) By using the '&' operator
d) It is not possible

Answer:

a) By using the '|' operator

Explanation:

In Java, you can handle multiple types of exceptions in a single catch block by separating the exception types with the '|' (pipe) operator.

20. What is a RuntimeException in Java?

a) An exception that is checked at compile time
b) An exception that is thrown at runtime and not checked at compile time
c) An exception that occurs during the compilation
d) A type of error

Answer:

b) An exception that is thrown at runtime and not checked at compile time

Explanation:

RuntimeExceptions are exceptions that are thrown at runtime and do not need to be declared or caught.

21. Can you throw an exception manually in Java?

a) Yes, using the 'throw' keyword
b) No, exceptions occur naturally
c) Only system exceptions can be thrown
d) Only in a catch block

Answer:

a) Yes, using the 'throw' keyword

Explanation:

You can manually throw an exception in Java using the 'throw' keyword followed by an instance of the exception.

22. Why is it advisable to handle exceptions?

a) To improve the performance of the application
b) To provide a user-friendly response to errors
c) To increase the speed of code execution
d) To debug the program

Answer:

b) To provide a user-friendly response to errors

Explanation:

Handling exceptions is important to provide a user-friendly response and to maintain the normal flow of the application when errors occur.

23. What does it mean to "catch" an exception?

a) To ignore an exception
b) To log the occurrence of an exception
c) To respond to an exception being thrown
d) To prevent an exception

Answer:

c) To respond to an exception being thrown

Explanation:

Catching an exception means writing code to respond to and handle the exception that has been thrown.

24. What type of exceptions need to be declared in a method's 'throws' clause?

a) All exceptions
b) Only unchecked exceptions
c) Only checked exceptions
d) Only RuntimeExceptions

Answer:

c) Only checked exceptions

Explanation:

Checked exceptions that might be thrown within a method must be declared in the method's 'throws' clause.

25. What is the difference between the Error and Exception classes in Java?

a) No difference, they are the same
b) Error is for compile-time issues, Exception is for runtime issues
c) Error represents serious problems that applications should not try to handle, Exception represents conditions that applications might want to handle
d) Error is a subclass of Exception

Answer:

c) Error represents serious problems that applications should not try to handle, Exception represents conditions that applications might want to handle

Explanation:

Error is used for serious system-level problems that applications generally should not try to handle, while Exception is used for conditions that applications might want to catch and handle.


Comments