Bird
Raised Fist0

What will this code print?

medium📝 Predict Output Q5 of Q15
C Sharp (C#) - Interfaces
What will this code print? interface ICalc { int Add(int a, int b) => a + b; } class Calc : ICalc { public int Add(int a, int b) => a * b; } class Program { static void Main() { ICalc calc = new Calc(); Console.WriteLine(calc.Add(2, 3)); } }
A5
B6
CCompile error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand method implementations

    Interface ICalc has default Add method returning sum. Class Calc overrides Add to return product.
  2. Step 2: Identify method called at runtime

    Calc's Add method overrides default, so calling Add(2,3) returns 2*3 = 6.
  3. Final Answer:

    6 -> Option B
  4. Quick Check:

    Override method output = D [OK]
Quick Trick: Class override replaces default interface method [OK]
Common Mistakes:
MISTAKES
  • Assuming default method runs despite override
  • Confusing sum and product results
  • Expecting compile or runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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