Bird
0
0

Given these classes:

hard🚀 Application Q9 of 15
C Sharp (C#) - Inheritance
Given these classes:
class Animal {
    public virtual string Speak() => "Animal sound";
}
class Dog : Animal {
    public override string Speak() => base.Speak() + " and Bark";
}
class Puppy : Dog {
    public override string Speak() => base.Speak() + " and Whine";
}

What is the output of new Puppy().Speak()?
A"Animal sound and Bark and Whine"
B"Animal sound and Whine"
C"Animal sound and Bark"
D"Whine and Bark and Animal sound"
Step-by-Step Solution
Solution:
  1. Step 1: Trace Speak() calls through inheritance

    Puppy.Speak() calls base.Speak() which is Dog.Speak(), which calls base.Speak() (Animal.Speak()).
  2. Step 2: Combine returned strings

    Animal.Speak() returns "Animal sound"; Dog adds " and Bark"; Puppy adds " and Whine".
  3. Final Answer:

    "Animal sound and Bark and Whine" -> Option A
  4. Quick Check:

    base chains calls up inheritance [OK]
Quick Trick: base calls chain up inheritance returning combined results [OK]
Common Mistakes:
MISTAKES
  • Ignoring base call chaining
  • Assuming only Puppy or Dog Speak runs
  • Mixing order of concatenation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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