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();
}
}- A. Compilation error: unreported exception ArithmeticException
- B. Compilation error because m() has no throws clause
- C. Compiles fine and prints "boom" then exits normally
- 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
- What is printed? java static void m(int x) throws java.io.IOException, InterruptedException { if (x == 0) throw new java.io.IOException()…
- What happens when this runs, assuming recurse() causes infinite recursion? java static void recurse() { recurse(); } public static void…
- What is printed? java class AEx extends Exception {} class BEx extends AEx {} // in main, wrapped in throws Exception: try { throw new…
- Does this compile (Java 7+)? java static void m() throws java.io.IOException, java.sql.SQLException { try { if (Math.random() < 2) throw…
- Does this compile? java class A { void m() throws java.io.IOException {} } class B extends A { @Override void m() throws…
- What is printed, and what happens to the RuntimeException? java static int f() { try { throw new RuntimeException("x"); } finally { return…