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

Runtime polymorphism execution 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
🎖️
Runtime Polymorphism 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 when calling the Speak method on a base class reference pointing to a derived class object?
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 myAnimal = new Dog();
        myAnimal.Speak();
    }
}
ADog barks
BAnimal speaks
CCompile-time error
DRuntime exception
Attempts:
2 left
💡 Hint
Remember that virtual methods allow the derived class to provide its own implementation that is called at runtime.
Predict Output
intermediate
2:00remaining
Output of method hiding vs overriding
What will be printed when running this C# code?
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 obj = new Derived();
        obj.Show();
    }
}
ADerived Show
BRuntime exception
CCompile-time error
DBase Show
Attempts:
2 left
💡 Hint
The new keyword hides the base method but does not override it.
🔧 Debug
advanced
2:00remaining
Identify runtime error in polymorphic call
What runtime error will this C# code produce when executed?
C Sharp (C#)
using System;

class Animal {
    public virtual void Speak() {
        Console.WriteLine("Animal speaks");
    }
}

class Cat : Animal {
    public override void Speak() {
        throw new InvalidOperationException("Cat cannot speak now");
    }
}

class Program {
    static void Main() {
        Animal pet = new Cat();
        pet.Speak();
    }
}
AInvalidOperationException with message "Cat cannot speak now"
BNullReferenceException
CNo error, prints "Animal speaks"
DCompile-time error
Attempts:
2 left
💡 Hint
Check what the overridden method in the derived class does.
🧠 Conceptual
advanced
2:00remaining
Effect of sealed override on runtime polymorphism
Consider this C# code snippet. What will be the output when calling Speak on a Base reference to a Derived2 object?
C Sharp (C#)
using System;

class Base {
    public virtual void Speak() {
        Console.WriteLine("Base speaks");
    }
}

class Derived1 : Base {
    public sealed override void Speak() {
        Console.WriteLine("Derived1 speaks");
    }
}

class Derived2 : Derived1 {
    public override void Speak() {
        Console.WriteLine("Derived2 speaks");
    }
}

class Program {
    static void Main() {
        Base obj = new Derived2();
        obj.Speak();
    }
}
ADerived2 speaks
BDerived1 speaks
CCompile-time error
DBase speaks
Attempts:
2 left
💡 Hint
Check what the sealed keyword does to method overriding.
Predict Output
expert
2:00remaining
Output of polymorphic method with base call
What is the output of this C# program?
C Sharp (C#)
using System;

class Vehicle {
    public virtual void Start() {
        Console.WriteLine("Vehicle started");
    }
}

class Car : Vehicle {
    public override void Start() {
        base.Start();
        Console.WriteLine("Car started");
    }
}

class Program {
    static void Main() {
        Vehicle v = new Car();
        v.Start();
    }
}
ACar started
B
Vehicle started
Car started
CVehicle started
DCompile-time error
Attempts:
2 left
💡 Hint
The derived method calls the base method before printing its own message.

Practice

(1/5)
1. What does runtime polymorphism in C# allow you to do?
easy
A. Create multiple instances of the same class
B. Change variable types at runtime
C. Call derived class methods through a base class reference
D. Use static methods without creating objects

Solution

  1. Step 1: Understand runtime polymorphism concept

    Runtime polymorphism allows a base class reference to call methods overridden in derived classes.
  2. Step 2: Identify correct behavior

    This means the actual method called depends on the object's real type, not the reference type.
  3. Final Answer:

    Call derived class methods through a base class reference -> Option C
  4. Quick Check:

    Runtime polymorphism = base ref calls derived method [OK]
Hint: Think: base class ref calls derived method at runtime [OK]
Common Mistakes:
  • Confusing polymorphism with changing variable types
  • Thinking static methods are polymorphic
  • Believing polymorphism creates multiple instances
2. Which keyword is used in C# to allow a method to be overridden in a derived class?
easy
A. virtual
B. override
C. new
D. abstract

Solution

  1. Step 1: Identify keyword to enable overriding

    The base class method must be marked with virtual to allow overriding.
  2. Step 2: Understand roles of keywords

    override is used in derived classes, virtual in base classes.
  3. Final Answer:

    virtual -> Option A
  4. Quick Check:

    Base method uses virtual to allow override [OK]
Hint: Base method uses virtual; derived uses override [OK]
Common Mistakes:
  • Using override in base class instead of virtual
  • Confusing new keyword with override
  • Thinking abstract is required for all overrides
3. What is the output of this C# code?
class Animal {
  public virtual string Speak() => "Animal sound";
}
class Dog : Animal {
  public override string Speak() => "Bark";
}
class Cat : Animal {
  public override string Speak() => "Meow";
}

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

Solution

  1. Step 1: Identify object type and method called

    Variable a is of type Animal but references a Dog object.
  2. Step 2: Apply runtime polymorphism

    Since Speak is virtual and overridden in Dog, the Dog version runs, printing "Bark".
  3. Final Answer:

    Bark -> Option B
  4. Quick Check:

    Base ref calls Dog's Speak() = Bark [OK]
Hint: Base ref calls derived override method at runtime [OK]
Common Mistakes:
  • Expecting base class method output
  • Confusing object type with reference type
  • Thinking compile error due to override
4. Identify the error in this C# code related to runtime polymorphism:
class Base {
  public override void Show() {
    Console.WriteLine("Base Show");
  }
}
class Derived : Base {
  public override void Show() {
    Console.WriteLine("Derived Show");
  }
}
medium
A. Base class method must be virtual, not override
B. Derived class method cannot override base method
C. Missing semicolon after method declaration
D. No error, code is correct

Solution

  1. Step 1: Check base class method declaration

    Base class method incorrectly uses override instead of virtual.
  2. Step 2: Understand override rules

    Only derived classes use override; base class must use virtual to allow overriding.
  3. Final Answer:

    Base class method must be virtual, not override -> Option A
  4. Quick Check:

    Base method needs virtual keyword [OK]
Hint: Base method uses virtual, not override [OK]
Common Mistakes:
  • Using override in base class method
  • Thinking override is allowed without virtual
  • Ignoring method signature correctness
5. Given these classes:
class Vehicle {
  public virtual string Describe() => "Vehicle";
}
class Car : Vehicle {
  public override string Describe() => "Car";
}
class SportsCar : Car {
  public override 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());
What is the output?
hard
A. Vehicle\nCar\nSportsCar
B. Car\nCar\nCar
C. Vehicle\nVehicle\nVehicle
D. SportsCar\nSportsCar\nSportsCar

Solution

  1. Step 1: Identify actual object type for all references

    All variables v, c, and s reference a SportsCar object.
  2. Step 2: Apply runtime polymorphism for Describe()

    Since Describe is overridden in SportsCar, all calls print "SportsCar" regardless of reference type.
  3. Final Answer:

    SportsCar\nSportsCar\nSportsCar -> Option D
  4. Quick Check:

    All calls use SportsCar override [OK]
Hint: Actual object type decides method, not reference type [OK]
Common Mistakes:
  • Assuming base class method runs for base type variable
  • Confusing reference type with object type
  • Ignoring override in most derived class