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 the virtual method dispatch mechanism in C#?
It is the process where the program decides at runtime which version of a virtual method to call, based on the actual object's type, not the reference type.
Click to reveal answer
beginner
How do you declare a method as virtual in C#?
By using the virtual keyword before the method return type in the base class.
Click to reveal answer
beginner
What keyword is used to override a virtual method in a derived class?
The override keyword is used to provide a new implementation of a virtual method in a derived class.
Click to reveal answer
intermediate
Why is virtual method dispatch important in object-oriented programming?
It enables polymorphism, allowing objects of different classes to be treated uniformly while invoking their specific behaviors at runtime.
Click to reveal answer
intermediate
What happens if a method is not declared virtual but is redefined in a derived class?
The method in the derived class hides the base class method, but calls are resolved at compile time based on the reference type, not the object type.
Click to reveal answer
Which keyword in C# allows a method to be overridden in derived classes?
Avirtual
Boverride
Cnew
Dabstract
✗ Incorrect
The virtual keyword marks a method in the base class as overridable.
What determines which method implementation is called when using virtual methods?
AThe object type at runtime
BThe reference type
CThe method name only
DThe compiler settings
✗ Incorrect
Virtual method dispatch uses the actual object's type at runtime to decide which method to call.
If a base class method is not virtual, what happens when a derived class defines a method with the same name?
AThe derived method overrides the base method
BThe base method is called always
CA compile error occurs
DThe derived method hides the base method
✗ Incorrect
Without virtual, the derived method hides the base method, and calls depend on reference type.
Which keyword must be used in the derived class to provide a new implementation of a virtual method?
Avirtual
Bnew
Coverride
Dsealed
✗ Incorrect
The override keyword tells the compiler this method replaces the base virtual method.
What is the main benefit of virtual method dispatch?
Explain how virtual method dispatch works in C# with an example.
Think about a base class with a virtual method and a derived class overriding it.
You got /4 concepts.
Describe the difference between method overriding and method hiding in C#.
Consider what happens when the base method is virtual or not.
You got /4 concepts.
Practice
(1/5)
1. What does the virtual method dispatch mechanism in C# primarily allow? virtual methods let child classes provide their own version of a method. What is the main benefit?
easy
A. It forces the program to call the base class method only.
B. It makes all methods static by default.
C. It disables method overriding in child classes.
D. It allows the program to decide at runtime which method version to call.
Solution
Step 1: Understand virtual method purpose
Virtual methods allow child classes to override a method and provide their own implementation.
Step 2: Identify when method is chosen
The actual method called is decided at runtime, based on the object's real type, not the variable's type.
Final Answer:
It allows the program to decide at runtime which method version to call. -> Option D
Hint: Virtual means runtime method choice, not compile-time [OK]
Common Mistakes:
Thinking method is fixed at compile time
Confusing virtual with static methods
Assuming base method always runs
2. Which of the following is the correct syntax to declare a virtual method in a C# class?
easy
A. virtual public void Display() { }
B. public void virtual Display() { }
C. public virtual void Display() { }
D. public override void Display() { }
Solution
Step 1: Recall virtual method syntax
The keyword virtual comes after the access modifier and before the return type and method name.
Step 2: Check each option
public virtual void Display() { } matches correct syntax: public virtual void Display() { }. Options B and C have wrong order, D uses override which is for overriding, not declaring virtual.
Final Answer:
public virtual void Display() { } -> Option C
Quick Check:
virtual keyword after access modifier [OK]
Hint: virtual keyword goes right after access modifier [OK]
Common Mistakes:
Placing virtual after method name
Using override instead of virtual to declare
Wrong keyword order
3. Consider the following code:
class Base {
public virtual string GetName() => "Base";
}
class Derived : Base {
public override string GetName() => "Derived";
}
Base obj = new Derived();
Console.WriteLine(obj.GetName());
What will be the output?
medium
A. Base
B. Derived
C. Compile-time error
D. Runtime exception
Solution
Step 1: Identify method overriding
The Derived class overrides the virtual method GetName from Base.
Step 2: Understand virtual dispatch
The variable obj is of type Base but holds a Derived object. Virtual dispatch calls the Derived version at runtime.
Final Answer:
Derived -> Option B
Quick Check:
Virtual method calls child's override [OK]
Hint: Virtual calls run child's method if overridden [OK]
Common Mistakes:
Assuming base method runs due to variable type
Confusing compile-time and runtime binding
Expecting errors from override
4. What is wrong with this code snippet?
class Animal {
public virtual void Speak() {
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal {
public void Speak() {
Console.WriteLine("Dog barks");
}
}
Animal a = new Dog();
a.Speak();
medium
A. Dog's Speak method should be marked override to override base virtual method.
B. Animal's Speak method should not be virtual.
C. Dog's Speak method should be static.
D. No error; code runs and prints "Dog barks".
Solution
Step 1: Check method overriding rules
To override a virtual method, the child method must use override keyword.
Step 2: Analyze given code
Dog's Speak method lacks override, so it hides base method instead of overriding. Virtual dispatch calls base method.
Final Answer:
Dog's Speak method should be marked override to override base virtual method. -> Option A
Quick Check:
Override keyword needed to override virtual method [OK]
Hint: Override keyword required to override virtual method [OK]
Common Mistakes:
Forgetting override keyword in child method
Assuming method hides base automatically
Confusing virtual and override keywords
5. You have a base class Shape with a virtual method Draw(). Two derived classes Circle and Square override Draw(). You want to write a method that takes a list of Shape objects and calls Draw() on each, ensuring the correct derived method runs.
Which approach correctly uses virtual method dispatch to achieve this?
hard
A. Declare Draw() as virtual in Shape, override in derived classes, then call Draw() on each Shape reference in the list.
B. Declare Draw() as static in Shape and call it directly on the class.
C. Do not use virtual; instead, use type checking and cast each object to call the correct method.
D. Override Draw() in derived classes but call Shape.Draw() explicitly for all objects.
Solution
Step 1: Understand virtual method usage
Declaring Draw() as virtual in base allows derived classes to override it.
Step 2: Use polymorphism in list iteration
Calling Draw() on each Shape reference triggers virtual dispatch, running the correct derived method.
Final Answer:
Declare Draw() as virtual in Shape, override in derived, call Draw() on each Shape reference. -> Option A
Quick Check:
Virtual + override + call on base type = correct method run [OK]
Hint: Use virtual + override, call on base type for correct method [OK]
Common Mistakes:
Using static methods which don't support polymorphism
Manually casting instead of relying on virtual dispatch