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 pet = new Dog();
    pet.Speak();
  }
}
AIAnimal
BWoof
CDog
DCompile error
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface implementation

    The class Dog implements IAnimal and provides Speak method that prints "Woof".
  2. Step 2: Trace the program execution

    Main creates an IAnimal reference to a Dog object and calls Speak, which runs Dog's Speak method printing "Woof".
  3. Final Answer:

    Woof -> Option B
  4. Quick Check:

    Interface call runs implemented method [OK]
Quick Trick: Interface variable calls class method implementation [OK]
Common Mistakes:
MISTAKES
  • Expecting interface name output
  • Thinking interface cannot be instantiated
  • Confusing class and interface names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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