Multiple choice technology programming languages

package polymorphism; class TestSupe { static int p = 100; } public class TestPoly1 extends TestSupe { static int p =1000; public static void main(String[] args) { TestPoly1 TP = new TestPoly1(); TP.p = 400; TestSupe TS = new TestSupe(); TS.p = 300; TestSupe TSP = new TestPoly1(); TSP.p = 500; System.out.print("TestSupe = " + TestSupe.p); System.out.print(" ,TestPoly = " + TestPoly1.p); System.out.print(" ,TP = " + TP.p); System.out.print(" ,TS = " + TS.p); System.out.print(" ,TSP = " + TSP.p); } }

  1. TestSupe = 300 ,TestPoly = 400 ,TP = 400 ,TS = 300 ,TSP = 500

  2. TestSupe = 500 ,TestPoly = 400 ,TP = 400 ,TS = 500 ,TSP = 500

  3. TestSupe = 500 ,TestPoly = 400 ,TP = 400 ,TS = 300 ,TSP = 500

  4. TestSupe = 300 ,TestPoly = 400 ,TP = 500 ,TS = 500 ,TSP = 500

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

In Java, static variables are NOT polymorphic - they are bound to the reference type, not the actual object type. When we assign TSP.p = 500, TSP is declared as TestSupe, so it sets TestSupe.p to 500. Similarly, TP.p = 400 sets TestPoly1.p to 400. When printing static variables using the class name (TestSupe.p or TestPoly1.p), we access the variable for that specific class. This is static variable hiding, not overriding.

AI explanation

Static fields belong to the class, not the instance, and — crucially — which class's static field gets accessed through a reference is determined by the declared (reference) type, not the runtime object type. So TSP.p = 500 (where TSP is declared as TestSupe even though it holds a TestPoly1 object) actually assigns to TestSupe.p, overwriting the earlier TS.p = 300. Similarly, TP.p = 400 assigns to TestPoly1.p since TP is declared as TestPoly1. Working through all five print statements with this rule gives TestSupe = 500, TestPoly = 400, TP = 400, TS = 500, TSP = 500.