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

Interface as contract mental model 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 that uses an interface as a contract?

C Sharp (C#)
interface IAnimal {
    string Speak();
}

class Dog : IAnimal {
    public string Speak() {
        return "Woof!";
    }
}

class Program {
    static void Main() {
        IAnimal pet = new Dog();
        System.Console.WriteLine(pet.Speak());
    }
}
AWoof! Woof!
BWoof!
CCompile-time error: Dog does not implement IAnimal
DRuntime error: Cannot convert Dog to IAnimal
Attempts:
2 left
💡 Hint

Remember, the interface defines a method contract that the class must implement.

🧠 Conceptual
intermediate
1:30remaining
Interface contract enforcement

Which statement best describes the role of an interface in C#?

AAn interface defines a contract that classes must follow by implementing its methods and properties.
BAn interface can hold state and fields to share data between classes.
CAn interface provides default implementations for all its methods.
DAn interface is a class that can be instantiated directly.
Attempts:
2 left
💡 Hint

Think about what an interface requires from classes that use it.

🔧 Debug
advanced
2:30remaining
Why does this interface implementation fail?

Given the code below, why does it fail to compile?

C Sharp (C#)
interface IVehicle {
    void Drive();
}

class Car : IVehicle {
    public void drive() {
        System.Console.WriteLine("Driving");
    }
}

class Program {
    static void Main() {
        IVehicle v = new Car();
        v.Drive();
    }
}
AMethod name in Car is lowercase 'drive' instead of 'Drive', so it does not implement the interface method.
BInterface IVehicle cannot be implemented by a class named Car.
CThe interface method Drive must return a value, but Car's method returns void.
DThe class Car must be declared abstract to implement an interface.
Attempts:
2 left
💡 Hint

Check method names and case sensitivity carefully.

📝 Syntax
advanced
2:00remaining
Correct interface implementation syntax

Which option shows the correct way to implement this interface in C#?

C Sharp (C#)
interface IShape {
    double Area();
}
A
class Circle : IShape {
    public void Area() {
        return 3.14 * 2 * 2;
    }
}
B
class Circle implements IShape {
    public double Area() {
        return 3.14 * 2 * 2;
    }
}
C
class Circle : IShape {
    double Area() {
        return 3.14 * 2 * 2;
    }
}
D
class Circle : IShape {
    public double Area() {
        return 3.14 * 2 * 2;
    }
}
Attempts:
2 left
💡 Hint

Remember the correct keyword for implementing interfaces and method signatures.

🚀 Application
expert
3:00remaining
Interface contract with multiple implementations

Consider this interface and two classes implementing it. What is the output when running the program?

C Sharp (C#)
interface ILogger {
    void Log(string message);
}

class ConsoleLogger : ILogger {
    public void Log(string message) {
        System.Console.WriteLine($"Console: {message}");
    }
}

class FileLogger : ILogger {
    public void Log(string message) {
        System.Console.WriteLine($"File: {message}");
    }
}

class Program {
    static void Main() {
        ILogger logger = new ConsoleLogger();
        logger.Log("Start");
        logger = new FileLogger();
        logger.Log("End");
    }
}
A
Start
End
B
File: Start
Console: End
C
Console: Start
File: End
DCompile-time error: Cannot assign FileLogger to ILogger
Attempts:
2 left
💡 Hint

Think about how the interface variable can hold different implementations.

Practice

(1/5)
1.

What does an interface in C# represent?

easy
A. A contract that defines methods a class must implement
B. A class that contains method implementations
C. A variable type that stores data
D. A method that runs automatically

Solution

  1. Step 1: Understand the role of an interface

    An interface defines a set of method signatures without implementations.
  2. Step 2: Compare with classes

    Classes implement interfaces by providing method bodies, fulfilling the contract.
  3. Final Answer:

    A contract that defines methods a class must implement -> Option A
  4. Quick Check:

    Interface = contract for methods [OK]
Hint: Interfaces define method rules, not code bodies [OK]
Common Mistakes:
  • Thinking interfaces contain method code
  • Confusing interfaces with classes
  • Believing interfaces store data
2.

Which of the following is the correct way to declare an interface in C#?

?
easy
A. interface IAnimal { void Speak(); }
B. class IAnimal { void Speak(); }
C. interface IAnimal() { void Speak(); }
D. interface IAnimal { void Speak() {} }

Solution

  1. Step 1: Check interface declaration syntax

    Interfaces use the keyword 'interface' followed by the name and method signatures without bodies.
  2. Step 2: Identify correct method signature

    Method declarations in interfaces do not have bodies, so no curly braces after method.
  3. Final Answer:

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

    Interface syntax = keyword + method signatures [OK]
Hint: Interfaces have method signatures only, no bodies [OK]
Common Mistakes:
  • Adding parentheses after interface name
  • Using class keyword instead of interface
  • Providing method bodies inside interface
3.

What will be the output of the following code?

interface IWorker { void Work(); }
class Employee : IWorker {
  public void Work() { Console.WriteLine("Employee working"); }
}
class Robot : IWorker {
  public void Work() { Console.WriteLine("Robot working"); }
}
class Program {
  static void Main() {
    IWorker w = new Robot();
    w.Work();
  }
}
medium
A. No output
B. Employee working
C. Compilation error
D. Robot working

Solution

  1. Step 1: Identify the object type assigned to interface variable

    The variable 'w' is of type IWorker but assigned a new Robot instance.
  2. Step 2: Determine which Work() method runs

    Calling w.Work() runs Robot's Work method, printing "Robot working".
  3. Final Answer:

    Robot working -> Option D
  4. Quick Check:

    Interface variable calls actual object's method [OK]
Hint: Interface calls method of assigned object's class [OK]
Common Mistakes:
  • Assuming interface variable calls Employee method
  • Expecting compilation error due to interface
  • Thinking no output will print
4.

Identify the error in this code snippet:

interface IShape {
  double Area();
}
class Circle : IShape {
  public double Area() {
    return 3.14 * radius * radius;
  }
}
medium
A. Interface method cannot return double
B. Missing radius field or property in Circle class
C. Circle class should not implement IShape
D. Area method should be void

Solution

  1. Step 1: Check Circle class members

    The method Area uses 'radius' but no radius variable or property is declared in Circle.
  2. Step 2: Understand interface method return type

    Interface method returning double is valid; no error there.
  3. Final Answer:

    Missing radius field or property in Circle class -> Option B
  4. Quick Check:

    Undefined variable 'radius' causes error [OK]
Hint: Check all variables used are declared [OK]
Common Mistakes:
  • Thinking interface methods can't return values
  • Believing class can't implement interface
  • Assuming method return type must be void
5.

You want to create a system where different devices can Start() and Stop() but each device does it differently. How should you use interfaces to design this?

hard
A. Create a base class Device with Start and Stop methods and inherit it
B. Write Start and Stop methods directly in each device class without interface
C. Define an interface IDevice with Start and Stop methods, then implement it in each device class
D. Use abstract classes only, no interfaces

Solution

  1. Step 1: Understand interface purpose

    Interfaces define a contract for methods without implementation, perfect for different device behaviors.
  2. Step 2: Apply interface to devices

    Define IDevice with Start and Stop, then each device class implements these methods with its own details.
  3. Final Answer:

    Define an interface IDevice with Start and Stop methods, then implement it in each device class -> Option C
  4. Quick Check:

    Interface = shared method rules, different implementations [OK]
Hint: Use interfaces for shared method names, different code [OK]
Common Mistakes:
  • Using base class limits flexibility
  • Skipping interface loses contract benefits
  • Confusing abstract classes with interfaces