tgoop.com/topJavaQuizExplain/269
Last Update:
What will be the output of the code?
✅ A. Vehicle is moving at a speed of 100 km/h. This car is red
❌ B. Car is moving at a speed of 100 km/h. This car is red
❌ C. This car is red
❌ D. Compilation error
❌ E. Runtime error
❌ F. None of the above
Explanation:
The code defines two classes Vehicle and Car. Vehicle is a superclass that has a protected instance variable speed and a method move() that prints the current speed of the vehicle. Car is a subclass of Vehicle that adds a private instance variable color and a method display() that prints the color of the car.
The Main class creates a Car object myCar with a speed of 100 and a color of "red". The move() method is called on myCar, which is inherited from Vehicle, and it prints "Vehicle is moving at a speed of 100 km/h". The display() method is then called on myCar and it prints "This car is red".
Therefore, the correct answer is option A Vehicle is moving at a speed of 100 km/h and This car is red. The output first shows the message printed by the move() method of the Vehicle superclass, and then the message printed by the display() method of the Car subclass.
BY Explanations “Top Java Quiz Questions”
Share with your friend now:
tgoop.com/topJavaQuizExplain/269