Challenge - 5 Problems
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Remember that classes implementing interfaces must provide method bodies.
✗ Incorrect
The class Greeting implements the interface IMessage and provides the ShowMessage method. Calling ShowMessage on the interface reference calls the implemented method, printing the message.
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Check how the property Model is implemented in the class.
✗ Incorrect
The class Car implements the interface property Model with get and set accessors. The constructor sets the Model to "Tesla Model S". Printing myCar.Model outputs the string.
❓ Predict Output
advanced2: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(); } }
Attempts:
2 left
💡 Hint
Explicit interface implementations require casting to call.
✗ Incorrect
The class MyClass explicitly implements Display for both interfaces. Calling Display via IAlpha reference prints "Alpha Display" and via IBeta reference prints "Beta Display".
❓ Predict Output
advanced2: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(); } }
Attempts:
2 left
💡 Hint
Interface inheritance means IDerived includes IBase's members.
✗ Incorrect
IDerived inherits from IBase, so Show is declared once. Implementation provides one Show method that satisfies both interfaces. Calling Show prints the message.
🧠 Conceptual
expert2:00remaining
Interface declaration syntax error identification
Which option contains a syntax error in the interface declaration?
Attempts:
2 left
💡 Hint
Interface members cannot have access modifiers like 'public'.
✗ Incorrect
In C#, interface members are implicitly public and cannot include access modifiers. Option C incorrectly uses 'public' inside the interface.