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

Virtual method dispatch mechanism 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 Dispatch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of virtual method call with base reference
What is the output of the following C# code?
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();
    }
}
ADog barks
BRuntime exception
CCompilation error
DAnimal speaks
Attempts:
2 left
💡 Hint
Remember that virtual methods call the most derived override at runtime.
Predict Output
intermediate
2:00remaining
Output when calling non-virtual method on derived class
What will this C# 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();
    }
}
ADerived Show
BBase Show
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Non-virtual methods are called based on the variable's declared type.
🔧 Debug
advanced
2:00remaining
Identify the runtime error in virtual method dispatch
What error will this C# code produce when run?
C Sharp (C#)
using System;
class Parent {
    public virtual void Display() {
        Console.WriteLine("Parent Display");
    }
}
class Child : Parent {
    public override void Display() {
        throw new NullReferenceException();
    }
}
class Program {
    static void Main() {
        Parent p = new Child();
        p.Display();
    }
}
ANo output, program runs normally
BCompilation error due to override
CStackOverflowException at runtime
DNullReferenceException at runtime
Attempts:
2 left
💡 Hint
Look at what the overridden method does when called.
🧠 Conceptual
advanced
2:00remaining
Effect of sealed keyword on virtual method dispatch
Consider this C# code snippet. What will be the output?
C Sharp (C#)
using System;
class A {
    public virtual void Print() {
        Console.WriteLine("A Print");
    }
}
class B : A {
    public sealed override void Print() {
        Console.WriteLine("B Print");
    }
}
class C : B {
    public override void Print() {
        Console.WriteLine("C Print");
    }
}
class Program {
    static void Main() {
        A obj = new C();
        obj.Print();
    }
}
AA Print
BB Print
CCompilation error
DC Print
Attempts:
2 left
💡 Hint
The sealed keyword prevents further overrides.
Predict Output
expert
3:00remaining
Output of virtual method calls with multiple inheritance levels
What is the output of this C# program?
C Sharp (C#)
using System;
class X {
    public virtual void Foo() {
        Console.WriteLine("X Foo");
    }
}
class Y : X {
    public override void Foo() {
        Console.WriteLine("Y Foo");
    }
}
class Z : Y {
    public override void Foo() {
        Console.WriteLine("Z Foo");
    }
}
class Program {
    static void Main() {
        X obj = new Z();
        obj.Foo();
        Y yobj = new Z();
        yobj.Foo();
    }
}
AZ Foo\nZ Foo
BX Foo\nY Foo
CY Foo\nZ Foo
DCompilation error
Attempts:
2 left
💡 Hint
Virtual methods call the most derived override regardless of reference type.

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

  1. Step 1: Understand virtual method purpose

    Virtual methods allow child classes to override a method and provide their own implementation.
  2. 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.
  3. Final Answer:

    It allows the program to decide at runtime which method version to call. -> Option D
  4. Quick Check:

    Virtual method dispatch = runtime method choice [OK]
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

  1. Step 1: Recall virtual method syntax

    The keyword virtual comes after the access modifier and before the return type and method name.
  2. 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.
  3. Final Answer:

    public virtual void Display() { } -> Option C
  4. 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

  1. Step 1: Identify method overriding

    The Derived class overrides the virtual method GetName from Base.
  2. 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.
  3. Final Answer:

    Derived -> Option B
  4. 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

  1. Step 1: Check method overriding rules

    To override a virtual method, the child method must use override keyword.
  2. Step 2: Analyze given code

    Dog's Speak method lacks override, so it hides base method instead of overriding. Virtual dispatch calls base method.
  3. Final Answer:

    Dog's Speak method should be marked override to override base virtual method. -> Option A
  4. 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

  1. Step 1: Understand virtual method usage

    Declaring Draw() as virtual in base allows derived classes to override it.
  2. Step 2: Use polymorphism in list iteration

    Calling Draw() on each Shape reference triggers virtual dispatch, running the correct derived method.
  3. Final Answer:

    Declare Draw() as virtual in Shape, override in derived, call Draw() on each Shape reference. -> Option A
  4. 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
  • Calling base method explicitly ignoring overrides