MCQs on Exception Handling | Java Error Management

Multiple-choice questions (MCQs) on Exception Handling in Java, divided into specific topics like Try, Catch, Finally Blocks, Throw and Throws Keywords, Custom Exception Handling, and Checked and Unchecked Exceptions. These questions cover various scenarios and concepts to enhance your understanding of Java’s error management system.


1. Try, Catch, Finally Blocks

  1. Which of the following statements is true about the try block in Java?
    a) It must always be followed by a catch block.
    b) It can only handle runtime exceptions.
    c) It is used to catch exceptions.
    d) It is used to define code that may throw an exception.
  2. What happens if an exception occurs in a try block without an appropriate catch block?
    a) The program terminates without any further action.
    b) The exception is ignored and the program continues.
    c) The program will run the finally block.
    d) The compiler throws an error.
  3. Which block is guaranteed to execute regardless of whether an exception occurs?
    a) catch block
    b) finally block
    c) try block
    d) error block
  4. What will the following code output?
    try { throw new Exception("Test Exception"); } catch (Exception e) { System.out.println("Caught"); } finally { System.out.println("Finally"); }

    a) Caught
    b) Finally
    c) Caught Finally
    d) Nothing
  5. Which of the following statements is true about the finally block?
    a) It executes only when no exception occurs.
    b) It executes only when an exception occurs.
    c) It always executes, whether an exception occurs or not.
    d) It can be skipped using throw statement.
  6. Which of the following is a correct combination of blocks in exception handling?
    a) try-catch-finally
    b) try-finally-catch
    c) catch-try-finally
    d) catch-finally-try
  7. What happens if there is a return statement in a try block?
    a) The finally block will not be executed.
    b) The finally block will execute before the return statement.
    c) The program throws an exception.
    d) The finally block will be skipped.
  8. Can a finally block be used without a catch block?
    a) Yes, it can be used without a catch block.
    b) No, it must always follow a catch block.
    c) Yes, but only in try statements that throw errors.
    d) No, finally block requires at least one catch.
  9. What will the following code output?
    try { int x = 5 / 0; } catch (ArithmeticException e) { System.out.println("ArithmeticException caught"); } finally { System.out.println("Finally block executed"); }

    a) ArithmeticException caught
    b) Finally block executed
    c) ArithmeticException caught Finally block executed
    d) Error
  10. Which of the following is NOT a valid exception handling syntax in Java?
    a) try { } catch (Exception e) { }
    b) try { } finally { }
    c) catch { }
    d) try { } catch (Exception e) { } finally { }

2. Throw and Throws Keywords

  1. What is the purpose of the throw keyword in Java?
    a) To handle exceptions
    b) To declare exceptions
    c) To create a custom exception
    d) To manually throw an exception
  2. Which of the following is a valid use of the throw keyword?
    a) throw new Exception("Error occurred");
    b) throw "Error";
    c) throw Exception;
    d) throw "Exception";
  3. What is the function of the throws keyword in Java?
    a) To catch exceptions
    b) To handle runtime exceptions
    c) To declare exceptions that a method might throw
    d) To define custom exceptions
  4. What is the purpose of using throws in a method signature?
    a) To throw an exception from the method
    b) To define which exceptions the method may throw
    c) To handle exceptions that occur in the method
    d) To prevent exceptions from occurring in the method
  5. Which of the following statements is true about the throws keyword?
    a) It is used to throw exceptions explicitly.
    b) It is used to declare exceptions that may occur within a method.
    c) It is used to handle checked exceptions.
    d) It is used to create new exceptions.
  6. Which of the following will cause a compilation error?
    public void method() throws IOException {}

    a) No compilation error.
    b) The method does not throw IOException.
    c) The method does not handle exceptions.
    d) The method is missing a catch block.
  7. What happens when an exception is thrown in a method that has a throws clause?
    a) The exception must be handled within the method.
    b) The exception is passed to the calling method.
    c) The program will stop executing.
    d) The exception is ignored by Java.
  8. Which keyword is used to pass an exception to the calling method in Java?
    a) throw
    b) throws
    c) catch
    d) finally
  9. Which of the following is true regarding the throw and throws keywords?
    a) throw is used to declare exceptions, and throws is used to throw exceptions.
    b) throw is used to throw exceptions, and throws is used to declare exceptions.
    c) Both keywords are used for declaring exceptions.
    d) Both keywords are used for catching exceptions.
  10. What happens if a method declares an exception using throws and the exception is not thrown?
    a) A compilation error occurs.
    b) The program will terminate.
    c) The program runs normally.
    d) The exception is thrown at runtime.

3. Custom Exception Handling

  1. How can a custom exception be created in Java?
    a) By extending the Error class
    b) By extending the Throwable class
    c) By extending the Exception class
    d) By extending the RuntimeException class
  2. Which of the following is a valid way to throw a custom exception in Java?
    a) throw new CustomException();
    b) throw CustomException();
    c) throw CustomException;
    d) throw new Exception(CustomException);
  3. What is the purpose of a custom exception?
    a) To handle runtime exceptions only
    b) To provide more specific exception handling tailored to an application
    c) To handle only checked exceptions
    d) To stop the program execution on errors
  4. Which constructor signature is correct for a custom exception class?
    a) public CustomException() {}
    b) public CustomException(String message) {}
    c) public CustomException(Throwable cause) {}
    d) All of the above
  5. Which of the following is a valid custom exception class in Java?
    a) class CustomException extends Exception {}
    b) class CustomException implements Exception {}
    c) class CustomException extends RuntimeException {}
    d) class CustomException implements Throwable {}
  6. What must be done to handle a custom exception in Java?
    a) It must be declared in the method signature.
    b) It must be handled with a catch block.
    c) It cannot be caught by a catch block.
    d) Both a and b.
  7. Which of the following is an advantage of using custom exceptions?
    a) They provide specific error messages for particular problems.
    b) They are automatically handled by Java.
    c) They only work for checked exceptions.
    d) They can handle multiple errors in the program simultaneously.
  8. How can custom exceptions be used in error logging?
    a) By attaching specific error messages with exception data
    b) By preventing the program from crashing
    c) By logging the exception type automatically
    d) By displaying a generic error message
  9. Can custom exceptions be caught in a try-catch block?
    a) No, they cannot be caught.
    b) Yes, if they extend Exception or RuntimeException.
    c) Yes, but only if they are unchecked exceptions.
    d) No, they must be handled using throw only.
  10. What is a good practice when naming custom exceptions in Java?
    a) Name them generically like CustomException.
    b) Name them based on the error they represent, e.g., FileNotFoundException.
    c) Name them with a prefix Throwable.
    d) Name them after the method they are thrown from.

Answers Table

QnoAnswer
1d) It is used to define code that may throw an exception.
2c) The program will run the finally block.
3b) finally block
4c) Caught Finally
5c) It always executes, whether an exception occurs or not.
6a) try-catch-finally
7b) The finally block will execute before the return statement.
8a) Yes, it can be used without a catch block.
9c) ArithmeticException caught Finally block executed
10c) catch { }
11d) To manually throw an exception
12a) throw new Exception("Error occurred");
13c) To declare exceptions that a method might throw
14b) To define which exceptions the method may throw
15b) It is used to declare exceptions that may occur within a method.
16a) No compilation error.
17b) The exception is passed to the calling method.
18b) throws
19b) throw is used to throw exceptions, and throws is used to declare exceptions.
20c) The program runs normally.
21c) By extending the Exception class
22a) throw new CustomException();
23b) To provide more specific exception handling tailored to an application
24d) All of the above
25a) class CustomException extends Exception {}
26d) Both a and b.
27a) They provide specific error messages for particular problems.
28a) By attaching specific error messages with exception data
29b) Yes, if they extend Exception or RuntimeException.
30b) Name them based on the error they represent, e.g., FileNotFoundException.

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top