Generics Questions and Answers
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
1. Which one of the following classes are present System.Collections.Generic namespace? Stack Tree SortedDictionary SortedArray
- 1 and 2 only
- 2 and 4 only
- 1 and 3 only
- All of the above
2. For the code snippet shown below, which of the following statements are valid? public class Generic<T> { public T Field; public void TestSub() { T i = Field + 1; } } class MyProgram { static void Main(string[] args) { Generic<int> gen = new Generic<int>(); gen.TestSub(); } }
- Addition will produce result 1.
- Result of addition is system-dependent.
- Program will generate run-time exception.
- Compiler will report an error: Operator '+' is not defined for types T and int.
3. Which of the following statements are valid about generics in .NET Framework? Generics is a language feature. We can create a generic class, however, we cannot create a generic interface in C#.NET. Generics delegates are not allowed in C#.NET. Generics are useful in collection classes in .NET framework. None of the above
- 1 and 2 Only
- 1, 2 and 3 Only
- 1 and 4 Only
- All of the above
4. Which of the following statements is valid about generic procedures in C#.NET?
- All procedures in a Generic class are generic.
- Only those procedures labeled as Generic are generic.
- Generic procedures can take at the most one generic parameter.
- Generic procedures must take at least one type parameter.
5. Which one of the following classes are present System.Collections.Generic namespace? Stack Tree SortedDictionary SortedArray
- 1 and 2 only
- 2 and 4 only
- 1 and 3 only
- All of the above
6. For the code snippet shown below, which of the following statements are valid? public class Generic<T> { public T Field; public void TestSub() { T i = Field + 1; } } class MyProgram { static void Main(string[] args) { Generic<int> gen = new Generic<int>(); gen.TestSub(); } }
- Addition will produce result 1.
- Result of addition is system-dependent.
- Program will generate run-time exception.
- Compiler will report an error: Operator '+' is not defined for types T and int.
7. Which of the following statements are valid about generics in .NET Framework? Generics is a language feature. We can create a generic class, however, we cannot create a generic interface in C#.NET. Generics delegates are not allowed in C#.NET. Generics are useful in collection classes in .NET framework. None of the above
- 1 and 2 Only
- 1, 2 and 3 Only
- 1 and 4 Only
- All of the above
8. Which of the following statements is valid about generic procedures in C#.NET?
- All procedures in a Generic class are generic.
- Only those procedures labeled as Generic are generic.
- Generic procedures can take at the most one generic parameter.
- Generic procedures must take at least one type parameter.
9. For the code snippet given below, which of the following statements is valid? public class Generic<T> { public T Field; } class Program { static void Main(string[ ] args) { Generic<String> g = new Generic<String>(); g.Field = "Hello"; Console.WriteLine(g.Field); } }
- It will print string "Hello" on the console.
- Name Generic cannot be used as a class name because it's a keyword.
- Compiler will give an error.
- Member Field of class Generic is not accessible directly.
10. For the code snippet given below, which of the following statements are valid? public class MyContainer<T> where T: IComparabte { // Insert code here } Class MyContainer requires that it's type argument must implement IComparabte interface. Type argument of class MyContainer must be IComparabte. Compiler will report an error for this block of code. This requirement on type argument is called as constraint.
- 1 and 2 Only
- 1, 2 and 3 Only
- 1 and 4 Only
- All of the above