Multiple choice technology programming languages

package overriding; class Super3 { void testDivide(int i, int j) throws Exception { double p = i / j; System.out.println("p in Super =" + p); } } public class TestOverrideSub3 extends Super3 { void testDivide(int i, int j) throws ArithmeticException { double p = i / j; System.out.println("p in Sub =" + p); } public static void main(String[] args) { Super3 s3 = new TestOverrideSub3(); try { s3.testDivide(10, 5); } catch (Exception e) { System.out.println("Exception " + e.getMessage()); } } }

  1. p in Super =2.0

  2. p in Sub =2.0

  3. Compiler error

  4. Exception

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

The subclass narrows the thrown exception from Exception to ArithmeticException, which is allowed because ArithmeticException is a subclass of Exception. Due to polymorphism, the subclass version runs, printing p in Sub.

AI explanation

testDivide in the subclass has the exact same signature as the superclass method, so it overrides it (not overloads). Java resolves overridden instance methods using the runtime type of the object, not the reference type it's stored in — here that's TestOverrideSub3, even though the reference variable is typed Super3. So the subclass's version runs, printing "p in Sub =2.0" since 10/5 is 2 (int division) then widened to double. No exception occurs because the divisor isn't zero.