Bird
0
0

Consider this code:

hard🚀 Application Q9 of 15
C Sharp (C#) - Inheritance
Consider this code:
abstract class Vehicle {
  public abstract string Move();
}
class Car : Vehicle {
  public override string Move() => "Car moves";
}
class Bike : Vehicle {
  public override string Move() => "Bike moves";
}
class Program {
  static void Main() {
    Vehicle v = new Car();
    System.Console.WriteLine(v.Move());
    v = new Bike();
    System.Console.WriteLine(v.Move());
  }
}

What is the output?
ACar moves Bike moves
BVehicle moves Vehicle moves
CCompilation error due to abstract class
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand abstract and override

    Abstract method Move() must be overridden in derived classes with concrete implementations.
  2. Step 2: Trace method calls

    Variable v points to Car then Bike, calling their respective Move() methods.
  3. Final Answer:

    Car moves Bike moves -> Option A
  4. Quick Check:

    Abstract override calls derived method correctly [OK]
Quick Trick: Abstract methods require override; calls run derived implementations [OK]
Common Mistakes:
MISTAKES
  • Expecting base abstract method output
  • Thinking abstract class cannot be instantiated (correct)
  • Confusing compile and runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes