Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

What does it mean when a C# class implements multiple interfaces?

easy
A. The class inherits code from multiple classes.
B. The class agrees to provide code for all methods defined in those interfaces.
C. The class can only use one interface at a time.
D. The class automatically gets all properties from the interfaces without coding.

Solution

  1. Step 1: Understand interface implementation

    Interfaces define method signatures but no code. A class implementing them must provide the code.
  2. Step 2: Multiple interfaces require all methods

    When a class implements several interfaces, it must write code for every method in all interfaces.
  3. Final Answer:

    The class agrees to provide code for all methods defined in those interfaces. -> Option B
  4. Quick Check:

    Multiple interface implementation = implement all methods [OK]
Hint: Interfaces are contracts; class must fulfill all method contracts [OK]
Common Mistakes:
  • Thinking interfaces provide code to inherit
  • Believing class can skip some interface methods
  • Confusing interfaces with classes
2.

Which of the following is the correct syntax to declare a class Car implementing interfaces IMovable and IEngine?

?
easy
A. public class Car : IMovable, IEngine { }
B. public class Car implements IMovable, IEngine { }
C. public class Car inherits IMovable, IEngine { }
D. public class Car : IMovable & IEngine { }

Solution

  1. Step 1: Recall C# interface syntax

    In C#, a class uses a colon ':' followed by interface names separated by commas.
  2. Step 2: Check each option

    public class Car : IMovable, IEngine { } uses ':' and commas correctly. Options B and C use wrong keywords. public class Car : IMovable & IEngine { } uses '&' which is invalid.
  3. Final Answer:

    public class Car : IMovable, IEngine { } -> Option A
  4. Quick Check:

    Interfaces listed after ':' separated by commas [OK]
Hint: Use ':' and commas to list interfaces after class name [OK]
Common Mistakes:
  • Using 'implements' keyword (Java style)
  • Using '&' instead of commas
  • Using 'inherits' keyword incorrectly
3.

What will be the output of the following C# code?

interface IA { void Show(); }
interface IB { void Show(); }
class Demo : IA, IB {
    public void Show() { Console.WriteLine("Hello"); }
}
class Program {
    static void Main() {
        Demo d = new Demo();
        d.Show();
    }
}
medium
A. Hello
B. Compilation error due to ambiguous Show method
C. Runtime error
D. No output

Solution

  1. Step 1: Understand method implementation for multiple interfaces

    Both interfaces have Show method. The class Demo implements one Show method that satisfies both.
  2. Step 2: Check program output

    Main creates Demo and calls Show, which prints "Hello".
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    Single method implements both interfaces' Show [OK]
Hint: One method can implement same method from multiple interfaces [OK]
Common Mistakes:
  • Expecting compile error for same method name
  • Thinking separate methods needed for each interface
  • Confusing interface method calls
4.

Identify the error in this code snippet:

interface IA { void Run(); }
interface IB { void Jump(); }
class Player : IA, IB {
    public void Run() { Console.WriteLine("Running"); }
}
medium
A. Run method should be private.
B. Class Player cannot implement two interfaces.
C. Interfaces cannot have methods.
D. Class Player must implement Jump method from IB interface.

Solution

  1. Step 1: Check interface methods

    IA requires Run(), IB requires Jump().
  2. Step 2: Verify class implementation

    Player implements Run() but misses Jump(), so it is incomplete.
  3. Final Answer:

    Class Player must implement Jump method from IB interface. -> Option D
  4. Quick Check:

    All interface methods must be implemented [OK]
Hint: Implement all interface methods to avoid errors [OK]
Common Mistakes:
  • Forgetting to implement all interface methods
  • Thinking interfaces can't have methods
  • Assuming methods can be private
5.

Given these interfaces and class:

interface IAlpha { void Action(); }
interface IBeta { void Action(); }
class Combined : IAlpha, IBeta {
    void IAlpha.Action() { Console.WriteLine("Alpha Action"); }
    void IBeta.Action() { Console.WriteLine("Beta Action"); }
}
class Program {
    static void Main() {
        Combined c = new Combined();
        // Which calls are valid?
    }
}

Which of the following calls will compile and print output?

hard
A. ((IBeta)c).Action(); // prints 'Beta Action'
B. c.Action(); // prints 'Alpha Action'
C. ((IAlpha)c).Action(); // prints 'Alpha Action'
D. c.Action(); // prints 'Beta Action'

Solution

  1. Step 1: Understand explicit interface implementation

    Combined class implements IAlpha.Action and IBeta.Action explicitly, so these methods are not accessible via class instance directly.
  2. Step 2: Check method calls

    Only calls through interface references like (IAlpha)c or (IBeta)c are valid. c.Action() is invalid and causes compile error.
  3. Final Answer:

    ((IAlpha)c).Action(); // prints 'Alpha Action' -> Option C
  4. Quick Check:

    Explicit interface methods need interface cast to call [OK]
Hint: Explicit interface methods require casting to interface type [OK]
Common Mistakes:
  • Calling explicit interface methods directly on class instance
  • Confusing explicit and implicit implementation
  • Assuming c.Action() works without cast