Generics Questions and Answers

Take Exam

Generics in C# programming questions with answers test your knowledge of type safety, reusability, and performance in modern C# development. Understanding generics is crucial for building flexible and maintainable code, especially for technical interviews and placement tests at companies like Capgemini, Accenture, and Cognizant. These programming questions and answers cover key topics like generic classes, methods, constraints, and collections. Practicing these MCQs enhances your ability to write type-safe and efficient code, a skill highly valued in software engineering roles. Try solving C# generics MCQs online or download C# programming practice PDFs for self-assessment.

Generics

Showing 10 of 32 questions

11. 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.

  • 1 and 2 Only
  • 3 and 4 Only
  • 2 and 3 Only
  • All of the above
Show Answer Report

12. Which of the following statements is valid about advantages of generics?

  • Generics shift the burden of type safety to the programmer rather than compiler.
  • Generics require use of explicit type casting.
  • Generics provide type safety without the overhead of multiple implementations.
  • Generics eliminate the possibility of run-time errors.
Show Answer Report

13. What is the main purpose of generics in C#?

  • To improve performance
  • To enable type-safe data structures
  • To simplify syntax
  • To allow dynamic typing
Show Answer Report

14. Which keyword is used to define a generic class in C#?

  • generic
  • type
  • <>
  • T
Show Answer Report

15. What is a constraint in C# generics?

  • A way to optimize generic code
  • A restriction on generic type parameters
  • A method to instantiate generics
  • A technique for generic inheritance
Show Answer Report

16. Which of the following is a valid generic class definition?

  • class MyClass<T> { }
  • class MyClass<generic> { }
  • class MyClass[T] { }
  • class MyClass{T} { }
Show Answer Report

17. What does the "where T : class" constraint mean?

  • T must be a value type
  • T must be a reference type
  • T must be a static class
  • T must be an abstract class
Show Answer Report

18. Which method correctly uses generics?

  • void Method<T>() { }
  • void Method(T) { }
  • void Method<>() { }
  • void Method<type>() { }
Show Answer Report

19. What is the default value of a generic type parameter?

  • null
  • 0
  • default(T)
  • new T()
Show Answer Report

20. Which constraint ensures a type has a parameterless constructor?

  • where T : constructor
  • where T : new()
  • where T : parameterless
  • where T : default
Show Answer Report
Questions and Answers for Competitive Exams Various Entrance Test