Exceptions Questions with Answers

70 practice questions on Exceptions (Core Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 70 free in the AptiNest app.

Sample questions

Question 1Easy

What is the superclass of both Exception and Error?

  1. Object directly
  2. RuntimeException
  3. Throwable
  4. Exception

Answer: C. Throwable

Explanation

java.lang.Throwable is the root of everything that can be thrown; Exception and Error are its two direct subclasses.

Step-by-step solution

  1. Exception and Error both belong to the Throwable class.
  2. Throwable is the parent class for all exceptions and errors in Java.
  3. Therefore, Throwable is the superclass of both Exception and Error.
  4. The correct answer matches this hierarchy.
  5. Final answer: Throwable.

Why the other options are wrong

  • A. Object is an ancestor of both, but the direct superclass of Exception and Error is Throwable.
  • B. RuntimeException is a subclass of Exception, so it is not the superclass of Error.
  • D. Exception is not a superclass of Error; both extend Throwable separately.

Tip: Remember: Throwable is the topmost class for exceptions and errors.

Question 2Easy

What is the output?

public class Main {
    public static void main(String[] args) {
        try {
            int[] a = new int[2];
            a[2] = 5;
            System.out.print("A ");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.print("B ");
        } finally {
            System.out.print("C");
        }
    }
}
  1. C
  2. B C
  3. A B C
  4. A C

Answer: B. B C

Explanation

a[2] is out of bounds for a length-2 array, so the exception skips "A", the catch prints B, and finally always prints C.

Step-by-step solution

  1. The array has length 2, so a[2] is out of bounds.
  2. The exception is thrown before 'A ' is printed, so A is skipped.
  3. The catch block prints 'B ', then finally prints 'C'.
  4. Output is B C, which is option B.

Why the other options are wrong

  • A. The catch block runs, so B is printed.
  • C. A is never printed because the exception skips that line.
  • D. The exception is caught, so B is printed; A is not printed.

Tip: Valid indexes for an array of length n run from 0 to n − 1; index n throws.

Question 3Easy

What happens when this code is compiled?

import java.io.IOException;

public class Main {
    static void read() {
        throw new IOException("disk");
    }

    public static void main(String[] args) {
        read();
    }
}
  1. It runs silently
  2. It throws IOException at runtime
  3. Compilation error: unreported exception IOException
  4. It throws RuntimeException at runtime

Answer: C. Compilation error: unreported exception IOException

Explanation

IOException is checked, so read() must either catch it or declare throws IOException.

Step-by-step solution

  1. read() method throws IOException which is a checked exception.
  2. Checked exceptions must be declared in method signature or caught.
  3. Main method does not handle or declare IOException, so compilation fails.
  4. Compiler reports error: unreported exception IOException.
  5. Code does not run because it fails to compile.
  6. Final answer: Compilation error: unreported exception IOException.

Why the other options are wrong

  • A. The code does not run silently; read() throws a checked exception that main does not handle.
  • B. The code does not reach runtime, since the compiler stops it because IOException is not handled.
  • D. No RuntimeException is thrown; the checked IOException causes a compile error.

Tip: Always handle checked exceptions with try-catch or declare them in method signature.

Question 4Easy

What is the output?

public class Main {
    public static void main(String[] args) {
        try {
            System.out.print(10 / 0);
        } catch (ArithmeticException e) {
            System.out.print(e.getMessage());
        }
    }
}
  1. / by zero
  2. 0
  3. Infinity
  4. ArithmeticException

Answer: A. / by zero

Explanation

Integer division by zero throws ArithmeticException with the message "/ by zero".

Step-by-step solution

  1. Division by zero throws ArithmeticException.
  2. Catch block prints e.getMessage() which is / by zero.
  3. So output is / by zero.

Why the other options are wrong

  • B. 10 / 0 with integers throws, so 0 is not printed and the message is used instead.
  • C. Integer division by zero throws ArithmeticException, so Infinity is not printed.
  • D. getMessage() prints the message text, not the class name, so the output is / by zero.

Tip: Remember: dividing by zero in Java throws ArithmeticException with message '/ by zero'.

Question 5Medium

What is the output?

public class Main {
    static String test(int n) {
        try {
            if (n == 0) throw new IllegalArgumentException();
            return "ok";
        } catch (IllegalArgumentException e) {
            return "caught";
        } finally {
            System.out.print("fin ");
        }
    }

    public static void main(String[] args) {
        System.out.println(test(0) + " " + test(1));
    }
}
  1. caught ok fin fin
  2. caught fin ok fin
  3. fin fin caught ok
  4. fin caught fin ok

Answer: C. fin fin caught ok

Explanation

Both calls finish (printing "fin " each time) before println builds and prints its argument string "caught ok".

Step-by-step solution

  1. test(0) throws, the catch returns 'caught', and finally prints 'fin ' before the method returns.
  2. test(1) returns 'ok', and finally prints 'fin ' before the method returns.
  3. Both 'fin ' lines print while the arguments are evaluated, before println runs.
  4. Println then prints 'caught ok', so the output is fin fin caught ok (option C).

Why the other options are wrong

  • A. Finally runs before println, so 'fin ' is printed first.
  • B. Both finally blocks print before println, so 'fin ' does not appear between the results.
  • D. Both 'fin ' lines appear before the result line, and the first result is 'caught', not 'fin'.

Tip: Method calls inside a println argument run first, so any output they print comes before the line itself.

Question 6Medium

What is the output of this program?

    class exception_handling {
        public static void main(String args[]) {
            try {
                int a[] = {1, 2,3 , 4, 5};
                for (int i = 0; i < 5; ++i)
                    System.out.print(a[i]);
                int x = 1/0;
            }
            catch(ArrayIndexOutOfBoundsException e) {
        	System.out.print("A");
            }
            catch(ArithmeticException e) {
                System.out.print("B");
            }
        }
    }
  1. 12345
  2. 12345A
  3. 12345B
  4. Comiplation Error

Answer: C. 12345B

Explanation

The values 1 to 5 print first, giving 12345. Then 1 / 0 throws an ArithmeticException, which the ArrayIndexOutOfBoundsException catch does not match. The ArithmeticException catch prints B, so the output is 12345B.

Step-by-step solution

  1. The loop prints 1 2 3 4 5 with no error, since indexes 0 to 4 exist.
  2. Then 1/0 throws ArithmeticException.
  3. The ArrayIndexOutOfBounds catch does not match, so Java checks the next catch.
  4. The ArithmeticException catch matches and prints B, so the output is 12345B, which is option C.

Why the other options are wrong

  • A. B is printed by the arithmetic catch block, so it must appear.
  • B. A is printed only for array index errors, and none happens here.
  • D. The two catch blocks are valid, so the code compiles.

Tip: Each catch matches one exception type; the first matching catch runs.

Question 7Medium

What is the output of this program?

class exception_handling {
        public static void main(String args[]) {
            try {
                int i, sum;
                sum = 10;
                for (i = -1; i < 3 ;++i) {
                    sum = (sum / i);
                System.out.print(i);
                }
            }
            catch(ArithmeticException e) {
                System.out.print("0");
            }
        }
    }
  1. -1
  2. 0
  3. -10
  4. -101

Answer: C. -10

Explanation

The loop starts at i = -1. In the first pass sum becomes 10 / -1 = -10 and -1 is printed. In the next pass i is 0, so sum / 0 throws ArithmeticException, the catch prints 0, and the program stops. The output is -10.

Step-by-step solution

  1. Sum starts at 10, and the loop starts with i = -1.
  2. When i = -1: sum = 10 ÷ -1 = -10, and then -1 is printed.
  3. I becomes 0: sum = -10 ÷ 0 throws an ArithmeticException, which stops the loop.
  4. The catch prints 0, so the output is -1 then 0, which is -10, option C as the correct answer.

Why the other options are wrong

  • A. This prints only -1; the catch still prints 0 after the division by zero.
  • B. This prints only 0, but -1 is printed before the exception happens.
  • D. I never reaches 1, because the exception stops the loop at i = 0.

Tip: A division by zero stops the loop at that point, so count only the lines printed before the exception.

Question 8Medium

Which of these keywords is used to generate an exception explicitly?

  1. try
  2. finally
  3. throw
  4. catch

Answer: C. throw

Explanation

Before you can catch an exception, some Java code somewhere must throw one. Any Java code can throw an exception: your code, code from a package written by someone else (such as the packages that come with the Java development environment), or the Java runtime system. Regardless of who (or what) throws the exception, it's always thrown with the Java throw statement.

Step-by-step solution

  1. Throw keyword is used to explicitly generate an exception in Java.
  2. Try block is used to enclose code that might throw an exception.
  3. Catch block is used to handle exceptions thrown by the try block.
  4. Finally block is executed regardless of whether an exception is thrown or not.
  5. So, the correct keyword to generate an exception explicitly is throw.

Why the other options are wrong

  • A. try marks the block that is checked for exceptions; it does not generate an exception itself.
  • B. finally runs after try and catch whether or not an exception happens; it does not throw one.
  • D. catch handles an exception that was already thrown; it does not generate one explicitly.

Tip: Remember: 'throw' is used to create exceptions, while 'try', 'catch', and 'finally' manage them.

Question 9Hard

What is the output?

public class Main {
    public static void main(String[] args) {
        int i = 0;
        while (true) {
            try {
                i++;
                if (i == 3) break;
                continue;
            } finally {
                System.out.print(i + " ");
            }
        }
        System.out.println("end");
    }
}
  1. end
  2. 1 2 3 end
  3. 3 end
  4. 1 2 end

Answer: B. 1 2 3 end

Explanation

finally runs even when control leaves the try block with continue or break, so it prints i on every iteration including the one that breaks.

Step-by-step solution

  1. On the first pass i is 1; continue leaves the try, so finally prints '1 '.
  2. On the second pass i is 2; continue again, and finally prints '2 '.
  3. On the third pass i is 3; break leaves the try, and finally still prints '3 '.
  4. The loop exits, println prints 'end', so the output is 1 2 3 end (option B).

Why the other options are wrong

  • A. Finally runs on every exit from the try block, so the loop output is printed.
  • C. Finally runs for the first two passes too, so 1 and 2 are printed.
  • D. I reaches 3 before the break, so 3 is printed too.

Tip: Finally runs whenever control leaves the try block, including by break or continue.

Question 10Hard

What happens when this code is compiled?

public class Main {
    public static void main(String[] args) {
        try {
            throw new IllegalArgumentException("a");
        } catch (IllegalArgumentException | NumberFormatException e) {
            System.out.println("caught");
        }
    }
}
  1. IllegalArgumentException at runtime
  2. caught
  3. It compiles; NumberFormatException is ignored
  4. Compilation error: alternatives in a multi-catch cannot be related by subclassing

Answer: D. Compilation error: alternatives in a multi-catch cannot be related by subclassing

Explanation

NumberFormatException is a subclass of IllegalArgumentException. Multi-catch alternatives must not be subtypes of each other.

Step-by-step solution

  1. IllegalArgumentException is the superclass of NumberFormatException.
  2. The alternatives in a multi-catch must not be subclasses of each other.
  3. NumberFormatException is a subclass of IllegalArgumentException, so javac rejects this code.
  4. Option D is the answer; the program never runs.

Why the other options are wrong

  • A. The program does not run, because it fails to compile.
  • B. The code does not compile, so 'caught' is never printed.
  • C. Javac rejects the code; it does not ignore the NumberFormatException alternative.

Tip: In a multi-catch, list only unrelated exception types; drop any type that is a subclass of another listed type.

Practise all 70 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Core Java topics · All topics