Exception Handling In Java MCQ Core Java java Java Exceptions Java MCQ MCQ by devs5003 - July 19, 2023March 20, 20240 Last Updated on March 20th, 2024Being a Java developer, you must be familiar with the importance of Exception handling in Java. Undoubtedly, Exception handling is the most important for you. You can’t imagine even a small project in Java without the implementation of Exception handling. Moreover, in order to work in an industry level project, you must have a good knowledge on Exception handling in Java. Apart from going through theoretical concepts multiple times, it is highly advisable that developers need much coding practice on Exception handling. In this article, we will be doing theoretical & code practice on ‘Exception handling in Java’ in the form of MCQs that are frequently asked in the interviews as well. Definitely, you must find this article beneficial whether it is an interview or a written test from these Exception Handling In Java MCQ. You may also go through the article How to resolve common Java Exceptions to get a good hold on your Exception handling concepts. Exception Handling In Java MCQ Q#1. Which of the following is incorrect about try-with-resources in Java? (a) try-with-resources was introduced in Java 7 (b) We don't need to use finally block, if we use try-with-resources (c) The try-with-resources statement ensures that each resource is closed at the end of the statement (d) It increases the complexity of the code Answer: d) It increases the complexity of the code Explanation: The try-with-resources was introduced in Java 7. When we use try-with-resources, the resources will automatically be closed. Hence, we don’t need to use finally block. It reduces the complexity of the code and even reduces the lines of code. Q#2. Which of the following scenarios is best suited for utilizing the try-with-resources statement? (a) Handling common runtime exceptions (b) Implementing custom exception classes (c) Working with IO operations involving streams (d) Synchronizing multi-threaded operations Answer: c) Working with IO operations involving streams Explanation: From the mentioned options, try-with-resources statement will be best suited when working with IO operations involving streams, such as reading from or writing to files. It ensures that the streams are automatically closed after usage, reducing the risk of resource leaks and improving code reliability. Q#3. What is the output of the following code snippet? try { throw new RuntimeException("Error"); } catch (Exception e) { System.out.println(e.getMessage()); } (a) Error (b) RuntimeException (c) null (d) The code will not compile. Answer: a) Error Explanation: The code explicitly throws a RuntimeException with the message “Error”. The catch block catches the exception and prints the error message using getMessage(). Q#4. Which of the following statement(s) is/are correct about multi-catch statement? (a) A single catch block can handle more than one type of exception. (b) A multi-catch statement is valid in Java 7 and later. (c) If a catch block handles more than one exception type, then the catch parameter is implicitly final. (d) Alternatives in a multi-catch statement cannot be related by subclassing. Answer: (a), (b), (c), (d) Explanation: All statements are correct about a multi-catch statement since Java 7. Q#5. Which of the following is the correct syntax for catching multiple exceptions in a single catch block? (a) catch (ExceptionType1 || ExceptionType2 || ExceptionType3 ex) (b) catch (ExceptionType1, ExceptionType2, ExceptionType3 ex) (c) catch (ExceptionType1 && ExceptionType2 && ExceptionType3 ex) (d) catch (ExceptionType1 | ExceptionType2 | ExceptionType3 ex) Answer: d) catch (ExceptionType1 | ExceptionType2 | ExceptionType3 e) Explanation: Multiple exceptions can be caught in a single catch block using the single pipe symbol (|) to separate the exception types. Q#6. What is wrong with the following code snippet in the context of try-with-resources? static String readFirstLineFromFile() throws IOException { try (FileReader fr = new FileReader(""); BufferedReader br = new BufferedReader(fr)) { fr= new FileReader("xyz.txt"); return br.readLine(); } } (a) There is no catch block after the try block. (b) The re-assignment of variable 'fr' is not allowed. (c) Two resources can't be declared in a try block. (d) The throws clause is not needed. Answer: b) The re-assignment of variable ‘fr’ is not allowed. Explanation: The resources declared in a try-with-resources context are final by default, hence we can’t re-assign them. There will be a compilation error at the same line. Q#7. Which of the following statements is true about exceptions in the context of Java 7 and later versions? (a) A try block must be followed by either a catch block or a finally block. (b) In order to close the resources opened in try block, it is mandatory to include a finally block. (c) Multiple types of exceptions can be handled by including multiple catch blocks. (d) It is not mandatory to include a catch block or finally block after a try block. Answer: d) It is not mandatory to include a catch block or finally block after a try block. Explanation: Checked exceptions must be caught or declared to be thrown by the method that can potentially throw them. Q#8. Which exception will be thrown by parseInt() method in Java? (a) IntegerOutOfBoundException (b) IntegerFormatException (c) ArithmeticException (d) NumberFormatException Answer: d) NumberFormatException Explanation: parseInt() method parses input into integer. This method will throw NumberFormatException. Q#9. Which of the following exception must be either caught or declared to be thrown in Java? (a) NullPointerException (b) ArrayIndexOutOfBoundsException (c) FileNotFoundException (d) ArithmeticException Answer: c) FileNotFoundException Explanation: FileNotFoundException is a checked exception in Java, hence it must be either caught or declared to be thrown. Q#10. What is the output of the following code snippet? try { throw new NullPointerException(); } catch (RuntimeException e) { System.out.println("RuntimeException"); } catch (Exception e) { System.out.println("Exception"); } (a) RuntimeException (b) Exception (c) NullPointerException (d) The code will not compile. Answer: a) RuntimeException Explanation: The code explicitly throws a NullPointerException, which is a subclass of RuntimeException. Since the catch block for RuntimeException is defined first, it is executed. Q#11. Which of the following is true about the catch block in Java? (a) A catch block can catch multiple types of exceptions using the semicolon (;). (b) A catch block can catch multiple types of exceptions using the logical AND operator (&). (c) A catch block can catch multiple types of exceptions using multiple catch statements. (d) A catch block can only catch one type of exception at a time. Answer: c) A catch block can catch multiple types of exceptions using multiple catch statements. Explanation: Traditionally, multiple types of exceptions can be caught in a catch block by using multiple catch statements, each catching a different exception type. In contrast, a multi-catch statement can catch multiple exceptions in a single catch block since Java 7. Q#12. Which statement is used to catch and handle multiple exceptions in a single catch block? (a) catch-all (b) multi-catch (c) exception-catch (d) exception-all Answer: b) multi-catch Explanation: The multi-catch statement in Java allows catching and handling multiple exceptions in a single catch block. Q#13. Which keyword will you use to specify that a method can potentially throw an exception? (a) try (b) catch (c) throw (d) throws Answer: d) throws Explanation: We use the throws keyword in a method declaration to state that the method can potentially throw one or more exceptions. Q#14. What is the purpose of the finally block in exception handling? (a) To catch and handle exceptions. (b) To specify that a method can potentially throw an exception. (c) To execute code regardless of whether an exception is thrown or not. (d) To explicitly throw an exception. Answer: c) To execute code regardless of whether an exception is thrown or not. Explanation: The finally block is used to specify code that should be executed regardless of whether an exception is thrown or not. Q#15. Which of the following statements is true about the catch block in exception handling? (a) A try block can have multiple catch blocks. (b) A catch block can have multiple try blocks. (c) A catch block must always be followed by a finally block. (d) A catch block cannot be used without a try block. Answer: a) A try block can have multiple catch blocks. Explanation: A try block can have multiple catch blocks to handle different types of exceptions. Q#16. Which of the following statements is true about the finally block in exception handling? (a) A finally block is always executed before a catch block. (b) A finally block is always executed after a catch block. (c) A finally block is only executed if an exception occurs. (d) A finally block is optional and can be omitted. Answer: b) A finally block is always executed after a catch block. Explanation: A finally block is always executed after a catch block, regardless of whether an exception occurs or not. Q#17. Which of the following is a subclass of the Exception class? (a) RuntimeError (b) Error (c) Throwable (d) StackOverflowError Answer: a) RuntimeError Explanation: RuntimeError is a subclass of the Exception class in Java. Q#18. Which of the following statements is true about the finally block? (a) The finally block is required for every try-catch statement. (b) The finally block is optional and can be omitted. (c) The finally block is executed only if an exception occurs. (d) The finally block is executed only if a catch block is present. Answer: b) The finally block is optional and can be omitted. Explanation: The finally block is optional and can be omitted in exception handling. Q#19. What is the output of the following code snippet? try { int[] array = new int[5]; System.out.println(array[5]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException"); } finally { System.out.println("Finally block executed."); } (a) ArrayIndexOutOfBoundsException Finally block executed. (b) ArrayIndexOutOfBoundsException (c) Finally block executed. (d) The code will not compile. Answer: a) ArrayIndexOutOfBoundsException Finally block executed. Explanation: The code attempts to access an element at index 5 in the array which is not available. Hence it raises an ArrayIndexOutOfBoundsException. The catch block is executed, and then the finally block is executed. Q#20. Which of the following is not a subclass of Throwable in Java? (a) Checked exception (b) Unchecked exception (c) Fatal exception (d) Error Answer: c) Fatal exception Explanation: “Fatal exception” is not a recognized subclass of Throwable in Java. Error is a direct subclass of Throwable. Q#21. Which of the following is/are correct statement(s) about Unchecked Exceptions in Java? (a) These exceptions occur during the execution of the program. (b) They are also referred to as Runtime exceptions. (c) These exceptions are generally ignored during the compilation process. (d) They are checked while compiling the program. Answer: (a), (b), (c) Explanation: They are not checked while compiling the program. Q#22. Which of the following is an incorrect statement about checked Exceptions in Java? (a) Checked exceptions are called compile-time exceptions. (b) They are subtypes of RuntimeException. (c) These exceptions are checked at compile-time by the compiler. (d) The IOException is a type of checked Exception. Answer: b) They are subtypes of RuntimeException Explanation: They are not subtypes of RuntimeException, but direct subtypes of Exception. Q#23. In which of the below classes the printStackTrace() method defined? (a) Exception. (b) RuntimeException. (c) Throwable. (d) Error. Answer: c) Throwable. Explanation: The printStackTrace() method is defined in the Throwable class. Q#24. Which of the following is a not a subclass of the Error class directly or indirectly? (a) RuntimeError (b) InternalError (c) StackOverflowError (d) OutOfMemoryError Answer: a) RuntimeError Explanation: RuntimeError is not a subclass of the Error class directly or indirectly in Java. Q#25. What is the output of the following code snippet? try { throw new Exception("Custom exception"); } catch (Exception e) { System.out.println(e.getMessage()); } (a) Custom exception (b) Exception (c) null (d) The code will not compile. Answer: a) Custom exception Explanation: The code explicitly throws an Exception with the message “Custom exception”. The catch block catches the exception and prints the error message using getMessage(). Q#26. Which of the following statements is true about custom exception classes in Java? (a) Custom exception classes must extend the Throwable class. (b) Custom exception classes must extend the RuntimeException class. (c) Custom exception classes must extend the Exception class. (d) Custom exception classes are not required to extend or implement any class or interface. Answer: a) Custom exception classes must extend the Exception class. Explanation: In Java, custom exception classes must be subclasses of the Exception class or one of its subclasses. Q#27. Which of the following is not a type of valid construct in exception handling? (a) try-catch-finally (b) try-finally (c) catch-finally (d) try-catch Answer: c) catch-finally Explanation: “catch-finally” is not a valid exception handling construct in Java. The correct construct is “try-catch” or “try-catch-finally” or “try-finally”. Q#28. Which of the following is not a checked exception in Java? (a) IOException (b) FileNotFoundException (c) NullPointerException (d) ClassNotFoundException Answer: c) NullPointerException Explanation: NullPointerException is an unchecked exception in Java. Q#29. What is the output of the following code snippet? try { throw new Error("Fatal error"); } catch (Exception e) { System.out.println("Exception"); } catch (Error e) { System.out.println("Error"); } (a) Exception (b) Error (c) Compiler error (d) The code will not compile. Answer: b) Error Explanation: The code explicitly throws an Error with the message “Fatal error”. Since Error is a subclass of Throwable, it matches the catch block for Error, and “Error” is printed. Q#30. Which of the following exceptions is not a subclass of the RuntimeException class? (a) NullPointerException (b) ArrayIndexOutOfBoundsException (c) IOException (d) ArithmeticException Answer: c) IOException Explanation: IOException is not a subclass of the RuntimeException class. It is a checked exception in Java. Q#31. Which of the following statements is true about the try-with-resources statement in Java? (a) It is used to handle multiple exceptions in a single catch block. (b) It is used to specify that a method can potentially throw an exception. (c) It is used to automatically close resources after usage. (d) It is used to define custom exception classes. Answer: c) It is used to automatically close resources after usage. Explanation: The try-with-resources statement in Java is used to automatically close resources after usage, ensuring that resources are properly managed and released. Q#32. Which of the following statements is true about handling exceptions in multi-threaded Java applications? (a) Each thread should handle exceptions independently. (b) Exceptions thrown by a thread cannot be caught by other threads. (c) A separate exception handler should be defined for each thread. (d) Exceptions in multi-threaded applications are handled automatically by the JVM. Answer: c) A separate exception handler should be defined for each thread. Explanation: In multi-threaded Java applications, it is recommended to define a separate exception handler for each thread to handle exceptions specific to that thread. Q#33. What is the output of the following code snippet? try { throw new Exception("First Exception"); } catch (Exception e) { try { throw new Exception("Second Exception"); } catch (Exception ex) { System.out.println(ex.getMessage()); } } (a) First Exception (b) Second Exception (c) First Exception followed by Second Exception (d) Second Exception followed by First Exception Answer: b) Second Exception Explanation: The code throws the first exception, catches it, and then throws the second exception, which is caught and its message is printed. Q#34. What is the output of the following code snippet? try { throw new Error(); } catch (Throwable t) { System.out.println(t.getClass().getSimpleName()); } (a) Error (b) Throwable (c) Exception (d) The code will not compile. Answer: a) Error Explanation: The code explicitly throws an Error, which is caught by the catch block. The getClass().getSimpleName() method is used to retrieve the simple name of the caught exception’s class. Q#35. Which of the following is the correct syntax for using the try-with-resources statement? (a) try [Resource r =new Resource()] { // code } (b) try (Resource r = new Resource(); // code ) (c) try { Resource r = new Resource(); // code } (d) try (Resource r = new Resource()) // code Answer: d) try (Resource r = new Resource()) // code Explanation: Option (d) is the correct syntax. Q#36. Which of the following interfaces must be implemented by a resource in order to be used with the try-with-resources statement? (a) Closeable (b) AutoCloseable (c) Resource (d) Disposable Answer: b) AutoCloseable Explanation: Resources used with the try-with-resources statement must implement the AutoCloseable interface, which provides the close() method for releasing system resources held by the resource. Q#37. Which method of AutoCloseable interface is called internally in the try-with-resources statement? (a) clean() (b) refresh() (c) close() (d) release() Answer: c) close() Explanation: The close() method in a resource class is responsible for releasing system resources held by the resource, such as closing file streams or network connections. Q#38. Which of the following statements is true regarding the order of closing resources in a try-with-resources statement? (a) Resources are closed in the order of declaration within the try block. (b) Resources are closed in the reverse order of declaration within the try block. (c) Resources are closed randomly. (d) The order of closing resources does not matter. Answer: b) Resources are closed in the reverse order of declaration within the try block. Explanation: In a try-with-resources statement, resources are closed in the reverse order of their declaration within the try block. This ensures that resources are properly closed, even if an exception occurs. Q#39. What happens if an exception is thrown both during resource initialization and within the try block of a try-with-resources statement? (a) The exception thrown during resource initialization takes precedence. (b) The exception thrown within the try block takes precedence. (c) Both exceptions are caught and handled. (d) Only the exception thrown within the try block is caught and handled. Answer: a) The exception thrown during resource initialization takes precedence. Explanation: If an exception is thrown both during resource initialization and within the try block, the exception thrown during resource initialization takes precedence. The exception thrown within the try block is added as a suppressed exception. Q#40. What is the advantage of using the try-with-resources statement instead of a traditional try-catch-finally approach? (a) It reduces boilerplate code. (b) It ensures proper resource cleanup without explicitly writing a finally block. (c) It simplifies complex exception handling. (d) It improves the performance of exception handling significantly. Answer: b) It ensures proper resource cleanup without explicitly writing a finally block. Explanation: The try-with-resources statement ensures proper resource cleanup by automatically calling the close() method on the resource, eliminating the need to explicitly write a finally block. It improves code readability and reduces the risk of resource leaks. ♥ You may also attempt MCQs on other topics in Java by visiting our Quizzes section. Related