C # - Generics Question and Answer

C # - Generics Question and Answer
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
Show Answer
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.
Show Answer
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
Show Answer
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.
Show Answer
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
Show Answer
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.
Show Answer
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
Show Answer
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.
Show Answer
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.
Show Answer
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
Show Answer
Questions and Answers for Competitive Exams Various Entrance Test