Generics Questions and Answers
Practice ModeShowing 10 of 32 questions
Q21
What is type erasure in context of C# generics?
Answer: Option B
Explanation: C# generics are reified and maintain type information at runtime, unlike Java.
Q22
Which collection is generic in C#?
Answer: Option C
Explanation: List<T> is a generic collection, while others are non-generic.
Q23
What does covariance allow in generics?
Answer: Option B
Explanation: Covariance enables using a more derived type than originally specified.
Q24
Which interface supports covariance in C#?
Answer: Option C
Explanation: IEnumerable<T> is covariant, allowing assignment compatibility.
Q25
What is the purpose of the "in" keyword in generic interfaces?
Answer: Option B
Explanation: The "in" keyword makes a generic parameter contravariant.
Q26
Which constraint would you use for value types?
Answer: Option B
Explanation: The "struct" constraint restricts to value types only.
Q27
What happens if you don't specify constraints on a generic type?
Answer: Option B
Explanation: Without constraints, only operations valid for System.Object are available.
Q28
How do you create a generic method?
Answer: Option A
Explanation: Generic methods have type parameters declared after the method name.
Q29
Which of these is a benefit of generics?
Answer: Option B
Explanation: Generics provide type safety and eliminate boxing/unboxing for value types.
Q30
What does the "out" keyword do in generic interfaces?
Answer: Option C
Explanation: The "out" keyword makes a generic parameter covariant.