0
0
C Sharp (C#)programming~20 mins

Virtual method dispatch mechanism in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.