Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Inheritance
What will be the output of the following code?
class Parent {
    public virtual void Show() {
        Console.WriteLine("Parent Show");
    }
}
class Child : Parent {
    public override void Show() {
        base.Show();
        Console.WriteLine("Child Show");
    }
}

var obj = new Child();
obj.Show();
AChild Show
BParent Show
CCompilation error
DParent Show\nChild Show
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding and base call

    The Child class overrides Show() and calls base.Show() which runs the Parent version first.
  2. Step 2: Trace the output

    First, "Parent Show" is printed from base.Show(), then "Child Show" is printed from the child method.
  3. Final Answer:

    Parent Show Child Show -> Option D
  4. Quick Check:

    base.Method() runs parent method first [OK]
Quick Trick: base.Method() runs parent method inside override [OK]
Common Mistakes:
MISTAKES
  • Ignoring base.Show() call
  • Expecting only child output
  • Thinking code causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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