Bird
Raised Fist0

What is the output of this code? interface ITest { void Print() => Console.WriteLine("Interface"); } class Test : ITest { } class Program { static void Main() { ITest t = new Test(); t.Print(); } }

medium📝 Predict Output Q4 of Q15
C Sharp (C#) - Interfaces
What is the output of this code? interface ITest { void Print() => Console.WriteLine("Interface"); } class Test : ITest { } class Program { static void Main() { ITest t = new Test(); t.Print(); } }
AInterface
BTest
CCompile error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze interface and class implementation

    Interface ITest has a default method Print() that writes "Interface". Class Test implements ITest but does not override Print().
  2. Step 2: Determine method call behavior

    Calling Print() on ITest reference calls the default interface method, printing "Interface".
  3. Final Answer:

    Interface -> Option A
  4. Quick Check:

    Default method call output = B [OK]
Quick Trick: Default method runs if class does not override [OK]
Common Mistakes:
MISTAKES
  • Expecting class method output without override
  • Thinking compile error occurs without override
  • Confusing interface and class outputs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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