C# Questions & Answers on Fundamental of Generics for Freshers

https://www.computersprofessor.com/2018/07/c-questions-answers-on-fundamental-of_19.html
1. What is meant by the term generics?
a) parameterized types
b) class
c) structure
d) interface
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
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
Answer: d
Explanation: By definition of generics.
4. What does the following code block defines?
class Gen<T>
{
T ob;
}
a) Generics class declaration
b) Generic constructor declaration
c) A simple class declaration
d) All of the mentioned
b) Generic constructor declaration
c) A simple class declaration
d) All of the mentioned
Answer: a
Explanation: class Gen
5. What does the following code set defines?
public Gen(T o)
{
ob = o;
}
a) Generics class decleration
b) Declaration of variable
c) Generic constructor declaration
d) All of the mentioned
b) Declaration of variable
c) Generic constructor declaration
d) All of the mentioned
Answer: c
6. Select the type argument of an open constructed type?
a) Gen
b) Gen
c) Gen<>
d) None of the mentioned
Answer: c
Explanation: A generic type, such as Gen
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
Answer: a
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} { /* … */ }
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
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?
class a
{
public void x(int p, double k)
{
Console.WriteLine("k : csharp!");
}
}
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
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
Answer: b
11. What will be the output of the given code snippet?
public class Generic<T>
{
Stack<T> stk = new Stack<T>();
public void push(T obj)
{
stk.Push(obj);
}
public T pop()
{
T obj = stk.Pop();
return obj;
}
}
class Program
{
static void Main(string[] args)
{
Generic<string> g = new Generic<string>();
g.push(40);
Console.WriteLine(g.pop());
Console.ReadLine();
}
}
a) 0
b) Runtime Error
c) 40
d) Compile time Error
b) Runtime Error
c) 40
d) Compile time Error
Answer: c
Output : 40.
12. What will be the output of the given code snippet?
public class Generic<T>
{
Stack<T> stk = new Stack<T>();
public void push(T obj)
{
stk.Push(obj);
}
public T pop()
{
T obj = stk.Pop();
return obj;
}
}
class Program
{
static void Main(string[] args)
{
Generic<int> g = new Generic<int>();
g.push("Csharp");
Console.WriteLine(g.pop());
Console.ReadLine();
}
}
a) Compile time error
b) Csharp
c) 0
d) Run time error
b) Csharp
c) 0
d) Run time error
Answer: b
Output : Csharp