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(); } }
ASpeak
BRuntime error
CCompilation error
DWoof
Step-by-Step Solution
Solution:
  1. Step 1: Trace interface implementation and execution

    Dog implements IAnimal with Speak() printing "Woof". In Main(), IAnimal a = new Dog(); calls a.Speak();, invoking Dog's method and outputting "Woof".
  2. Final Answer:

    Woof -> Option D
  3. Quick Check:

    Interface method call runs implemented method = Woof [OK]
Quick Trick: Interface calls invoke implemented methods at runtime [OK]
Common Mistakes:
MISTAKES
  • Expecting interface name printed
  • Thinking code won't compile
  • Expecting runtime errors without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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