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. }

  1. TestA

  2. TestB

  3. Compilation fails.

  4. An exception is thrown at runtime.


Correct Option: B

AI Explanation

To answer this question, we need to understand the concept of method overriding in Java.

In the given code, we have two classes: TestA and TestB. TestB extends TestA, which means TestB is a subclass of TestA. TestB overrides the start() method of TestA.

In the main method, we create a new instance of TestB and cast it to TestA: (TestA)new TestB(). Then we call the start() method on this instance.

Since TestB overrides the start() method, the start() method of TestB will be called. Therefore, the output of the program will be "TestB".

Now, let's go through each option to understand why it is correct or incorrect:

Option A) TestA - This option is incorrect because the start() method of TestB is called, not the start() method of TestA.

Option B) TestB - This option is correct because the start() method of TestB is called, and the output of the program will be "TestB".

Option C) Compilation fails - This option is incorrect because there is no compilation error in the given code.

Option D) An exception is thrown at runtime - This option is incorrect because the program executes without throwing any exception.

The correct answer is option B.

Find more quizzes: