Bird
Raised Fist0

Given this code, what will be the output?

hard🚀 Application Q9 of Q15
C Sharp (C#) - Interfaces
Given this code, what will be the output? interface IA { void Show() => Console.WriteLine("IA"); } interface IB : IA { void Show() => Console.WriteLine("IB"); } class C : IB { } class Program { static void Main() { IA a = new C(); IB b = new C(); a.Show(); b.Show(); } }
AIB IB
BIA IA
CIA IB
DCompile error
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface inheritance and default methods

    IB inherits IA and overrides Show() with its own default method.
  2. Step 2: Analyze calls via different interface references

    Calling Show() via IA reference calls IA's default method; via IB reference calls IB's default method.
  3. Final Answer:

    IA IB -> Option C
  4. Quick Check:

    Interface reference calls respective default method = B [OK]
Quick Trick: Interface reference type decides default method called [OK]
Common Mistakes:
MISTAKES
  • Assuming class overrides default methods automatically
  • Expecting same output for both calls
  • Thinking compile error occurs due to multiple defaults

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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