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

Implementing interfaces in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
    }
}
AHello from Person
BCompile-time error: Cannot convert Person to IGreet
CRuntime error: NullReferenceException
DNo output
Attempts:
2 left
💡 Hint
Look at how the interface method is implemented and called.
Predict Output
intermediate
2: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();
    }
}
A
Explicit Show
Explicit Show
B
Explicit Show
Public Show
C
Public Show
Public Show
D
Public Show
Explicit Show
Attempts:
2 left
💡 Hint
Explicit interface implementation hides the method from the class interface.
🔧 Debug
advanced
2: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;
    }
}
ANo error, code compiles fine
BInterface cannot have methods with parameters
CMethod signature does not match interface method
DClass must be abstract to implement interface
Attempts:
2 left
💡 Hint
Check parameter names and types in interface and class method.
Predict Output
advanced
2: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();
    }
}
A
First
First
B
Second
First
C
First
Second
D
Second
Second
Attempts:
2 left
💡 Hint
Explicit interface implementations are called via interface references.
🧠 Conceptual
expert
2:00remaining
Interface implementation and method accessibility
Which statement about explicit interface implementation in C# is TRUE?
AExplicit interface methods are accessible like any public method of the class.
BExplicitly implemented interface methods can only be called through an interface reference.
CExplicit interface implementation allows method overloading with the same signature.
DExplicit interface methods must be declared public in the class.
Attempts:
2 left
💡 Hint
Think about how explicit interface methods are accessed.