Generics Questions

Practice Generics MCQs with answers and explanations. Page 1 of 1.

Category
C# Programming
Topic
Generics
Page
1 / 1
Mode
Practice

Questions

Open any question to view the answer and explanation.

C#.NET collections — Which of the following types are present in the System.Collections.Generic namespace? 1) Stack 2) Tree 3) SortedDictionary 4) SortedArray
Open
View answer
C#.NET generics — What will the following code do? public class TestCuriousTab { public void TestSub<M>(M arg) { Console.Write(arg); } } class MyProgram { static void Main(string[] args) { TestCuriousTab tab = new TestCuriousTab(); tab.TestSub("CuriousTab "); tab.TestSub(4.2f); } }
Open
View answer
C#.NET generics — What happens in this generic class when using an operator on T? 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(); } }
Open
View answer
C#.NET generics — Which statement about generic procedures (generic methods) is valid?
Open
View answer
C#.NET generics — Which statement correctly states an advantage of generics?
Open
View answer
.NET Framework generics — Which statements are valid? 1) Generics is a language feature. 2) We can create a generic class, however, we cannot create a generic interface in C#.NET. 3) Generic delegates are not allowed in C#.NET. 4) Generics are useful in collection classes in the .NET Framework. 5) None of the above.
Open
View answer
C#.NET generics — What is the result of this code? 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); } }
Open
View answer
C#.NET Generics — interpret the where-constraint. Given: public class MyContainer<T> where T : IComparable { // Insert code here } Which statements are valid? Class MyContainer requires that its type argument must implement the IComparable interface. The type argument of class MyContainer must be IComparable itself (not just implement it). The compiler will report an error for this block of code. This requirement placed on the type argument is called a constraint.
Open
View answer
C#.NET Generics — multiple constraints: reference type + interface. Given: public class MyContainer<T> where T : class, IComparable { // Insert code here } Which statements are valid? MyContainer requires that its type argument implement IComparable. The compiler will report an error for this block of code. There are multiple constraints on the type argument. The type argument must be a reference type and must implement IComparable.
Open
View answer

Practice smarter

Solve a few questions daily and revisit weak topics regularly to improve accuracy.