250+ TOP MCQs on Generic Methods and Answers

C# MCQs on generic methods in C# Programming Language.

1. In the following C# code, which of the following statements are perfectly valid?

advertisement

  1. public class MyContainer<T> where T: class, IComparable
  2. {
  3.   /* insert code here */
  4. }

a) Class MyConatiner requires that its type argument must implement Icomparable interface
b) There are multiple constraints on type argument to MyConatiner class
c) Compiler will report an error
d) None of the mentioned

Answer: b
Clarification: None.

2. In the following C# code, which statements are perfectly valid?

  1. public class Csharp
  2. {
  3.     public void subject<S>(S arg)
  4.     {
  5.         Console.WriteLine(arg);
  6.     }
  7. }
  8. class Program
  9. {
  10.     static Void Main(string[] args)
  11.     {
  12.         Csharp c = new Csharp();
  13.         c.subject("hi");
  14.         c.subject(20);
  15.     }
  16. }

a) Run time exception error
b) Compile time error
c) Code runs successfully and prints required output
d) None of the mentioned

Answer: c
Clarification: None.
Output :

         hi
         20

3. Which of the given statements are valid about generics in .NET Framework?
a) generics are useful in collection classes in .NET framework
b) generics delegates are not allowed in C#.NET
c) generics is a not language feature
d) all of the mentioned

Answer: a
Clarification: None.

advertisement

4. Which statement is valid for the following C# code snippet?

  1. public class Generic<T>
  2. {
  3.     public T Field;
  4. }
  5. class Program
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         Generic<String> g = new Generic<String>();
  10.         g.Field = "Hi";
  11.         Console.WriteLine(g.Field);
  12.     }
  13. }

a) Compile time error
b) Generic being a keyword cannot be used as a class name
c) Runtime error
d) Code runs successfully

Answer: d
Clarification: None.
Output :

Hello

5. What will be the output of the following C# code?

  1.  public class Generic<T>
  2.  {
  3.      public T Field;
  4.  }
  5.  class Program
  6.  {
  7.      static void Main(string[] args)
  8.      {
  9.          Generic<int> g2 = new Generic<int>();
  10.          Generic<int> g3 = new Generic<int>();
  11.          g2.Field = 8;
  12.          g3.Field = 4;
  13.          if (g2.Field % g3.Field == 0)
  14.          {
  15.              Console.WriteLine("A");
  16.          }
  17.          else
  18.          Console.WriteLine("Prints nothing:");
  19.          Console.ReadLine();
  20.      }
  21.  }

a) Compile time error
b) A
c) Run time error
d) Code runs successfully but prints nothing

6. Which of the following is a valid statement about generic procedures in C#.NET are?
a) All procedures in a Generic class are generic
b) Generic procedures should take at least one type parameter
c) Only those procedures labeled as Generic are Generic
d) None of the mentioned

Answer: b
Clarification: None.

7. In the following C# code, which of the following statements are perfectly valid?

  1. public class MyContainer<T> where T: IComparable
  2. {
  3.   /* insert code here */
  4. }

a) Class MyConatiner requires that its type argument must implement Icomparable interface
b) There are multiple constraints on type argument to MyContainer class
c) Type argument of class MyContainer should be Icomparable
d) None of the mentioned

Answer: a
Clarification: None.

8. Which of the following statements are valid in the following C# code snippet?

  1. public class Generic<T>
  2. {
  3.     public T Field;
  4.     public void testSub()
  5.     {
  6.         T i = Field + 1;
  7.     }
  8. }
  9. class Program
  10. {
  11.     static void Main(string[] args)
  12.     {
  13.         Generic<int>g = new Generic<int>();
  14.         g.testSub();
  15.     }
  16. }

a) code runs successfully but prints nothing
b) code runs successfully and prints 1
c) program will give run time error
d) compile time error

Answer: d
Clarification: Compiler will give error as operator ‘+’ is not defined for types ‘T’ and ‘int’.

advertisement

9. Which among the given classes represents System.Collections.Generic namespace?
a) SortedDictionary
b) Sorted Array
c) Stack
d) All of the mentioned

Answer: a
Clarification: None.

10. What will be the output of the following C# code snippet?

  1. public class Generic<T>
  2. {
  3.     Stack<T> stk = new Stack<T>();
  4.     public void push(T obj)
  5.     {
  6.         stk.Push(obj);
  7.     }
  8.     public T pop()
  9.     {
  10.         T obj = stk.Pop();
  11.         return obj;
  12.     }
  13. }
  14. class Program
  15. {
  16.     static void Main(string[] args)
  17.     {
  18.         Generic<int> g = new Generic<int>();
  19.         g.push("Csharp");
  20.         Console.WriteLine(g.pop());
  21.         Console.ReadLine();
  22.     }
  23. }

a) Compile time error
b) Csharp
c) 0
d) Run time error

11. What will be the output of the following C# code snippet?

  1. public class Generic<T>
  2. {
  3.     Stack<T> stk = new Stack<T>();
  4.     public void push(T obj)
  5.     {
  6.         stk.Push(obj);
  7.     }
  8.     public T pop()
  9.     {
  10.         T obj = stk.Pop();
  11.         return obj;
  12.     }
  13. }
  14. class Program
  15. {
  16.     static void Main(string[] args)
  17.     {
  18.         Generic<string> g = new Generic<string>();
  19.         g.push(30);
  20.         Console.WriteLine(g.pop());
  21.         Console.ReadLine();
  22.     }
  23. }

a) 0
b) 30
c) Runtime Error
d) Compile time Error

Answer: b
Clarification: None.
Output :

30.

12. What will be the output of the following C# code snippet?

  1. public class Generic<T>
  2. {
  3.     Stack<T> stk = new Stack<T>();
  4.     public void push(T obj)
  5.     {
  6.         stk.Push(obj);
  7.     }
  8.     public T pop()
  9.     {
  10.         T obj = stk.Pop();
  11.         return obj;
  12.     }
  13. }
  14. class Program
  15. {
  16.     static void Main(string[] args)
  17.     {
  18.         Generic<string> g = new Generic<string>();
  19.         g.push("C++");
  20.         Console.WriteLine(g.pop() + " ");
  21.         Generic<int> g1 = new Generic<int>();
  22.         g1.push(20);
  23.         Console.WriteLine(g1.pop());
  24.         Console.ReadLine();
  25.     }
  26. }

a) C++
b) 20
c)

C++
20

d) 0

Answer: c
Clarification: None.
Output :

         C++
         20

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top