Generics Questions and Answers

Practice Mode
Showing 10 of 32 questions
Q11
For the code snippet given below, which of the following statements are valid? public class MyContainer<T> where T: class, IComparable {     //Insert code here }     Class MyContainer requires that it's type argument must implement IComparable interface.     Compiler will report an error for this block of code.     There are multiple constraints on type argument to MyContainer class.     Class MyContainer requires that its type argument must be a reference type and it must implement IComparable interface.
  • A 1 and 2 Only
  • B 3 and 4 Only
  • C 2 and 3 Only
  • D All of the above
Answer: Option B
Q12
Which of the following statements is valid about advantages of generics?
  • A Generics shift the burden of type safety to the programmer rather than compiler.
  • B Generics require use of explicit type casting.
  • C Generics provide type safety without the overhead of multiple implementations.
  • D Generics eliminate the possibility of run-time errors.
Answer: Option C
Q13
What is the main purpose of generics in C#?
  • A To improve performance
  • B To enable type-safe data structures
  • C To simplify syntax
  • D To allow dynamic typing
Answer: Option B
Explanation: Generics allow type-safe data structures without committing to actual data types.
Q14
Which keyword is used to define a generic class in C#?
  • A generic
  • B type
  • C <>
  • D T
Answer: Option C
Explanation: The angle brackets <> are used with type parameters to define generics.
Q15
What is a constraint in C# generics?
  • A A way to optimize generic code
  • B A restriction on generic type parameters
  • C A method to instantiate generics
  • D A technique for generic inheritance
Answer: Option B
Explanation: Constraints restrict the types that can be used as arguments for generic parameters.
Q16
Which of the following is a valid generic class definition?
  • A class MyClass<T> { }
  • B class MyClass<generic> { }
  • C class MyClass[T] { }
  • D class MyClass{T} { }
Answer: Option A
Explanation: Generic classes use type parameters in angle brackets.
Q17
What does the "where T : class" constraint mean?
  • A T must be a value type
  • B T must be a reference type
  • C T must be a static class
  • D T must be an abstract class
Answer: Option B
Explanation: This constraint restricts T to reference types only.
Q18
Which method correctly uses generics?
  • A void Method<T>() { }
  • B void Method(T) { }
  • C void Method<>() { }
  • D void Method<type>() { }
Answer: Option A
Explanation: Generic methods can be defined within both generic and non-generic classes.
Q19
What is the default value of a generic type parameter?
  • A null
  • B 0
  • C default(T)
  • D new T()
Answer: Option C
Explanation: The default keyword returns appropriate default values for value and reference types.
Q20
Which constraint ensures a type has a parameterless constructor?
  • A where T : constructor
  • B where T : new()
  • C where T : parameterless
  • D where T : default
Answer: Option B
Explanation: The new() constraint requires a public parameterless constructor.
Questions and Answers for Competitive Exams Various Entrance Test