Generic Methods MCQ Set-2

Errorlogger
0
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 it’s type arguement must implement Icomparable interface
b) There are multiple constraints on type arguement to MyConatiner class
c) Compiler will report an error
d) None of the mentioned



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 run successfully and print required output
d) None of the mentioned



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 language feature
d) All of the mentioned

e) both a and c


4. Which statement are 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



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



6. Correct statement valid 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



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 it’s 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



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 run successfully print nothing
b) code run successfully print 1
c) Program will give run time error
d) Compile time error



9. Which among the given class represents System.Collections.Generic namespace?
a) SortedDictionary
b) Sorted Array
c) Stack
d) Tree

e) both a and c


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



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



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

Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(30)

Our website uses cookies to enhance your experience. Check Now
Accept !