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
Recall & Review
beginner
What is runtime polymorphism in C#?
Runtime polymorphism means that the method to be called is determined during program execution, not at compile time. It allows a base class reference to call derived class methods using method overriding.
Click to reveal answer
beginner
How do you enable runtime polymorphism in C#?
By declaring a method in the base class as <code>virtual</code> and overriding it in the derived class using <code>override</code>. Then, calling the method through a base class reference triggers the derived class version at runtime.
Click to reveal answer
beginner
What keyword is used in the base class method to allow overriding?
The <code>virtual</code> keyword marks a method in the base class as overridable, enabling runtime polymorphism.
Click to reveal answer
intermediate
What happens if you call a non-virtual method through a base class reference?
The base class method is called, even if the derived class has a method with the same name. This is because non-virtual methods are resolved at compile time, not runtime.
Click to reveal answer
beginner
Explain the role of the override keyword in runtime polymorphism.
The <code>override</code> keyword tells the compiler that the derived class method replaces the base class virtual method. This enables the runtime to call the correct method version based on the actual object type.
Click to reveal answer
Which keyword in C# allows a method to be overridden for runtime polymorphism?
Avirtual
Bstatic
Csealed
Dconst
✗ Incorrect
The virtual keyword marks a method as overridable, enabling runtime polymorphism.
What keyword must a derived class method use to override a base class virtual method?
Anew
Boverride
Cvirtual
Dabstract
✗ Incorrect
The override keyword is used in the derived class to replace the base class virtual method.
If a method is not marked virtual, what happens when called through a base class reference?
ADerived class method is called
BCompile error occurs
CRuntime error occurs
DBase class method is called
✗ Incorrect
Non-virtual methods are resolved at compile time, so the base class method is called.
What determines which method is called in runtime polymorphism?
ARuntime type of the object
BCompile-time type of the reference
CMethod name length
DNumber of parameters
✗ Incorrect
The actual object type at runtime decides which overridden method is executed.
Which of these is NOT required for runtime polymorphism in C#?
ABase class method marked virtual
BCalling method through base class reference
CMethod marked as static
DDerived class method marked override
✗ Incorrect
Static methods cannot be overridden and do not support runtime polymorphism.
Describe how runtime polymorphism works in C# with an example.
Think about how a base class pointer can call a method that behaves differently depending on the actual object.
You got /4 concepts.
Explain the difference between virtual and non-virtual methods in the context of runtime polymorphism.
Consider what happens when you call a method through a base class reference.
You got /4 concepts.
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
Step 1: Understand runtime polymorphism concept
Runtime polymorphism allows a base class reference to call methods overridden in derived classes.
Step 2: Identify correct behavior
This means the actual method called depends on the object's real type, not the reference type.
Final Answer:
Call derived class methods through a base class reference -> Option C
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
Step 1: Identify keyword to enable overriding
The base class method must be marked with virtual to allow overriding.
Step 2: Understand roles of keywords
override is used in derived classes, virtual in base classes.
Final Answer:
virtual -> Option A
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
Step 1: Identify object type and method called
Variable a is of type Animal but references a Dog object.
Step 2: Apply runtime polymorphism
Since Speak is virtual and overridden in Dog, the Dog version runs, printing "Bark".
Final Answer:
Bark -> Option B
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
Step 1: Check base class method declaration
Base class method incorrectly uses override instead of virtual.
Step 2: Understand override rules
Only derived classes use override; base class must use virtual to allow overriding.
Final Answer:
Base class method must be virtual, not override -> Option A
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
Step 1: Identify actual object type for all references
All variables v, c, and s reference a SportsCar object.
Step 2: Apply runtime polymorphism for Describe()
Since Describe is overridden in SportsCar, all calls print "SportsCar" regardless of reference type.
Final Answer:
SportsCar\nSportsCar\nSportsCar -> Option D
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