C# Question & Answers – Generic Methods

This section of our 1000+ C# MCQs focuses on generic methods in C# Programming Language.

1. In the following C# code, which of the following statement is not correct?

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

a) Class MyContainer requires that its type argument must implement Icomparable interface
b) There are multiple constraints on type argument to MyContainer class
c) Class MyContainer requires that its type argument must be a reference type (class)
d) Compiler will report an error
View Answer

Answer: d
Explanation: None.
advertisement
advertisement

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
View Answer

Answer: c
Explanation: None.
Output :

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
         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
View Answer

Answer: a
Explanation: None.
advertisement

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

advertisement
  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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
Explanation: None.
Output :

 A

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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

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

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

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: None.
Output :

Csharp

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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: None.
Output :

         C++
         20

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.