A

Admin • 825.56K Points
Coach

Q. What 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();
}
}

  • (A) Compilation fails.
  • (B) ClassCastException exception is thrown at runtime.
  • (C) "vehiclecar" is printed.
  • (D) "vehiclecar" is printed. D. "vehiclebike" is printed.
  • Correct Answer - Option(B)
  • Views: 13
  • Filed under category Java
  • Hashtags:

No solution found for this question.
Add Solution and get +2 points.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.