Given: 1. class TestA { 2. public void start() { System.out.println(”TestA”); } 3. } 4. public class TestB extends TestA { 5. public void start() { System.out.println(”TestB”); } 6. public static void main(String[] args) { 7. ((TestA)new TestB()).start(); 8. } 9. } What is the result?

  1. TestA

  2. TestB

  3. Compilation fails.

  4. An exception is thrown at runtime.


Correct Option: B
Explanation:

To solve this question, the user needs to understand inheritance and polymorphism in Java.

The TestB class extends the TestA class and overrides its start method. The main method creates a new instance of TestB and casts it to a TestA object.

The start method is called on this TestA object, but since the object being referred to is actually an instance of TestB, the overridden start method in TestB is called instead of TestA's original start method.

Since TestB's start method prints "TestB" to the console, the resulting output when the program is run will be:

TestB

Therefore, the answer is: B.

Find more quizzes: