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


Correct Option: B

AI Explanation

To answer this question, we need to understand the concept of static variables and inheritance in Java.

In the given code, there are two classes: TestSupe and TestPoly1. TestPoly1 extends TestSupe, which means that TestPoly1 inherits all the variables and methods from TestSupe.

Let's go through each option to understand why it is correct or incorrect:

Option A) TestSupe = 300, TestPoly = 400, TP = 400, TS = 300, TSP = 500 This option is incorrect. Although the values of p are modified in different instances, the static variable p belongs to the class itself, not to its instances. Therefore, the value of p will be the same for all instances of the class.

Option B) TestSupe = 500, TestPoly = 400, TP = 400, TS = 500, TSP = 500 This option is correct. The static variable p in TestSupe is modified to 300, but then it is modified to 500 when accessing it through an instance of TestPoly1 (TSP.p). The static variable p in TestPoly1 is set to 400 and remains the same. The variable p in the instance TP is set to 400, and the variable p in the instance TS is set to 500.

Option C) TestSupe = 500, TestPoly = 400, TP = 400, TS = 300, TSP = 500 This option is incorrect. The value of TS.p is modified to 300, but the value of TSP.p is modified to 500. This suggests that TSP is an instance of TestPoly1, so the value of TestPoly1.p should be 500, not 400.

Option D) TestSupe = 300, TestPoly = 400, TP = 500, TS = 500, TSP = 500 This option is incorrect. The value of TP.p is modified to 400, and the value of TS.p is modified to 500. This suggests that TS is an instance of TestPoly1, so the value of TestPoly1.p should be 500, not 400.

The correct answer is Option B.

Find more quizzes: