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

Interface declaration syntax 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# code when calling obj.ShowMessage()?
C Sharp (C#)
interface IMessage
{
    void ShowMessage();
}

class Greeting : IMessage
{
    public void ShowMessage()
    {
        System.Console.WriteLine("Hello from interface implementation!");
    }
}

class Program
{
    static void Main()
    {
        IMessage obj = new Greeting();
        obj.ShowMessage();
    }
}
ACompilation error: interface method cannot be called
BHello from interface implementation!
CRuntime error: method not implemented
DNo output
Attempts:
2 left
💡 Hint
Remember that classes implementing interfaces must provide method bodies.
Predict Output
intermediate
2:00remaining
Interface property declaration and usage
What will be the output of this program?
C Sharp (C#)
interface ICar
{
    string Model { get; set; }
}

class Car : ICar
{
    public string Model { get; set; }

    public Car(string model)
    {
        Model = model;
    }
}

class Program
{
    static void Main()
    {
        ICar myCar = new Car("Tesla Model S");
        System.Console.WriteLine(myCar.Model);
    }
}
ATesla Model S
BCompilation error: property not implemented
CRuntime error: property access failed
Dnull
Attempts:
2 left
💡 Hint
Check how the property Model is implemented in the class.
Predict Output
advanced
2:00remaining
Explicit interface implementation output
What is the output of this code?
C Sharp (C#)
interface IAlpha
{
    void Display();
}

interface IBeta
{
    void Display();
}

class MyClass : IAlpha, IBeta
{
    void IAlpha.Display()
    {
        System.Console.WriteLine("Alpha Display");
    }

    void IBeta.Display()
    {
        System.Console.WriteLine("Beta Display");
    }
}

class Program
{
    static void Main()
    {
        MyClass obj = new MyClass();
        ((IAlpha)obj).Display();
        ((IBeta)obj).Display();
    }
}
A
Alpha Display
Beta Display
B
Beta Display
Alpha Display
CCompilation error: ambiguous method call
DRuntime error: method not found
Attempts:
2 left
💡 Hint
Explicit interface implementations require casting to call.
Predict Output
advanced
2:00remaining
Interface inheritance and method call output
What will this program print?
C Sharp (C#)
interface IBase
{
    void Show();
}

interface IDerived : IBase
{
    void Show();
}

class Implementation : IDerived
{
    public void Show()
    {
        System.Console.WriteLine("Show method in Implementation");
    }
}

class Program
{
    static void Main()
    {
        IBase obj = new Implementation();
        obj.Show();
    }
}
ANo output
BCompilation error: method Show must be implemented twice
CRuntime error: ambiguous method call
DShow method in Implementation
Attempts:
2 left
💡 Hint
Interface inheritance means IDerived includes IBase's members.
🧠 Conceptual
expert
2:00remaining
Interface declaration syntax error identification
Which option contains a syntax error in the interface declaration?
Ainterface IExample { void DoWork(); }
Binterface IExample { int Calculate(int x); }
Cinterface IExample { public void DoWork(); }
Dinterface IExample { string Name { get; set; } }
Attempts:
2 left
💡 Hint
Interface members cannot have access modifiers like 'public'.