Bird
0
0

Given the classes below, what will be the output?

hard🚀 Application Q15 of 15
C Sharp (C#) - Inheritance
Given the classes below, what will be the output?
class A {
    public virtual string GetName() => "A";
}
class B : A {
    public override string GetName() => "B";
}
class C : B {
    public override string GetName() => base.GetName() + "C";
}

var obj = new C();
Console.WriteLine(obj.GetName());
AAC
BCompilation error
CBC
DC
Step-by-Step Solution
Solution:
  1. Step 1: Trace method calls through inheritance

    Class C overrides GetName() and calls base.GetName(), which refers to B's override returning "B".
  2. Step 2: Combine returned strings

    C appends "C" to the result from B, so the final string is "B" + "C" = "BC".
  3. Final Answer:

    BC -> Option C
  4. Quick Check:

    base.GetName() calls immediate parent method [OK]
Quick Trick: base calls immediate parent method, not grandparent [OK]
Common Mistakes:
MISTAKES
  • Assuming base calls grandparent method
  • Ignoring string concatenation
  • Expecting only "C" output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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