Generics Questions and Answers

Practice Mode
Showing 10 of 32 questions
Q21
What is type erasure in context of C# generics?
  • A C# removes type information at runtime
  • B C# maintains type information at runtime
  • C Type information is stored in metadata only
  • D Types are converted to object references
Answer: Option B
Explanation: C# generics are reified and maintain type information at runtime, unlike Java.
Q22
Which collection is generic in C#?
  • A ArrayList
  • B Hashtable
  • C List<T>
  • D Queue
Answer: Option C
Explanation: List<T> is a generic collection, while others are non-generic.
Q23
What does covariance allow in generics?
  • A Using less derived types
  • B Using more derived types
  • C Using any type
  • D Using only value types
Answer: Option B
Explanation: Covariance enables using a more derived type than originally specified.
Q24
Which interface supports covariance in C#?
  • A IList<T>
  • B ICollection<T>
  • C IEnumerable<T>
  • D IDictionary<T>
Answer: Option C
Explanation: IEnumerable<T> is covariant, allowing assignment compatibility.
Q25
What is the purpose of the "in" keyword in generic interfaces?
  • A Makes parameter covariant
  • B Makes parameter contravariant
  • C Restricts to input parameters
  • D Allows output parameters only
Answer: Option B
Explanation: The "in" keyword makes a generic parameter contravariant.
Q26
Which constraint would you use for value types?
  • A where T : class
  • B where T : struct
  • C where T : value
  • D where T : valuetype
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?
  • A All operations are available
  • B Only System.Object operations are available
  • C The type cannot be used
  • D Runtime type checking is enforced
Answer: Option B
Explanation: Without constraints, only operations valid for System.Object are available.
Q28
How do you create a generic method?
  • A void Method<T> { }
  • B void Method(T param) { }
  • C void <T>Method() { }
  • D void Method()<T> { }
Answer: Option A
Explanation: Generic methods have type parameters declared after the method name.
Q29
Which of these is a benefit of generics?
  • A Reduced code readability
  • B Eliminates boxing and unboxing
  • C Increased memory usage
  • D Dynamic type checking
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?
  • A Makes parameter input-only
  • B Makes parameter output-only
  • C Makes parameter covariant
  • D Makes parameter contravariant
Answer: Option C
Explanation: The "out" keyword makes a generic parameter covariant.
Questions and Answers for Competitive Exams Various Entrance Test