Analyze the following code snippet. Which statement is true?
Java interview question for Advanced practice.
Answer
The code compiles and runs successfully, demonstrating polymorphism and casting.
Explanation
The correct answer is D. The code compiles and runs without error. The reference a is of type Animal but points to a Dog object. The calls a.makeSound() and a.eat() are polymorphic; they invoke the overridden methods in the Dog class at runtime. The cast ((Pet)a) is successful because the Dog object implements the Pet interface. Option C is incorrect because the overridden eat() method in Dog is called, printing 'Dog is eating'.