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

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(); } } Correct Answer ClassCastException exception is thrown at runtime.

Answer: Option 2

Let'

Related Questions

What is the result of compiling and running the following code?
public class Tester{static int x = 4;public Tester(){System.out.print(this.x); // line 1Tester();}public static void Tester(){ // line 2System.out.print(this.x); // line 3}public static void main(String... args){ // line 4new Tester();}}