0
0
C Sharp (C#)programming~20 mins

Multiple interface implementation in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
    }
}
AIA Show\nIB Show
BCompilation error due to ambiguous Show method
CShow\nShow
DIB Show\nIA Show
Attempts:
2 left
💡 Hint
Explicit interface implementations require casting to the interface to call the method.
Predict Output
intermediate
2: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();
    }
}
ACompilation error due to ambiguous Show method
BIB Show
CDemo Show
DIA Show
Attempts:
2 left
💡 Hint
Public method Show() in the class is called directly without casting.
🔧 Debug
advanced
2: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) { }
}
ACompilation error: 'Test' does not implement interface member 'IB.Display()'
BCompilation error: Method 'Display(int)' hides inherited member
CRuntime error: Ambiguous method call
DNo compilation error, code compiles successfully
Attempts:
2 left
💡 Hint
Check if all interface members are implemented explicitly or implicitly.
📝 Syntax
advanced
2: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.
Aclass C : IA, IB { void IA.Run() { } void IB.Run() { } }
Bclass C : IA, IB { public void Run() { } }
Cclass C : IA, IB { public void Run() { } void IB.Run() { } }
Dclass C : IA, IB { void Run() { } }
Attempts:
2 left
💡 Hint
Explicit interface implementation avoids ambiguity when methods have same signature.
🚀 Application
expert
3: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();
    }
}
ABase Action\nBase Action\nBase Action\nBase Action
BDerived Action\nDerived Action\nDerived Action\nDerived Action
CDerived Action\nBase Action\nDerived Action\nBase Action
DCompilation error due to interface inheritance conflict
Attempts:
2 left
💡 Hint
Virtual and override methods are called based on the actual object type, not the reference type.