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

Method overriding with virtual and override in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Virtual Override Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of overridden method call

What is the output of this C# program?

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();
    }
}
AAnimal speaks
BRuntime exception
CCompile-time error
DDog barks
Attempts:
2 left
💡 Hint

Remember that virtual methods allow derived classes to provide their own implementation.

Predict Output
intermediate
2:00remaining
Output when base method is not virtual

What will this 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();
    }
}
ABase Show
BDerived Show
CCompile-time error
DRuntime exception
Attempts:
2 left
💡 Hint

Check if the base method is virtual or not.

Predict Output
advanced
2:00remaining
Output with multiple overrides and base calls

What is the output of this program?

C Sharp (C#)
using System;
class A {
    public virtual void Print() {
        Console.WriteLine("A");
    }
}
class B : A {
    public override void Print() {
        Console.WriteLine("B");
        base.Print();
    }
}
class C : B {
    public override void Print() {
        Console.WriteLine("C");
        base.Print();
    }
}
class Program {
    static void Main() {
        A obj = new C();
        obj.Print();
    }
}
A
C
B
A
B
A
B
C
C
C
A
B
D
B
C
A
Attempts:
2 left
💡 Hint

Follow the calls from the most derived class up to base classes.

Predict Output
advanced
2:00remaining
Error when overriding without override keyword

What error does this code produce?

C Sharp (C#)
using System;
class Parent {
    public virtual void Display() {
        Console.WriteLine("Parent Display");
    }
}
class Child : Parent {
    public void Display() {
        Console.WriteLine("Child Display");
    }
}
class Program {
    static void Main() {
        Parent p = new Child();
        p.Display();
    }
}
ARuntime exception: Method not found
BOutput: Parent Display
CCompile-time error: Child.Display must use override keyword
DOutput: Child Display
Attempts:
2 left
💡 Hint

Check if the child method overrides or hides the base method.

🧠 Conceptual
expert
2:00remaining
Why use virtual and override keywords?

Which statement best explains why C# uses virtual and override keywords for method overriding?

ATo automatically generate base class methods in derived classes
BTo make methods run faster by skipping virtual dispatch
CTo allow the compiler to check that a derived class method correctly overrides a base class virtual method
DTo prevent any method in derived classes from changing base class behavior
Attempts:
2 left
💡 Hint

Think about safety and clarity in overriding methods.