C# Multiple Choice Questions – Generics

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

1. What is meant by the term generics?
a) parameterized types
b) class
c) structure
d) interface
View Answer

Answer: a
Explanation: The term generics means parameterized types. Parameterized types are important because they enable us to create classes, structures, interfaces, methods, and delegates in which, the type of data upon which they operate is specified as a parameter.

2. Are generics in C# are same as the generics in java and templates in C++?
a) Yes
b) No
c) May be
d) None of the mentioned
View Answer

Answer: b
Explanation: Although C# generics are similar to templates in C++ and generics in Java, they are not the same as either. In fact, there are some fundamental differences among these three approaches to generics.

3. Choose the advantages of using generics?
a) Generics facilitate type safety
b) Generics facilitate improved performance and reduced code
c) Generics promote the usage of parameterized types
d) All of the mentioned
View Answer

Answer: d
Explanation: By definition of generics.
advertisement
advertisement

4. What does the following C# code block defines?

  1.  class Gen<T> 
  2.  {  
  3.       T ob;    
  4.  }

a) Generics class declaration
b) Generic constructor declaration
c) A simple class declaration
d) All of the mentioned
View Answer

Answer: a
Explanation: class Gen<T> This defines the generics declaration where ‘T’ is the name of the type parameter. This parameter is used as a placeholder for the actual type that will be specified when a Gen object is created. Gen is a generic class. T is used to declare a variable called ‘ob’.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

5. What does the following C# code set defines?

advertisement
  1.  public Gen(T o) 
  2.  {
  3.       ob = o; 
  4.  }

a) Generics class Declaration
b) Declaration of variable
c) Generic constructor declaration
d) All of the mentioned
View Answer

Answer: c
Explanation: None.
advertisement

6. Select the type argument of an open constructed type?
a) Gen<int>
b) Gen<T>
c) Gen<>
d) None of the mentioned
View Answer

Answer: c
Explanation: A generic type, such as Gen<T>, is an abstraction. In C# terminology, a construct such as Gen<T> is called an open constructed type, because the type parameter T (rather than an actual type, such as int) is specified.

7. Which among the given classes is present in System.Collection.Generic.namespace?
a) Stack
b) Tree
c) Sorted Array
d) All of the mentioned
View Answer

Answer: a
Explanation: None.

8. Which of these is a correct way of defining generic method?
a) name(T1, T2, …, Tn) { /* … */ }
b) public name { /* … */ }
c) class name[T1, T2, …, Tn] { /* … */ }
d) name{T1, T2, …, Tn} { /* … */ }
View Answer

Answer: b
Explanation: The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the method’s return type. For static generic methods, the type parameter section must appear before the method’s return type.

9. Which of these type parameters is used for generic methods to return and accept any type of object?
a) K
b) N
c) T
d) V
View Answer

Answer: c
Explanation: T is used for type, A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable.

10. Choose the correct way to call subroutine fun() of the sample class?

  1. class a
  2. {
  3.     public void x(int p, double k)
  4.     {
  5.         Console.WriteLine("k : csharp!");
  6.     }
  7. }

a)

   delegate void del(int i);
   x s = new x();
   del d = new del(ref s.x);
   d(8, 2.2f);

b)

   delegate void del(int p,  double k);
   del d;
   x s = new x();
   d = new del(ref s.x);
   d(8, 2.2f);

c)

   x s = new x();
   delegate void d = new del(ref x);
   d(8, 2.2f);

d) all of the mentioned
View Answer

Answer: b
Explanation: None.

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(40);
  20.         Console.WriteLine(g.pop());
  21.         Console.ReadLine();
  22.     }
  23. }

a) 0
b) Runtime Error
c) 40
d) Compile time Error
View Answer

Answer: c
Explanation: None.
Output :

40

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

More MCQs on C# Generics:

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.