Challenge - 5 Problems
Multiple Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of multiple interface implementation with explicit methods
What is the output of this C# program that implements two interfaces with explicit method implementations?
C Sharp (C#)
using System; interface IA { void Show(); } interface IB { void Show(); } class Demo : IA, IB { void IA.Show() { Console.WriteLine("IA Show"); } void IB.Show() { Console.WriteLine("IB Show"); } } class Program { static void Main() { Demo d = new Demo(); ((IA)d).Show(); ((IB)d).Show(); } }
Attempts:
2 left
💡 Hint
Explicit interface implementations require casting to the interface to call the method.
✗ Incorrect
The class Demo implements IA.Show and IB.Show explicitly. To call these methods, you must cast the object to the interface type. The calls ((IA)d).Show() and ((IB)d).Show() invoke the respective interface implementations, printing 'IA Show' and 'IB Show'.
❓ Predict Output
intermediate2:00remaining
Output when calling method without explicit interface cast
Given this code, what will be the output when calling Show() directly on the object?
C Sharp (C#)
using System; interface IA { void Show(); } interface IB { void Show(); } class Demo : IA, IB { public void Show() { Console.WriteLine("Demo Show"); } void IA.Show() { Console.WriteLine("IA Show"); } void IB.Show() { Console.WriteLine("IB Show"); } } class Program { static void Main() { Demo d = new Demo(); d.Show(); } }
Attempts:
2 left
💡 Hint
Public method Show() in the class is called directly without casting.
✗ Incorrect
The class Demo has a public Show() method. This method is called directly on the object d, so it prints 'Demo Show'. The explicit interface implementations are not called unless the object is cast to the interface.
🔧 Debug
advanced2:00remaining
Identify the error in multiple interface implementation
What error will this code produce when compiled?
C Sharp (C#)
interface IA { void Display(); } interface IB { void Display(); } class Test : IA, IB { public void Display() { } public void Display(int x) { } }
Attempts:
2 left
💡 Hint
Check if all interface members are implemented explicitly or implicitly.
✗ Incorrect
The class Test has a public Display() method that matches IA.Display() and IB.Display(). Since both interfaces have the same method signature, the single public method satisfies both. The overload Display(int x) does not affect interface implementation. Therefore, the code compiles successfully.
📝 Syntax
advanced2:00remaining
Which option correctly implements two interfaces with same method name?
Select the option that correctly implements interfaces IA and IB, both having void Run(), in one class without ambiguity.
Attempts:
2 left
💡 Hint
Explicit interface implementation avoids ambiguity when methods have same signature.
✗ Incorrect
Option A uses explicit interface implementation for both IA.Run() and IB.Run(), so the class implements both interfaces without ambiguity. Option A compiles but the single Run() method implements both interfaces implicitly. Option A is invalid because IB.Run() is explicit but IA.Run() is implicit, causing ambiguity. Option A is invalid because void Run() without access modifier is private and does not implement interfaces.
🚀 Application
expert3:00remaining
Determine the output of interface method calls with inheritance and multiple interfaces
Consider these interfaces and classes. What is the output when running the Main method?
C Sharp (C#)
using System; interface IA { void Action(); } interface IB : IA { void Action(); } class Base : IA { public virtual void Action() { Console.WriteLine("Base Action"); } } class Derived : Base, IB { public override void Action() { Console.WriteLine("Derived Action"); } } class Program { static void Main() { Derived d = new Derived(); IA ia = d; IB ib = d; Base b = d; d.Action(); ia.Action(); ib.Action(); b.Action(); } }
Attempts:
2 left
💡 Hint
Virtual and override methods are called based on the actual object type, not the reference type.
✗ Incorrect
The Derived class overrides the virtual method Action from Base. All references (IA, IB, Base, Derived) point to the same Derived object. Calling Action() on any of these calls the overridden Derived.Action(), printing 'Derived Action' four times.