Single Choice Not Set

QWhat will be the result of compiling and executing the following program code?
class Vehicle{
      public void printSound(){
            System.out.print("vehicle");
      }
}
class Car extends Vehicle{
      public void printSound(){
            System.out.print("car");
      }
}
class Bike extends Vehicle{
      public void printSound(){
            System.out.print("bike");
      }
}
public class Test{
      public static void main(String[] args){
            Vehicle v = new Car();
            Bike b = (Bike) v;
        
            v.printSound();
            b.printSound();
      }
}

ID: #2269 Inheritance in Java MCQ 2,182 views
Question Info
#2269Q ID
Not SetDifficulty
Inheritance in Java MCQTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A Compilation fails.
  • B ClassCastException exception is thrown at runtime.
  • C "vehiclecar" is printed.
  • D "vehiclebike" is printed.
Correct Answer

Explanation

ClassCastException exception is thrown at runtime.

Share This Question

Challenge a friend or share with your study group.