Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Inheritance
What will be the output of the following code?
class A {
    public virtual void Show() { Console.WriteLine("A Show"); }
}
class B : A {
    public override void Show() {
        base.Show();
        Console.WriteLine("B Show");
    }
}
class Program {
    static void Main() {
        B obj = new B();
        obj.Show();
    }
}
AA Show\nB Show
BB Show\nA Show
CA Show
DB Show
Step-by-Step Solution
Solution:
  1. Step 1: Understand method calls in derived class

    Class B overrides Show and calls base.Show() which calls A's Show method printing "A Show".
  2. Step 2: Trace output order

    After calling base.Show(), it prints "B Show". So output is "A Show" then "B Show" on separate lines.
  3. Final Answer:

    A Show\nB Show -> Option A
  4. Quick Check:

    base calls parent method first = A Show then B Show [OK]
Quick Trick: base.Method() runs parent code before derived code [OK]
Common Mistakes:
MISTAKES
  • Assuming derived method runs before base
  • Ignoring base call output
  • Confusing override with new keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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