Challenge - 5 Problems
Virtual Dispatch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Remember that virtual methods call the most derived override at runtime.
✗ Incorrect
The variable 'a' is of type Animal but refers to a Dog object. Because Speak() is virtual and overridden in Dog, the Dog's Speak() method runs, printing 'Dog barks'.
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Non-virtual methods are called based on the variable's declared type.
✗ Incorrect
The Show() method is not virtual. The variable 'b' is of type Base, so Base.Show() is called, printing 'Base Show'.
🔧 Debug
advanced2: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(); } }
Attempts:
2 left
💡 Hint
Look at what the overridden method does when called.
✗ Incorrect
The Child's Display method throws a NullReferenceException explicitly. When p.Display() runs, this exception is thrown at runtime.
🧠 Conceptual
advanced2: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(); } }
Attempts:
2 left
💡 Hint
The sealed keyword prevents further overrides.
✗ Incorrect
Class B seals the Print method, so class C cannot override it. This causes a compilation error.
❓ Predict Output
expert3: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(); } }
Attempts:
2 left
💡 Hint
Virtual methods call the most derived override regardless of reference type.
✗ Incorrect
Both obj and yobj refer to Z instances. Foo() is overridden in Z, so both calls print 'Z Foo'.