Bird
0
0

Given these classes:

hard🚀 Application Q15 of 15
C Sharp (C#) - Inheritance
Given these classes:
class Vehicle { public virtual string Move() => "Moving"; } class Car : Vehicle { public override string Move() => "Car is moving"; } class Bike : Vehicle { }

What will be the output of:
Vehicle v1 = new Car(); Vehicle v2 = new Bike(); Console.WriteLine(v1.Move()); Console.WriteLine(v2.Move());
ACar is moving\nMoving
BMoving\nMoving
CCar is moving\nBike is moving
DCompile error due to missing override
Step-by-Step Solution
Solution:
  1. Step 1: Understand virtual and override behavior

    Car overrides Move(), so v1.Move() calls Car's version. Bike does not override, so v2.Move() calls Vehicle's version.
  2. Step 2: Predict output lines

    v1.Move() outputs "Car is moving"; v2.Move() outputs "Moving".
  3. Final Answer:

    Car is moving\nMoving -> Option A
  4. Quick Check:

    Override changes output, no override uses base = D [OK]
Quick Trick: Override changes method output; no override uses base method [OK]
Common Mistakes:
MISTAKES
  • Expecting Bike to output 'Bike is moving'
  • Thinking missing override causes compile error
  • Confusing which method is called

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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