Multiple choice technology programming languages

package exceptions; public class TestException3 { static double testException(int x, int y) { return (x/y); } public static void main(String[] args) { try { double p = testException(10,0); System.out.println("p = "+p); } catch(Exception e) { System.out.println("Exception happened "+e.getMessage()); System.exit(0); } finally { System.out.println("In finally"); } } }

  1. Compiler Error

  2. Exception happened / by zero

  3. Exception happened / by zero In finally

  4. In finally

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The code divides 10 by 0, throwing ArithmeticException '/ by zero'. This is caught by the Exception handler, which prints the message and calls System.exit(0). System.exit() terminates the JVM immediately, so the finally block never executes. Output is only the exception message.

AI explanation

testException(10,0) performs integer division 10/0, which throws an ArithmeticException at runtime (not a compile error — division by zero for integers is only caught at runtime, not compile time). The catch block catches Exception and calls e.getMessage(), which for this exception returns the string '/ by zero', so it prints 'Exception happened / by zero'. Critically, the catch block then calls System.exit(0), which terminates the JVM immediately — the finally block does NOT execute after System.exit(), unlike normal control flow. So 'In finally' is never printed, making the exact output 'Exception happened / by zero' only, matching the marked answer. The other options wrongly assume finally still runs or that it's a compiler error.