C# Question & Answers on Generic Methods for Freshers

1. For the code set given below,which of the following statements are perfectly valid?
  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
2. For the code given below 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
Output : hi
20
3. Which of 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
4. Which statement is valid for the given snippet of 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<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) Run time error
d) Code runs successfully
Answer: d
Output : Hello
5. Choose the output for given set of 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
Answer: b
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
Answer: b
7. For the code set given below,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 arguement must implement Icomparable interface
b) There are multiple constraints on type arguement to MyContainer class
c) Type arguement of class MyContainer should be Icomparable
d) None of the mentioned
Answer: a
8. Choose the statements which are valid for given 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

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
Answer: a
10. What will be the output of given 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
Answer: b
Output : Csharp
11. What will be the output of the given 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
Output : 30.
12. What will be the output of the given 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
Output : C++
20

Related

Multiple Choice Questions 6723474986057466669

Post a Comment

emo-but-icon

item