C# Questions & Answers on Type Interface for Freshers

1. Why are generics used?

a) Generics make code more fast
b) Generics make code more optimised and readable
c) Generics add stability to your code by making more of your bugs detectable at compile time
d) Generics add stability to your code by making more of your bugs detectable at run time
Answer: c

Explanation: Generics add stability to your code by making more of your bugs detectable at compile time.
2. 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.
3. Which of these is an 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.
4. 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<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
5. 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
6. What does the following code block define?
  1.  class Gen<T> {  
  2.                   T ob;    
  3.               }
a) Generics class decleration
b) Decleration of variable
c) A simple class decleration
d) Both a & b
Answer: d

Explanation: class Gen This defines the generics declaration where ‘T’ is the name of 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’.
7. 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
Answer: c
Output : C++
20
8. Select the type argument of open constructed type?

a) Gen
b) Gen
c) Gen<>
d) None of the mentioned
Answer: c

Explanation: A generic type, such as Gen, is an abstraction.In C# terminology, a construct such as Gen is called an open constructed type, because the type parameter T (rather than an actual type, such as int) is specified.
9. 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
Answer: b
10. What does the following code set defines?
  1.  public Gen(T o) {
  2.                      ob = o; 
  3.                  }
a) Generics class decleration
b) Decleration of variable
c) Generic constructor decleration
d) All of the mentioned
Answer: c

Related

CSS Multiple Choice Questions & Answers on Styling Lists and Navigation Menus for Freshers

1. Which of the following property sets list-style-type, list-style-position, and liststyle-image? a) styleb) list-stylec) menu-styled) img-style Answer: b Explanation: Syntax: list-style...

C Programming Questions and Answers on Register Variables for Freshers

1. What is the output of this C code? #include int main() { register int i = 10; int *p = &i; *p = 11; printf("%d %d\n", i, *p); ...

HTML Multiple Choice Questions & Answers on Compatibility Libraries for Freshers

1. Which one of the following is an open source JavaScript library that enables styling for HTML5 elements in versions of Internet Explorer before IE 9 ? a) HTML5b) HTML5 Shivc) HTML5 Shimd) None o...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item