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

Generic interfaces in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Generic Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of generic interface implementation
What is the output of the following C# code that uses a generic interface?
C Sharp (C#)
using System;

interface IContainer<T>
{
    T GetItem();
}

class Box<T> : IContainer<T>
{
    private T item;
    public Box(T item) { this.item = item; }
    public T GetItem() => item;
}

class Program
{
    static void Main()
    {
        IContainer<string> container = new Box<string>("Hello");
        Console.WriteLine(container.GetItem());
    }
}
AHello
BSystem.String
CBox`1
DCompilation error
Attempts:
2 left
💡 Hint
Look at what the GetItem method returns and what is passed to the Box constructor.
❓ Predict Output
intermediate
2:00remaining
Output of generic interface with multiple types
What will be printed when running this C# program using a generic interface with different types?
C Sharp (C#)
using System;

interface IPrinter<T>
{
    void Print(T value);
}

class Printer<T> : IPrinter<T>
{
    public void Print(T value) => Console.WriteLine($"Value: {value}");
}

class Program
{
    static void Main()
    {
        IPrinter<int> intPrinter = new Printer<int>();
        IPrinter<double> doublePrinter = new Printer<double>();
        intPrinter.Print(5);
        doublePrinter.Print(3.14);
    }
}
AValue: 5\nValue: 3.14
BValue: 5.0\nValue: 3.14
C5\n3.14
DCompilation error due to interface mismatch
Attempts:
2 left
💡 Hint
Check how the Print method formats the output string.
🔧 Debug
advanced
2:00remaining
Identify the error in generic interface implementation
What error will this C# code produce when compiled?
C Sharp (C#)
interface IRepository<T>
{
    void Add(T item);
    T Get(int id);
}

class Repository : IRepository<int>
{
    public void Add(int item) { }
    public string Get(int id) { return "item"; }
}
ARuntime error: InvalidCastException
BNo error, compiles and runs fine
CCompilation error: 'Repository.Get(int)' does not implement interface member 'IRepository<int>.Get(int)'
DCompilation error: Missing method Add
Attempts:
2 left
💡 Hint
Check the return type of the Get method in the class versus the interface.
📝 Syntax
advanced
2:00remaining
Which option correctly declares a generic interface with two type parameters?
Select the correct syntax for declaring a generic interface with two type parameters TKey and TValue in C#.
Ainterface IDictionary<TKey; TValue> { void Add(TKey key, TValue value); }
Binterface IDictionary<TKey, TValue> { void Add(TKey key, TValue value); }
Cinterface IDictionary<TKey TValue) { void Add(TKey key, TValue value); }
Dinterface IDictionary<T1, T2> { void Add(TKey key, TValue value); }
Attempts:
2 left
💡 Hint
Look for correct generic type parameter syntax with angle brackets and commas.
🚀 Application
expert
3:00remaining
Determine the number of items in the generic interface collection
Given the following code, what is the output of the program?
C Sharp (C#)
using System;
using System.Collections.Generic;

interface IStore<T>
{
    void Add(T item);
    int Count { get; }
}

class Store<T> : IStore<T>
{
    private List<T> items = new List<T>();
    public void Add(T item) => items.Add(item);
    public int Count => items.Count;
}

class Program
{
    static void Main()
    {
        IStore<string> store = new Store<string>();
        store.Add("apple");
        store.Add("banana");
        store.Add("cherry");
        Console.WriteLine(store.Count);
    }
}
A0
BCompilation error due to missing Count implementation
CRuntime error: NullReferenceException
D3
Attempts:
2 left
💡 Hint
Count returns the number of items added to the list.

Practice

(1/5)
1. What is the main purpose of a generic interface in C#?
easy
A. To allow the interface to work with any data type specified later
B. To restrict the interface to only one specific data type
C. To make the interface private and inaccessible outside its class
D. To automatically implement all methods without code

Solution

  1. Step 1: Understand generic interfaces

    Generic interfaces use placeholders (type parameters) to allow flexibility in the data types they work with.
  2. Step 2: Identify the purpose

    This flexibility means the same interface can be reused with different types, making code more reusable and type-safe.
  3. Final Answer:

    To allow the interface to work with any data type specified later -> Option A
  4. Quick Check:

    Generic interface = flexible type [OK]
Hint: Generic interfaces use placeholders for types [OK]
Common Mistakes:
  • Thinking generic interfaces fix one type only
  • Confusing generic interfaces with access modifiers
  • Assuming generic interfaces auto-implement methods
2. Which of the following is the correct syntax to declare a generic interface named IContainer with a type parameter T?
easy
A. interface IContainer<T> () { }
B. interface IContainer { T }
C. interface IContainer<T> { }
D. class IContainer<T> { }

Solution

  1. Step 1: Recall generic interface syntax

    Generic interfaces use angle brackets after the interface name to specify type parameters, like IContainer<T>.
  2. Step 2: Check each option

    interface IContainer<T> { } correctly declares a generic interface. interface IContainer { T } is invalid syntax. class IContainer<T> { } uses 'class' instead of 'interface'. interface IContainer<T> () { } has incorrect parentheses.
  3. Final Answer:

    interface IContainer<T> { } -> Option C
  4. Quick Check:

    Generic interface syntax = interface Name<T> [OK]
Hint: Generic interfaces use angle brackets after name [OK]
Common Mistakes:
  • Using class instead of interface keyword
  • Omitting angle brackets for type parameter
  • Adding parentheses after interface name
3. What will be the output of the following C# code?
interface IPrinter<T> { void Print(T item); }
class StringPrinter : IPrinter<string> {
public void Print(string item) { Console.WriteLine(item); }
}
var printer = new StringPrinter();
printer.Print("Hello World");
medium
A. Hello World
B. Compile-time error due to missing type parameter
C. Runtime error due to invalid cast
D. No output

Solution

  1. Step 1: Understand interface implementation

    The class StringPrinter implements IPrinter<string> and defines the Print method to write the string to console.
  2. Step 2: Trace method call

    The Print method is called with "Hello World", so it prints this string to the console.
  3. Final Answer:

    Hello World -> Option A
  4. Quick Check:

    Implemented method prints input string [OK]
Hint: Implemented method prints passed argument [OK]
Common Mistakes:
  • Expecting compile error for correct generic usage
  • Confusing interface with abstract class
  • Thinking Print method returns a value
4. Identify the error in this code snippet:
interface IRepository<T> {
void Add(T item);
}
class Repository : IRepository {
public void Add(object item) { /* implementation */ }
}
medium
A. Interface IRepository cannot have methods
B. Class Repository must specify the type parameter when implementing IRepository
C. Method Add should return a value
D. Class Repository cannot implement interfaces

Solution

  1. Step 1: Check interface implementation

    The interface IRepository is generic and requires a type parameter <T>. The class Repository implements it without specifying this type.
  2. Step 2: Identify the fix

    The class must specify the type parameter, for example IRepository<object>, to correctly implement the interface.
  3. Final Answer:

    Class Repository must specify the type parameter when implementing IRepository -> Option B
  4. Quick Check:

    Generic interface requires type parameter [OK]
Hint: Generic interfaces need type parameters when implemented [OK]
Common Mistakes:
  • Omitting type parameter in implementation
  • Assuming interface methods must return values
  • Thinking classes can't implement interfaces
5. Given the generic interface and classes below, what will be the output?
interface ITransformer<T> {
T Transform(T input);
}
class IntDoubler : ITransformer<int> {
public int Transform(int input) => input * 2;
}
class StringRepeater : ITransformer<string> {
public string Transform(string input) => input + input;
}
var doubler = new IntDoubler();
var repeater = new StringRepeater();
Console.WriteLine(doubler.Transform(5));
Console.WriteLine(repeater.Transform("Hi"));
hard
A. 5 HiHi
B. 55 HiHi
C. 10 Hi
D. 10 HiHi

Solution

  1. Step 1: Analyze IntDoubler.Transform

    This method doubles the integer input. For input 5, output is 5 * 2 = 10.
  2. Step 2: Analyze StringRepeater.Transform

    This method repeats the string input twice. For input "Hi", output is "Hi" + "Hi" = "HiHi".
  3. Final Answer:

    10 HiHi -> Option D
  4. Quick Check:

    Int doubled = 10, string repeated = HiHi [OK]
Hint: Trace each Transform method with given input [OK]
Common Mistakes:
  • Mixing outputs of different classes
  • Forgetting string concatenation doubles text
  • Assuming Transform returns original input
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard