Challenge - 5 Problems
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of interface method call
What is the output of this C# program when calling the interface method?
C Sharp (C#)
using System; interface IGreet { void SayHello(); } class Person : IGreet { public void SayHello() { Console.WriteLine("Hello from Person"); } } class Program { static void Main() { IGreet greeter = new Person(); greeter.SayHello(); } }
Attempts:
2 left
💡 Hint
Look at how the interface method is implemented and called.
✗ Incorrect
The class Person implements IGreet and its method SayHello prints "Hello from Person". The interface reference calls this method, so the output is that string.
❓ Predict Output
intermediate2:00remaining
Output when interface method is explicitly implemented
What will be the output of this program?
C Sharp (C#)
using System; interface IDisplay { void Show(); } class Demo : IDisplay { void IDisplay.Show() { Console.WriteLine("Explicit Show"); } public void Show() { Console.WriteLine("Public Show"); } } class Program { static void Main() { Demo d = new Demo(); d.Show(); ((IDisplay)d).Show(); } }
Attempts:
2 left
💡 Hint
Explicit interface implementation hides the method from the class interface.
✗ Incorrect
The call d.Show() calls the public Show method. The cast to IDisplay calls the explicit interface implementation, printing "Explicit Show".
🔧 Debug
advanced2:00remaining
Identify the error in interface implementation
Why does this code fail to compile?
C Sharp (C#)
interface ICalc { int Add(int a, int b); } class Calculator : ICalc { public int Add(int x, int y) { return x + y; } }
Attempts:
2 left
💡 Hint
Check parameter names and types in interface and class method.
✗ Incorrect
The interface method Add expects parameters named 'a' and 'b', but the class uses 'x' and 'y'. In C#, parameter names do not affect method signature, so this is not an error. The code actually compiles fine. So the correct answer is D.
❓ Predict Output
advanced2:00remaining
Output of multiple interface implementations
What is the output of this program?
C Sharp (C#)
using System; interface IFirst { void Print(); } interface ISecond { void Print(); } class Multi : IFirst, ISecond { void IFirst.Print() { Console.WriteLine("First"); } void ISecond.Print() { Console.WriteLine("Second"); } } class Program { static void Main() { Multi m = new Multi(); ((IFirst)m).Print(); ((ISecond)m).Print(); } }
Attempts:
2 left
💡 Hint
Explicit interface implementations are called via interface references.
✗ Incorrect
The calls cast the object to IFirst and ISecond, calling their respective explicit implementations printing "First" and "Second".
🧠 Conceptual
expert2:00remaining
Interface implementation and method accessibility
Which statement about explicit interface implementation in C# is TRUE?
Attempts:
2 left
💡 Hint
Think about how explicit interface methods are accessed.
✗ Incorrect
Explicit interface methods are hidden from the class interface and can only be accessed via an interface reference.