Challenge - 5 Problems
Generic Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()); } }
Attempts:
2 left
💡 Hint
Look at what the GetItem method returns and what is passed to the Box constructor.
✗ Incorrect
The Box class implements IContainer and stores the item passed in the constructor. The GetItem method returns that item, which is the string "Hello".
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Check how the Print method formats the output string.
✗ Incorrect
The Print method writes "Value: " followed by the value passed. For int 5 and double 3.14, it prints exactly those values with the prefix.
🔧 Debug
advanced2: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"; } }
Attempts:
2 left
💡 Hint
Check the return type of the Get method in the class versus the interface.
✗ Incorrect
The interface expects Get to return int, but the class returns string, causing a compilation error.
📝 Syntax
advanced2: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#.
Attempts:
2 left
💡 Hint
Look for correct generic type parameter syntax with angle brackets and commas.
✗ Incorrect
Option B correctly uses angle brackets with comma-separated type parameters and valid method signature.
🚀 Application
expert3: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); } }
Attempts:
2 left
💡 Hint
Count returns the number of items added to the list.
✗ Incorrect
Three items are added to the Store, so Count returns 3.