Bird
Raised Fist0

What will be the output of the following code?

medium📝 Predict Output Q13 of Q15
C Sharp (C#) - Inheritance
What will be the output of the following code?
class Base {
    public virtual void Show() { Console.WriteLine("Base"); }
}
class Derived : Base {
    public sealed override void Show() { Console.WriteLine("Derived"); }
}
class MoreDerived : Derived {
    public override void Show() { Console.WriteLine("MoreDerived"); }
}

var obj = new MoreDerived();
obj.Show();
ABase
BDerived
CMoreDerived
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand sealed override effect

    The method Show in Derived is sealed, so it cannot be overridden in MoreDerived.
  2. Step 2: Check the code for override in MoreDerived

    MoreDerived tries to override Show, which causes a compilation error.
  3. Final Answer:

    Compilation error -> Option D
  4. Quick Check:

    Sealed method cannot be overridden [OK]
Quick Trick: Sealed override blocks further overrides causing errors [OK]
Common Mistakes:
MISTAKES
  • Assuming MoreDerived.Show runs
  • Ignoring sealed keyword effect
  • Thinking output is Derived

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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