Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Inheritance
What will be the output of this C# code?
class Base { public virtual void Show() { Console.WriteLine("Base Show"); } } class Derived : Base { public override void Show() { Console.WriteLine("Derived Show"); } } class Program { static void Main() { Base b = new Derived(); b.Show(); } }
ADerived Show
BNo output
CCompilation error
DBase Show
Step-by-Step Solution
Solution:
  1. Step 1: Understand virtual and override keywords

    Base class method Show() is virtual, allowing Derived to override it with its own version.
  2. Step 2: Analyze method call on Base reference

    Though b is Base type, it points to Derived object, so Derived's Show() runs, printing "Derived Show".
  3. Final Answer:

    Derived Show -> Option A
  4. Quick Check:

    Virtual override calls derived method = "Derived Show" [OK]
Quick Trick: Virtual methods call derived override even via base reference [OK]
Common Mistakes:
MISTAKES
  • Expecting base method output when using base type variable
  • Confusing virtual and non-virtual methods
  • Thinking code causes compilation error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes