Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q4 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 { } class Program { static void Main() { Dog d = new Dog(); d.Speak(); } }
AAnimal speaks
BDog speaks
CCompilation error
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand inheritance and method call

    Dog inherits from Animal but does not override Speak(), so Dog uses Animal's Speak() method.
  2. Step 2: Predict output of d.Speak()

    Calling d.Speak() prints "Animal speaks" because Dog inherits that method.
  3. Final Answer:

    Animal speaks -> Option A
  4. Quick Check:

    Inherited method output = "Animal speaks" [OK]
Quick Trick: Derived class uses base class method if not overridden [OK]
Common Mistakes:
MISTAKES
  • Expecting Dog to print "Dog speaks" without override
  • Thinking code causes compilation error
  • Assuming no output without method override

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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