Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Inheritance
What will be the output of this C# code?
class Animal { public void Speak() { Console.WriteLine("Animal speaks"); } }
class Dog : Animal { public void Bark() { Console.WriteLine("Dog barks"); } }
var d = new Dog();
d.Speak();
ADog barks
BNo output
CCompile error
DAnimal speaks
Step-by-Step Solution
Solution:
  1. Step 1: Understand inheritance and method calls

    Dog inherits Animal, so Dog objects can call Animal's methods like Speak().
  2. Step 2: Analyze the code output

    Calling d.Speak() runs Animal's Speak method, printing "Animal speaks".
  3. Final Answer:

    Animal speaks -> Option D
  4. Quick Check:

    Inherited method runs = "Animal speaks" [OK]
Quick Trick: Inherited methods can be called on child objects [OK]
Common Mistakes:
MISTAKES
  • Thinking Bark() runs instead of Speak()
  • Expecting compile error due to inheritance
  • Assuming no output without calling Bark()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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