Java Exception Handling Quiz - MCQ - Multiple Choice Questions

Exception handling is an important aspect of Java programming that allows you to handle and manage unexpected runtime errors. In this blog post, we present a Java Exception Handling Quiz comprising 10+ multiple-choice questions (MCQ). This quiz aims to assess your understanding of exception handling in Java, including the basics of exceptions, try-catch blocks, and exception propagation. Let's put your knowledge of Java exception handling to the test!.

Learn and Master Java Programming: Learn Java Programming with Examples

Learn everything about Java 8 features: Java 8 Tutorial and Examples

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills

1. What is an exception in Java? 

a) An error that occurs during compilation 
b) An unexpected event that occurs during program execution 
c) A warning issued by the Java compiler 
d) A reserved keyword in the Java language 

Answer:

b) An unexpected event that occurs during program execution 

Explanation:

An exception in Java is an unexpected event that occurs during program execution, disrupting the normal flow of the program. 

2. What is the purpose of exception handling in Java? 

a) To prevent exceptions from occurring 
b) To hide exceptions from the user 
c) To gracefully handle and recover from exceptions 
d) To terminate the program when an exception occurs 

Answer:

c) To gracefully handle and recover from exceptions  

Explanation:

The purpose of exception handling in Java is to gracefully handle and recover from exceptions, allowing the program to continue execution despite the occurrence of an exception. 

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

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

Answer:

a) throw  

Explanation:

The throw keyword is used to throw an exception explicitly in Java, allowing you to create and throw custom exceptions or throw predefined exceptions. 

4. What is the role of the finally block in a try-catch-finally statement? 

a) To catch and handle exceptions 
b) To specify the code that is always executed, regardless of whether an exception occurs or not 
c) To specify the code that is executed when an exception occurs 
d) To specify the code that is executed after the try block but before any catch block 

Answer:

b) To specify the code that is always executed, regardless of whether an exception occurs or not 

Explanation:

The finally block in a try-catch-finally statement is used to specify the code that is always executed, regardless of whether an exception occurs or not. It is typically used to perform cleanup tasks or release resources. 

5. What is exception propagation in Java? 

a) The process of creating and throwing custom exceptions 
b) The process of handling multiple exceptions in a single try-catch block 
c) The process of passing an exception from one method to its calling method 
d) The process of catching and handling exceptions in nested try-catch blocks 

Answer:

c) The process of passing an exception from one method to its calling method 

Explanation:

Exception propagation in Java refers to the process of passing an exception from one method to its calling method, allowing for centralized exception handling or propagation to higher-level methods.

6. Which exception will the following throw?

public class Test {

    public static void main(String[] args) {
        Object obj = new Integer(3);
        String str = (String) obj;
        System.out.println(str);
    }
}
A. ArrayIndexOutOfBoundsException
B. ClassCastException
C. IllegalArgumentException
D. NumberFormatException
E. None of the above.
 

Answer:

B. ClassCastException

Explanation: 

The second line tries to cast an Integer to a String. Since String does not extend Integer, this is not allowed and a ClassCastException is thrown.

7. Which of the following are checked exceptions? (Choose all that apply)

A. Exception
B. IllegalArgumentException
C. IOException
D. NullPointerException
E. NumberFormatException
F. StackOverflowError
 

Answer:

A and C

Explanation: 

Option A is the exception base class, which is a checked exception. Options B, D, and E extend RuntimeException directly or indirectly and therefore are unchecked exceptions. Option F is a throwable and not an exception, so should not be caught or declared. 

8. What is the output of the following Java program?

public class Test {
    public static void main(String[] args) {
        int x = 0;
        int y = 1;
        int z = y / x;
    }
}
A. Compiler Error
B. Compiles and runs fine
C. Compiles fine but throws ArithmeticException exception
D. None of above

Answer:

C. Compiles fine but throws ArithmeticException exception

Explanation: 

ArithmeticException is an unchecked exception, i.e., not checked by the compiler. So the program compiles fine.
 
Here is the output of this program:
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Test.main(Test.java:6)

9. Which of the following exceptions are thrown by the JVM? (Choose all that apply)

A. ArrayIndexOutOfBoundsException
B. ExceptionInInitializerError
C. java.io.IOException
D. NullPointerException
E. NumberFormatException
 

Answer:

A, B, D

Explanation: 

java.io.IOException is thrown by many methods in the java.io package, but it is always thrown programmatically. The same is true for NumberFormatException; it is thrown programmatically by the wrapper classes of java.lang. The other three exceptions are all thrown by the JVM when the corresponding problem arises.

10. What is the output of the following program?

 public class Laptop {
     public void start() {
         try {
             System.out.print("Starting up ");
             throw new Exception();
         } catch (Exception e) {
             System.out.print("Problem ");
             System.exit(0);
         } finally {
             System.out.print("Shutting down ");
         }
     }
     public static void main(String[] args) {
         new Laptop().start();
     }
 }
A. Starting up
B. Starting up Problem
C. Starting up Problem Shutting down
D. Starting up Shutting down
E. The code does not compile.
F. An uncaught exception is thrown
 

Answer:

B. Starting up Problem

Explanation: 

The main() method invokes start on a new Laptop object. Line 4 prints Starting up; then line 5 throws an Exception. Line 6 catches the exception, line 7 prints the Problem, and then line 8 calls System.exit, which terminates the JVM. The finally block does not execute because the JVM is no longer running. 

Conclusion

Congratulations on completing the Java Exception Handling Quiz! We hope it challenged your knowledge and provided valuable insights into exception handling in Java. Exception handling is crucial for writing robust and reliable code. 

Learn and Master Java Programming: Learn Java Programming with Examples

Learn everything about Java 8 features: Java 8 Tutorial and Examples

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills

Keep practicing, keep learning, and become proficient in effectively managing exceptions in your Java programs!

Comments