Java Exception Handling Quiz - Multiple Choice Questions

In this post, we have provided Java Exception Handling multiple-choice questions to test your knowledge about exception handling in Java.

Learn Java exception handling at https://www.javaguides.net/p/java-exception-handling-tutorial.html

We would suggest you, try these code snippets in eclipse IDE and understand how the program works (However, the answer with the explanation given at end of this post). These questions may ask in interviews or similar questions may appear in interviews so prepare yourself.

The answer and explanation of each question have given at end of this post.

Q1 - 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.

Q2 - Which of the following are checked exceptions? (Choose all that apply)

A. Exception

B. IllegalArgumentException

C. IOException

D. NullPointerException

E. NumberFormatException

F. StackOverflowError

Q3 - 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

Q4 - 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

Q5 - 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


Answers

Q1

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.

Q2

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, and so should not be caught or declared. 

Q3

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)

Q4

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.

Q5

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 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. 

Comments