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

Interface declaration syntax 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
🎖️
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'.

Practice

(1/5)
1. What is the main purpose of an interface in C#?
easy
A. To define a contract with method and property signatures only
B. To implement all method bodies for a class
C. To store data like variables and constants
D. To create an instance of a class directly

Solution

  1. Step 1: Understand what an interface is

    An interface only declares method and property signatures without implementations.
  2. Step 2: Compare with other options

    Interfaces do not implement methods or store data; they define a contract for classes.
  3. Final Answer:

    To define a contract with method and property signatures only -> Option A
  4. Quick Check:

    Interface purpose = contract definition [OK]
Hint: Interfaces declare methods, they don't implement them [OK]
Common Mistakes:
  • Thinking interfaces contain method bodies
  • Confusing interfaces with classes
  • Believing interfaces store data
2. Which of the following is the correct syntax to declare an interface named IMyInterface in C#?
easy
A. interface IMyInterface { void MyMethod(); }
B. class IMyInterface { void MyMethod(); }
C. interface IMyInterface() { void MyMethod(); }
D. interface IMyInterface[] { void MyMethod(); }

Solution

  1. Step 1: Check the keyword and name format

    Interfaces use the keyword interface followed by the name without parentheses or brackets.
  2. Step 2: Validate method declaration inside interface

    Methods inside interfaces have only signatures ending with semicolons, no bodies.
  3. Final Answer:

    interface IMyInterface { void MyMethod(); } -> Option A
  4. Quick Check:

    Correct interface syntax = interface IMyInterface { void MyMethod(); } [OK]
Hint: Use 'interface Name { }' without parentheses or brackets [OK]
Common Mistakes:
  • Using class keyword instead of interface
  • Adding parentheses after interface name
  • Using brackets [] after interface name
3. What will be the output of the following code?
interface IExample { void Show(); }
class Demo : IExample {
  public void Show() { Console.WriteLine("Hello Interface"); }
}
class Program {
  static void Main() {
    IExample obj = new Demo();
    obj.Show();
  }
}
medium
A. Compilation error: Show method missing
B. Hello Interface
C. Runtime error: Cannot create interface instance
D. No output

Solution

  1. Step 1: Understand interface implementation

    The class Demo implements IExample and provides the Show method.
  2. Step 2: Analyze the Main method

    An object of Demo is created and assigned to an IExample reference, then Show() is called, printing the message.
  3. Final Answer:

    Hello Interface -> Option B
  4. Quick Check:

    Interface method call prints message [OK]
Hint: Interface methods must be implemented to avoid errors [OK]
Common Mistakes:
  • Assuming interfaces can be instantiated directly
  • Forgetting to implement interface methods
  • Expecting no output without method body
4. Identify the error in the following interface declaration:
interface ITest {
  void Run() {}
}
medium
A. Missing semicolon after method declaration
B. Method name must be lowercase
C. Interface name must start with lowercase 'i'
D. Interfaces cannot have method bodies

Solution

  1. Step 1: Check method declaration in interface

    Interfaces only declare method signatures without bodies (no curly braces).
  2. Step 2: Validate other syntax rules

    Method names can be any case; interface names usually start with uppercase 'I'. Semicolon is required after signature.
  3. Final Answer:

    Interfaces cannot have method bodies -> Option D
  4. Quick Check:

    Interface methods = signatures only [OK]
Hint: No method bodies allowed inside interfaces [OK]
Common Mistakes:
  • Adding method bodies inside interfaces
  • Confusing naming conventions with syntax errors
  • Omitting semicolon after method signature
5. You want to declare an interface IVehicle with two methods: Start() and Stop(). Which of the following is the correct way to declare it and implement it in a class Car?
hard
A. interface IVehicle { void Start(); void Stop(); } class Car { public void Start() { } public void Stop() { } }
B. interface IVehicle { void Start() {} void Stop() {} } class Car : IVehicle { }
C. interface IVehicle { void Start(); void Stop(); } class Car : IVehicle { public void Start() { Console.WriteLine("Car started"); } public void Stop() { Console.WriteLine("Car stopped"); } }
D. interface IVehicle { void Start(); void Stop(); } class Car : IVehicle { void Start() { } void Stop() { } }

Solution

  1. Step 1: Declare interface with method signatures only

    IVehicle must declare Start() and Stop() without bodies.
  2. Step 2: Implement interface methods publicly in class

    Car must implement both methods with public access and provide method bodies.
  3. Step 3: Check other options for errors

    interface IVehicle { void Start() {} void Stop() {} } class Car : IVehicle { } has method bodies in interface (invalid). interface IVehicle { void Start(); void Stop(); } class Car { public void Start() { } public void Stop() { } } does not implement interface. interface IVehicle { void Start(); void Stop(); } class Car : IVehicle { void Start() { } void Stop() { } } implements methods but lacks public modifier, causing error.
  4. Final Answer:

    Correct interface and class implementation with public methods -> Option C
  5. Quick Check:

    Interface methods declared; class implements publicly [OK]
Hint: Interface methods need public implementation in classes [OK]
Common Mistakes:
  • Adding method bodies inside interface
  • Not implementing interface in class
  • Omitting public modifier in class methods