QuizzesJava Exception Handling
intermediate
java

Java Exception Handling

Test your knowledge of try-catch-finally, checked vs unchecked exceptions, custom exceptions, multi-catch, try-with-resources, and best practices.

25 minutes 20 questions Pass at 70%
Time Limit
25 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

20 questions on Java Exception Handling. Work through them here, then start the quiz to answer them and see your score.

  1. 1.What is an exception in Java?

    • AAn event that disrupts the normal flow of a program at runtime
    • BA method that returns an error code
    • CA compile-time syntax error
    • DA warning logged to the console
  2. 2.What is the difference between checked and unchecked exceptions?

    • AChecked exceptions cause compile errors; unchecked cause runtime errors only
    • BChecked exceptions must be declared or caught at compile time; unchecked exceptions (RuntimeException subclasses) do not
    • CUnchecked exceptions must be caught; checked exceptions are optional
    • DChecked exceptions are faster; unchecked are slower
  3. 3.What does this code print?

    public class Main {
        public static void main(String[] args) {
            try {
                int result = 10 / 0;
                System.out.println("Result: " + result);
            } catch (ArithmeticException e) {
                System.out.println("Caught: " + e.getMessage());
            } finally {
                System.out.println("Finally block");
            }
        }
    }
    • ACaught: / by zero Finally block
    • BFinally block
    • CResult: Infinity Finally block
    • DCaught: / by zero
  4. 4.Does the finally block always execute?

    • AOnly when an exception is thrown
    • BYes, except when System.exit() is called or the JVM crashes
    • COnly when no exception is thrown
    • DYes, always without exception
  5. 5.What does this code print?

    public class Main {
        static int test() {
            try {
                return 1;
            } finally {
                return 2;
            }
        }
        public static void main(String[] args) {
            System.out.println(test());
        }
    }
    • ACompilation error
    • B2
    • CRuntime exception
    • D1
  6. 6.What is multi-catch syntax in Java 7+?

    • AUsing multiple catch blocks for the same exception
    • BCatching multiple exception types in one catch clause using the | operator
    • CCatching multiple exceptions with nested try blocks
    • DCatching exceptions in multiple threads simultaneously
  7. 7.What does this code print?

    public class Main {
        public static void main(String[] args) {
            try {
                String s = null;
                System.out.println(s.length());
            } catch (NullPointerException e) {
                System.out.println("NPE caught");
            } catch (Exception e) {
                System.out.println("Exception caught");
            }
        }
    }
    • ANPE caught Exception caught
    • BCompilation error
    • CNPE caught
    • DException caught
  8. 8.What happens if you put a more general exception before a specific one?

    try {
        // some code
    } catch (Exception e) {
        System.out.println("General");
    } catch (NullPointerException e) {
        System.out.println("NPE");
    }
    • ABoth blocks run
    • BRuns fine, NPE block is a fallback
    • CCompilation error — unreachable catch block
    • DThe NPE block runs first
  9. 9.What is try-with-resources?

    • AA try block with extra memory allocated
    • BA try block that loads external resources from files
    • CA try block that retries on failure
    • DA try block that automatically closes resources implementing AutoCloseable when the block exits
  10. 10.What does this code print?

    public class Main {
        static class MyResource implements AutoCloseable {
            public void close() { System.out.println("closed"); }
            public void use() { System.out.println("using"); }
        }
        public static void main(String[] args) {
            try (MyResource r = new MyResource()) {
                r.use();
            }
            System.out.println("done");
        }
    }
    • Ausing done
    • Busing closed done
    • Cclosed using done
    • Dusing done closed
  11. 11.How do you create a custom exception in Java?

    • AExtend RuntimeException (unchecked) or Exception (checked)
    • BImplement the Exception interface
    • COverride the Throwable class
    • DUse the @Exception annotation
  12. 12.What does this code print?

    class AgeException extends RuntimeException {
        AgeException(String msg) { super(msg); }
    }
    public class Main {
        static void setAge(int age) {
            if (age < 0) throw new AgeException("Age cannot be negative: " + age);
        }
        public static void main(String[] args) {
            try {
                setAge(-5);
            } catch (AgeException e) {
                System.out.println(e.getMessage());
            }
        }
    }
    • Anull
    • BAge cannot be negative: -5
    • CAgeException: Age cannot be negative: -5
    • DUncaught exception
  13. 13.What is exception chaining?

    • ARe-throwing the same exception multiple times
    • BChaining try blocks one inside another
    • CWrapping a low-level exception inside a higher-level exception using the cause parameter
    • DCatching multiple exceptions with one catch block
  14. 14.What does this code print?

    public class Main {
        public static void main(String[] args) {
            try {
                try {
                    throw new RuntimeException("inner");
                } finally {
                    System.out.println("inner finally");
                }
            } catch (RuntimeException e) {
                System.out.println("caught: " + e.getMessage());
            }
        }
    }
    • Ainner finally caught: inner
    • Bcaught: inner inner finally
    • Ccaught: inner
    • Dinner finally
  15. 15.What is the difference between throw and throws in Java?

    • AThey are interchangeable
    • Bthrow declares an exception; throws actually throws it
    • Cthrow actually throws an exception instance; throws declares that a method may throw exceptions
    • Dthrows is for checked exceptions only; throw is for unchecked
  16. 16.What does this code print?

    public class Main {
        public static void main(String[] args) {
            System.out.println(getValue());
        }
        static String getValue() {
            try {
                throw new RuntimeException("error");
            } catch (RuntimeException e) {
                return "caught";
            } finally {
                return "finally";
            }
        }
    }
    • Acaught
    • Berror
    • Cfinally
    • DCompilation error
  17. 17.What is a StackOverflowError and what causes it?

    • AA RuntimeException from using a Stack with too many elements
    • BAn Error thrown when the call stack exceeds its limit, usually from infinite recursion
    • CA checked exception thrown when the heap is full
    • DA warning when method calls are too deep
  18. 18.What is the best practice for exception handling?

    • AUse empty catch blocks to suppress all errors silently
    • BCatch specific exceptions, log with context, and only catch what you can meaningfully handle
    • CAlways catch Exception at the top level to prevent crashes
    • DAlways rethrow every exception you catch
  19. 19.What does suppressed exceptions mean in try-with-resources?

    • AExceptions thrown during close() that are attached to the primary exception rather than replacing it
    • BExceptions silently ignored in the catch block
    • CExceptions caught but not logged
    • DExceptions thrown in the finally block that override the try exception
  20. 20.What does this code print?

    public class Main {
        public static void main(String[] args) {
            try {
                int[] arr = new int[3];
                arr[5] = 10;
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Index: " + e.getMessage());
            } catch (RuntimeException e) {
                System.out.println("Runtime: " + e.getMessage());
            } finally {
                System.out.println("done");
            }
        }
    }
    • ARuntime: 5 done
    • BIndex: 5 done
    • CIndex: Index 5 out of bounds for length 3 done
    • Ddone