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
Recall & Review
beginner
What is an interface in C#?
An interface in C# is like a contract that defines a set of methods and properties without implementing them. Classes that use the interface promise to implement those methods and properties.
Click to reveal answer
beginner
Why do we say an interface acts as a contract?
Because it sets clear rules (methods and properties) that any class implementing it must follow, ensuring consistent behavior across different classes.
Click to reveal answer
intermediate
Can a class implement multiple interfaces in C#?
Yes, a class can implement multiple interfaces, meaning it agrees to follow multiple contracts at the same time.
Click to reveal answer
intermediate
What happens if a class does not implement all members of an interface it declares?
The C# compiler will give an error because the class breaks the contract by not providing all required methods or properties.
Click to reveal answer
intermediate
How does using interfaces improve code flexibility?
Interfaces allow different classes to be used interchangeably if they follow the same contract, making code easier to extend and maintain.
Click to reveal answer
What does an interface in C# define?
AA class with full method implementations
BA variable type
CA set of method and property signatures without implementation
DA method that runs automatically
✗ Incorrect
An interface only defines method and property signatures, acting as a contract without implementation.
If a class implements an interface, what must it do?
AImplement all methods and properties declared in the interface
BIgnore the interface methods
COnly implement some methods
DInherit from another class
✗ Incorrect
Implementing an interface means the class must provide code for all its methods and properties.
Can a class implement more than one interface in C#?
AYes, it can implement multiple interfaces
BNo, only one interface is allowed
COnly if interfaces are related
DOnly if the class is abstract
✗ Incorrect
C# allows a class to implement multiple interfaces, combining multiple contracts.
What is the main benefit of using interfaces?
AThey replace classes
BThey store data
CThey speed up program execution
DThey allow different classes to be used interchangeably
✗ Incorrect
Interfaces enable polymorphism by allowing different classes to follow the same contract and be used interchangeably.
What error occurs if a class does not implement all interface members?
AWarning only
BCompiler error
CNo error
DRuntime error
✗ Incorrect
The compiler enforces the contract and will raise an error if the class does not implement all required members.
Explain the idea of an interface as a contract in C# using a real-life example.
Think about agreements you make in daily life that require you to do certain things.
You got /4 concepts.
Describe how interfaces help make code more flexible and easier to maintain.
Consider how different devices can use the same charger type because they follow the same standard.
You got /4 concepts.
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
Step 1: Understand the role of an interface
An interface defines a set of method signatures without implementations.
Step 2: Compare with classes
Classes implement interfaces by providing method bodies, fulfilling the contract.
Final Answer:
A contract that defines methods a class must implement -> Option A
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
Step 1: Check interface declaration syntax
Interfaces use the keyword 'interface' followed by the name and method signatures without bodies.
Step 2: Identify correct method signature
Method declarations in interfaces do not have bodies, so no curly braces after method.
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
Step 1: Identify the object type assigned to interface variable
The variable 'w' is of type IWorker but assigned a new Robot instance.
Step 2: Determine which Work() method runs
Calling w.Work() runs Robot's Work method, printing "Robot working".
Final Answer:
Robot working -> Option D
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
Step 1: Check Circle class members
The method Area uses 'radius' but no radius variable or property is declared in Circle.
Step 2: Understand interface method return type
Interface method returning double is valid; no error there.
Final Answer:
Missing radius field or property in Circle class -> Option B
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
Step 1: Understand interface purpose
Interfaces define a contract for methods without implementation, perfect for different device behaviors.
Step 2: Apply interface to devices
Define IDevice with Start and Stop, then each device class implements these methods with its own details.
Final Answer:
Define an interface IDevice with Start and Stop methods, then implement it in each device class -> Option C
Quick Check:
Interface = shared method rules, different implementations [OK]
Hint: Use interfaces for shared method names, different code [OK]