Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Interfaces
What will be the output of this C# code?
interface IPrinter { void Print(); }
class ConsolePrinter : IPrinter { public void Print() { Console.WriteLine("Console"); } }
class FilePrinter : IPrinter { public void Print() { Console.WriteLine("File"); } }
class Program { static void Main() { IPrinter printer = new FilePrinter(); printer.Print(); } }
AConsole
BNo output
CCompilation error
DFile
Step-by-Step Solution
Solution:
  1. Step 1: Identify which class instance is created

    The variable 'printer' is assigned a new FilePrinter object.
  2. Step 2: Understand method call behavior

    Calling printer.Print() executes FilePrinter's Print method, printing "File".
  3. Final Answer:

    File -> Option D
  4. Quick Check:

    Interface method calls actual object's method [OK]
Quick Trick: Interface calls method of assigned object type [OK]
Common Mistakes:
MISTAKES
  • Confusing which class instance is created
  • Expecting compilation error
  • Thinking no output is printed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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