Bird
0
0

Consider the following code snippet:

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Interfaces
Consider the following code snippet:
interface IAnimal { void Speak(); }
abstract class Mammal { public void Breathe() { Console.WriteLine("Breathing"); } public abstract void Speak(); }
class Dog : Mammal, IAnimal { public override void Speak() { Console.WriteLine("Woof"); } }

var dog = new Dog();
dog.Breathe();
dog.Speak();

What will be the output when this code runs?
ABreathing Woof
BWoof Breathing
CBreathing
DCompilation error due to multiple inheritance
Step-by-Step Solution
Solution:
  1. Step 1: Understand class and interface usage

    The class Dog inherits from abstract class Mammal and implements interface IAnimal. It overrides Speak() and inherits Breathe().
  2. Step 2: Trace method calls

    Calling dog.Breathe() prints "Breathing". Calling dog.Speak() prints "Woof" as overridden in Dog.
  3. Final Answer:

    Breathing Woof -> Option A
  4. Quick Check:

    Abstract class method + override = correct output [OK]
Quick Trick: Abstract class methods run normally; override abstract methods [OK]
Common Mistakes:
MISTAKES
  • Thinking multiple inheritance causes error in C#
  • Confusing order of output lines
  • Missing override keyword causing compile error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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