Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Interfaces
What will be the output of this code?
interface IAnimal { void Speak(); }
class Dog : IAnimal { public void Speak() { Console.WriteLine("Woof"); } }
class Program { static void Main() { IAnimal a = new Dog(); a.Speak(); } }
AWoof
BNo output
CCompile error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface and class implementation

    Dog implements IAnimal and provides Speak method printing "Woof".
  2. Step 2: Analyze Main method execution

    Variable 'a' of type IAnimal references Dog instance; calling Speak prints "Woof".
  3. Final Answer:

    Woof -> Option A
  4. Quick Check:

    Interface method call outputs implemented method [OK]
Quick Trick: Interface reference calls implemented method at runtime [OK]
Common Mistakes:
MISTAKES
  • Expecting compile error due to interface variable
  • Thinking no output because interface has no body
  • Confusing runtime error with missing implementation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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