Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Interfaces
What will be the output of the following code?
interface IExample { void Show(); }
class Demo : IExample {
  public void Show() { Console.WriteLine("Hello Interface"); }
}
class Program {
  static void Main() {
    IExample obj = new Demo();
    obj.Show();
  }
}
ACompilation error: Show method missing
BHello Interface
CRuntime error: Cannot create interface instance
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface implementation

    The class Demo implements IExample and provides the Show method.
  2. Step 2: Analyze the Main method

    An object of Demo is created and assigned to an IExample reference, then Show() is called, printing the message.
  3. Final Answer:

    Hello Interface -> Option B
  4. Quick Check:

    Interface method call prints message [OK]
Quick Trick: Interface methods must be implemented to avoid errors [OK]
Common Mistakes:
MISTAKES
  • Assuming interfaces can be instantiated directly
  • Forgetting to implement interface methods
  • Expecting no output without method body

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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