Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Method overriding with virtual and override in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Virtual Override Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of overridden method call

What is the output of this C# program?

C Sharp (C#)
using System;
class Animal {
    public virtual void Speak() {
        Console.WriteLine("Animal speaks");
    }
}
class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("Dog barks");
    }
}
class Program {
    static void Main() {
        Animal a = new Dog();
        a.Speak();
    }
}
AAnimal speaks
BRuntime exception
CCompile-time error
DDog barks
Attempts:
2 left
💡 Hint

Remember that virtual methods allow derived classes to provide their own implementation.

Predict Output
intermediate
2:00remaining
Output when base method is not virtual

What will this program print?

C Sharp (C#)
using System;
class Base {
    public void Show() {
        Console.WriteLine("Base Show");
    }
}
class Derived : Base {
    public new void Show() {
        Console.WriteLine("Derived Show");
    }
}
class Program {
    static void Main() {
        Base b = new Derived();
        b.Show();
    }
}
ABase Show
BDerived Show
CCompile-time error
DRuntime exception
Attempts:
2 left
💡 Hint

Check if the base method is virtual or not.

Predict Output
advanced
2:00remaining
Output with multiple overrides and base calls

What is the output of this program?

C Sharp (C#)
using System;
class A {
    public virtual void Print() {
        Console.WriteLine("A");
    }
}
class B : A {
    public override void Print() {
        Console.WriteLine("B");
        base.Print();
    }
}
class C : B {
    public override void Print() {
        Console.WriteLine("C");
        base.Print();
    }
}
class Program {
    static void Main() {
        A obj = new C();
        obj.Print();
    }
}
A
C
B
A
B
A
B
C
C
C
A
B
D
B
C
A
Attempts:
2 left
💡 Hint

Follow the calls from the most derived class up to base classes.

Predict Output
advanced
2:00remaining
Error when overriding without override keyword

What error does this code produce?

C Sharp (C#)
using System;
class Parent {
    public virtual void Display() {
        Console.WriteLine("Parent Display");
    }
}
class Child : Parent {
    public void Display() {
        Console.WriteLine("Child Display");
    }
}
class Program {
    static void Main() {
        Parent p = new Child();
        p.Display();
    }
}
ARuntime exception: Method not found
BOutput: Parent Display
CCompile-time error: Child.Display must use override keyword
DOutput: Child Display
Attempts:
2 left
💡 Hint

Check if the child method overrides or hides the base method.

🧠 Conceptual
expert
2:00remaining
Why use virtual and override keywords?

Which statement best explains why C# uses virtual and override keywords for method overriding?

ATo automatically generate base class methods in derived classes
BTo make methods run faster by skipping virtual dispatch
CTo allow the compiler to check that a derived class method correctly overrides a base class virtual method
DTo prevent any method in derived classes from changing base class behavior
Attempts:
2 left
💡 Hint

Think about safety and clarity in overriding methods.

Practice

(1/5)
1. What keyword in C# allows a method in a base class to be changed by a derived class?
easy
A. new
B. override
C. virtual
D. abstract

Solution

  1. Step 1: Understand base class method flexibility

    The virtual keyword marks a method in the base class as changeable by derived classes.
  2. Step 2: Differentiate from other keywords

    override is used in derived classes, new hides methods, and abstract requires implementation.
  3. Final Answer:

    virtual -> Option C
  4. Quick Check:

    Base method change = virtual [OK]
Hint: Base class method change uses virtual keyword [OK]
Common Mistakes:
  • Confusing override with virtual
  • Using new instead of virtual for overriding
  • Thinking abstract allows method change without implementation
2. Which of the following is the correct syntax to override a virtual method named Display in a derived class?
easy
A. public void Display() { }
B. public virtual void Display() { }
C. public new void Display() { }
D. public override void Display() { }

Solution

  1. Step 1: Identify override syntax

    To change a virtual method in a derived class, use override before the method signature.
  2. Step 2: Eliminate other options

    The plain public void Display() { } lacks the override keyword, public virtual void Display() { } incorrectly uses virtual in the derived class, and public new void Display() { } hides the base method but doesn't override it for polymorphism.
  3. Final Answer:

    public override void Display() { } -> Option D
  4. Quick Check:

    Override method uses override keyword [OK]
Hint: Override methods must use override keyword [OK]
Common Mistakes:
  • Omitting override keyword in derived class
  • Using virtual instead of override in derived class
  • Using new keyword instead of override
3. What will be the output of the following code?
class Animal {
  public virtual string Speak() { return "Animal sound"; }
}
class Dog : Animal {
  public override string Speak() { return "Bark"; }
}
class Cat : Animal {
  public override string Speak() { return "Meow"; }
}

Animal a = new Dog();
Animal b = new Cat();
Console.WriteLine(a.Speak());
Console.WriteLine(b.Speak());
medium
A. Bark Meow
B. Animal sound Animal sound
C. Bark Animal sound
D. Animal sound Meow

Solution

  1. Step 1: Understand virtual and override behavior

    Because Speak is virtual and overridden, the derived class method runs even when referenced as base type.
  2. Step 2: Trace the output calls

    a is a Dog instance, so Speak() returns "Bark"; b is a Cat instance, so it returns "Meow".
  3. Final Answer:

    Bark Meow -> Option A
  4. Quick Check:

    Override method output = Bark, Meow [OK]
Hint: Virtual method calls use derived override at runtime [OK]
Common Mistakes:
  • Expecting base class method output
  • Ignoring override effect on base class reference
  • Confusing new keyword behavior with override
4. Identify the error in this code snippet:
class Base {
  public virtual void Show() { Console.WriteLine("Base"); }
}
class Derived : Base {
  public void Show() { Console.WriteLine("Derived"); }
}

Base obj = new Derived();
obj.Show();
medium
A. No error; output is Base
B. No error; output is Derived
C. Compile-time error: missing override keyword
D. Runtime error: method not found

Solution

  1. Step 1: Check method overriding rules

    The derived class method Show does not use override, so it hides the base method instead of overriding.
  2. Step 2: Determine method called by base reference

    Because Show is virtual in base but not overridden, calling obj.Show() calls base class method, outputting "Base".
  3. Final Answer:

    No error; output is Base -> Option A
  4. Quick Check:

    Missing override means base method runs [OK]
Hint: Override keyword needed to replace virtual method [OK]
Common Mistakes:
  • Assuming method hides override automatically
  • Expecting Derived output without override
  • Thinking missing override causes compile error
5. Given the classes below, what will be the output?
class Vehicle {
  public virtual string Describe() => "Vehicle";
}
class Car : Vehicle {
  public override string Describe() => "Car";
}
class SportsCar : Car {
  public new string Describe() => "SportsCar";
}

Vehicle v = new SportsCar();
Car c = new SportsCar();
SportsCar s = new SportsCar();

Console.WriteLine(v.Describe());
Console.WriteLine(c.Describe());
Console.WriteLine(s.Describe());
hard
A. Vehicle Car SportsCar
B. Car Car SportsCar
C. Vehicle Vehicle Vehicle
D. SportsCar SportsCar SportsCar

Solution

  1. Step 1: Understand new vs override

    SportsCar uses new to hide Describe, not override it. So base class virtual dispatch applies only up to Car.
  2. Step 2: Trace each call

    v.Describe() calls Vehicle reference to SportsCar instance, but virtual dispatch stops at Car override, so returns "Car".
    c.Describe() calls Car reference to SportsCar, same as above, returns "Car".
    s.Describe() calls SportsCar reference, so calls hidden method returning "SportsCar".
  3. Final Answer:

    Car Car SportsCar -> Option B
  4. Quick Check:

    new hides method, override dispatches virtual [OK]
Hint: new hides method; override participates in virtual dispatch [OK]
Common Mistakes:
  • Expecting new method to override virtual dispatch
  • Confusing new with override behavior
  • Assuming base reference calls hidden method