Multiple choice technology programming languages

What will be the result of executing the following code? // Filename; SuperclassX.java package packageX; public class SuperclassX { protected void superclassMethodX() { } int superclassVarX; } // Filename SubclassY.java 1. package packageX.packageY; 2. 3. public class SubclassY extends SuperclassX 4. { 5. SuperclassX objX = new SubclassY(); 6. SubclassY objY = new SubclassY(); 7. void subclassMethodY() 8. { 9. objY.superclassMethodX(); 10. int i; 11. i = objY.superclassVarX; 12. } 13. }

  1. Compilation error at line 5

  2. Compilation error at line 9

  3. Runtime exception at line 11

  4. None of these

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

The code compiles successfully. SubclassY extends SuperclassX (protected method, package-private field) from packageX.packageY - subclasses can access protected members. Line 5 is valid (polymorphism: SubclassY IS-A SuperclassX). Lines 9-11 access protected method and package-private field through a SubclassY reference, which is valid within the same package hierarchy. The code compiles and runs without errors, so 'None of these' (D) is correct.

AI explanation

Line 9 compiles fine because superclassMethodX() is protected, which subclasses in other packages can access. But superclassVarX has default (package-private) access, and SubclassY is in packageX.packageY — a different package from packageX — so it cannot see that inherited field. Line 11 is therefore a compile-time error, not a runtime exception, and since none of the other options describe that outcome correctly, 'None of these' is the right answer.