ProgrammingHardTCS NQT · Infosys pattern

What happens when this runs?

What happens when this runs?
class Test {
    static void m() { throw new ArithmeticException("boom"); }
    public static void main(String[] args) {
        m();
    }
}
  1. A. Compilation error: unreported exception ArithmeticException
  2. B. Compilation error because m() has no throws clause
  3. C. Compiles fine and prints "boom" then exits normally
  4. D. Compiles fine; ArithmeticException propagates uncaught and terminates the program with a stack trace

Correct answer: D. Compiles fine; ArithmeticException propagates uncaught and terminates the program with a stack trace

ArithmeticException is unchecked (extends RuntimeException), so neither declaring throws nor catching it is required for compilation. At runtime it propagates uncaught, printing a stack trace and terminating the JVM.

More OOP with C++ and Java questions

Exams that ask this topic