Java Exception Handling Coding Questions and Answers

Welcome to the Java Exception Handling Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge on the Java Exception Handling topic. Each question has a correct and brief explanation.

1. What is the result of executing this Java code snippet?

public class Test {
    public static void main(String[] args) {
        try {
            int[] numbers = {1, 2, 3};
            System.out.println(numbers[5]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Array index out of bounds!");
        }
    }
}
a) The program prints "Array index out of bounds!"
b) The program prints a different error message
c) The program throws an ArrayIndexOutOfBoundsException
d) The program executes without any output

Answer:

a) The program prints "Array index out of bounds!"

Explanation:

The code tries to access an index that is out of bounds for the array numbers. This triggers the ArrayIndexOutOfBoundsException, which is caught and handled in the catch block.

2. What does this Java code snippet output?

public class Test {
    public static void main(String[] args) {
        try {
            int x = 0;
            int y = 5 / x;
        } catch (Exception e) {
            System.out.println("Exception occurred");
        }
    }
}
a) 0
b) The program throws an ArithmeticException
c) Exception occurred
d) The program executes without any output

Answer:

c) Exception occurred

Explanation:

Dividing by zero throws an ArithmeticException, which is caught by the catch block since it catches all Exception types.

3. Identify the output of the following code:

public class Test {
    public static void main(String[] args) {
        try {
            badMethod();
            System.out.println("A");
        } catch (Exception ex) {
            System.out.println("B");
        } finally {
            System.out.println("C");
        }
        System.out.println("D");
    }
    public static void badMethod() {
        throw new Error();
    }
}
a) A B C D
b) B C D
c) C
d) B C

Answer:

c) C

Explanation:

The given code snippet throws an Error from the badMethod() method. It's important to note the difference between Error and Exception in Java: 

Error represents serious problems that a reasonable application should not try to catch, while Exception is a condition that your application might want to catch. 

Here's the flow of the code based on its behavior: 

1. The main method calls badMethod(), which immediately throws an Error. 

2. Because the thrown object is an Error and not an Exception, the catch block designed to catch an Exception will not catch this Error. 

3. The finally block executes after the try block exits (whether the try block exits normally or abruptly due to an exception or error). So, "C" is printed. 

4. After executing the finally block, since the Error is not caught by the catch block, it propagates up the call stack, and the rest of the code in the main method after the try-catch-finally block (i.e., System.out.println("D");) does not execute. 

5. The program terminates abnormally due to the uncaught Error.

Therefore, the correct output is: C

4. What will be printed by this Java code?

public class Test {
    public static void main(String[] args) {
        try {
            System.out.println("Hello, world!");
        } finally {
            System.out.println("Finally executing...");
        }
    }
}
a) Hello, world!
b) Finally executing...
c) Hello, world! Finally executing...
d) The program throws an exception

Answer:

c) Hello, world! Finally executing...

Explanation:

The try block is executed normally, and then the finally block is executed, resulting in both statements being printed.

5. What does this code snippet output?

public class Test {
    public static void main(String[] args) {
        try {
            String s = null;
            System.out.println(s.length());
        } catch (NullPointerException e) {
            System.out.println("Caught NullPointerException");
        } catch (Exception e) {
            System.out.println("Caught Exception");
        }
    }
}
a) Caught NullPointerException
b) Caught Exception
c) An uncaught exception is thrown
d) The program executes without any output

Answer:

a) Caught NullPointerException

Explanation:

Accessing the length of a null string throws a NullPointerException, which is caught by the first catch block.

6. What is the result of executing this code?

public class Test {
    public static void main(String[] args) {
        try {
            int[] arr = new int[-5];
        } catch (NegativeArraySizeException e) {
            System.out.println("Negative array size");
        }
    }
}
a) Negative array size
b) An array with size -5 is created
c) The program throws a different type of exception
d) The program executes without any output

Answer:

a) Negative array size

Explanation:

Trying to create an array with a negative size throws a NegativeArraySizeException, which is caught and handled.

7. What will the following Java code snippet output?

public class Test {
    public static void main(String[] args) {
        try {
            throw new RuntimeException();
        } catch (RuntimeException e) {
            System.out.println("RuntimeException caught");
        } catch (Exception e) {
            System.out.println("Exception caught");
        }
    }
}
a) RuntimeException caught
b) Exception caught
c) The program throws an uncaught exception
d) The program executes without any output

Answer:

a) RuntimeException caught

Explanation:

The RuntimeException is caught by the first catch block that matches its type.

8. What does the following code snippet print?

public class Test {
    public static void main(String[] args) {
        String str = null;
        try {
            System.out.println(str.length());
        } catch (NullPointerException e) {
            System.out.println("Null Pointer Exception");
        } finally {
            System.out.println("Finally block executed");
        }
    }
}
a) Null Pointer Exception
b) Finally block executed
c) Null Pointer Exception
d) The program throws an uncaught exception

Answer:

c) Null Pointer Exception Finally block executed

Explanation:

A NullPointerException is thrown and caught, then the finally block executes, resulting in both messages being printed.

9. Determine the output of this Java code:

class MyException extends Exception {}

public class Test {
    public static void main(String[] args) {
        try {
            throw new MyException();
        } catch (MyException e) {
            System.out.println("Custom exception caught");
        }
    }
}
a) Custom exception caught
b) The program throws an uncaught exception
c) Compilation error
d) Runtime error

Answer:

a) Custom exception caught

Explanation:

MyException is a custom exception class. It's thrown and immediately caught in the catch block.

10. What is the result of the following code snippet?

public class Test {
    public static void main(String[] args) {
        try {
            riskyMethod();
        } catch (Exception e) {
            System.out.println("Exception caught");
        }
    }
    static void riskyMethod() throws Exception {
        throw new Exception("Problem");
    }
}
a) Exception caught
b) A message "Problem" is printed
c) The program throws an uncaught exception
d) Compilation error

Answer:

a) Exception caught

Explanation:

riskyMethod throws an Exception with the message "Problem", which is caught by the catch block in the main method.

Comments