Bird
0
0

What will be printed when the following code runs?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Interfaces

What will be printed when the following code runs?

interface IDevice { void TurnOn(); }
class Fan : IDevice {
  public void TurnOn() { Console.WriteLine("Fan is on"); }
}
class Program {
  static void Main() {
    IDevice device = new Fan();
    device.TurnOn();
  }
}
AFan is on
BIDevice TurnOn
CCompilation error
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface implementation

    Fan implements IDevice and provides TurnOn method.
  2. Step 2: Analyze method call

    Calling TurnOn on IDevice reference calls Fan's implementation.
  3. Final Answer:

    Fan is on -> Option A
  4. Quick Check:

    Interface reference calls implemented method. [OK]
Quick Trick: Interface calls implemented method at runtime [OK]
Common Mistakes:
MISTAKES
  • Expecting interface name to print
  • Assuming compilation error due to interface
  • Thinking no output occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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