Select chapter



Basics (2.papers)
Data Types (2.papers)
enums (2.papers)
OOPS (4.papers)
Arrays (1.papers)
Structs (2.papers)
Properties (2.papers)
Strings (2.papers)
Interfaces (1.papers)
Constructors (2.papers)
Ref-Out-Keywords (1.papers)
Collections (2.papers)
Generics (1.papers)
Hide

Sponsored Links


  

c# generics interview questions and answers

1
What are Generics in C# ?
  1. Generics in C# are reusable methods useful for eliminating parameters.
  2. Generics in C# are type safe reusable types.
  3. Generics in C# are static classes useful for eliminating boxing and unboxing conversions.
  4. None of the above.
Show Answer
Share Facebook gmail Twitter
2
What is the Benefit of Generics in C# ?
  1. Generics avoid type safety issues in C#.
  2. Generics are useful for avoiding boxing and unboxing conversions.
  3. Generics are useful for avoiding upcasting or downcasting issues.
  4. All the above.
Show Answer
Share Facebook gmail Twitter
3
How to create a generic class in C# ?
  1. public  class A<T1,T2> {   }
  2. public class A(T1,T2) { }
  3. public class A{  }
  4. public class A<dynamic T1,dynamic T2> {  }
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
5
How to create a Generic method in C# (identify the correct syntax) ?
  1. public class A{ public void M<T>() {}  }
  2. public class A<T>{
      public void M(T t) {  }
    }
  3. public class A{ public T M<dynamic T>() {  } }
  4. None of the above.
Show Answer
Share Facebook gmail Twitter
6
How to create a Generic interface?
  1. public interface I1<T1,T2> {      }
  2. public interface I1(T1 t1,T2 t2) {  }
  3. public interface I1<dynamic T1,dynamic T2 > {  }
  4. None of the above .
Show Answer
Share Facebook gmail Twitter
7
Which of the following syntax is incorrect  for the given Generic interface I1?
public interface I1<T>{}
  1. public interface I2 : I1<int> { }
  2. public interface I3<T> : I1<T> { }
  3. public interface I4<T> : I1<Z> { }
  4. public interface I5<T> : I1<int> { }
Show Answer
Share Facebook gmail Twitter
8
How to implement generic interface in C# (identify correct syntax which is implementing the below given interface )?
public interface I1<T> {   }
  1. public class A:I1<T> {  }
    A<int> a=new A<int>();
  2. public class A<T>:I1{  }
    
  3. public class A:I1<int> {  }
  4. None of the above.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
10
How to restrict a Generic type parameter to use only a specific class ?
  1. use where keyword as shown below.
    Public class A<T> where T:ClassName
    {
    }
  2. public class A<T> where T is ClassName {  }
  3. use class name as Type parameter while declaring a class as shown below.
    public class A<ClassName>
    {
    }
  4. public class A<T> where T as ClassName {  }
Show Answer
Share Facebook gmail Twitter
11
How to restrict a Generic type argument to have a public parameter less constructor ?
  1. No way to restrict.
  2. declare a generic class as followed.
    Public class A<T> where T:ClassName()
    {
    }
  3. public class A<T> where T:new()
    {
    }
  4. public class A<ClassName()>
    {
    }
Show Answer
Share Facebook gmail Twitter
12
How to restrict a Generic type argument must be a reference type ?
  1. No way to restrict.
  2. public class A<T> where T is ClassName {  }
  3. public class A<T> where T:ReferenceType
    {
    }
  4. public class A<T> where T:class {  }
Show Answer
Share Facebook gmail Twitter
13
What is an unbound type parameter in C# Generics?
  1. A Type parameter without any restrictions.
  2. A Type parameter with more than one constraint can be called as unbound type parameter.
Show Answer
Share Facebook gmail Twitter
14
What is the use of default keyword?
  1. default keyword is useful for assigning default value to a generic type parameter ( default will give null when type parameter T is a reference type, it gives zero when T is a value type and if it is not a struct. Default gives a struct object if T is a struct and all struct value type members are assigned to zero and all reference type members to null).
  2. default keyword is useful for identifying the default value of primitive types in C# .
  3. First two statements are correct.
  4. First two statements are incorrect.
Show Answer
Share Facebook gmail Twitter
15
Which of the following syntax is correct for returning an addition result of two numbers when type parameter is int type and concatenation result if type parameter is of  type string ?
  1. public class GenericCalc<T> where T:IConvertible
    {
        public T Add(T t1, T t2)
        {
            return t1 + t2;
        }
    }
  2. public class GenericCalc<T> where T:IConvertible
    {
        public T Add(T t1, T t2)
        {
            T t3 = default(T);
            if (t1 is int)
            {
                int x = t1.ToInt32(CultureInfo.CurrentUICulture) + t2.ToInt32(CultureInfo.CurrentUICulture);
                return (T)System.Convert.ChangeType(x, typeof(T));
            }
            else if(t1 is string)
            {
                string  x = t1.ToString() + t2.ToString();
                return (T)System.Convert.ChangeType(x, Type.GetTypeCode(typeof(T)));
            }
            return t3;
        }
    }
  3. public class GenericCalc<T> where T:IConvertible
    {
        public T Add(T t1, T t2)
        {
            if (t1 is int && t2 is int)
            {
                return t1 as int + t2 as int;
            }
            else
                return t1 as string + t2 as string;
        }
    }
  4. None of the above.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
17
How to create a Generic delegate in C# ?
  1. public delegate D<T>(int x,string y);
  2. public delegate void D<T>(int x,string y);
  3. public delegate void D(int x,int y)<T> ;
  4. public delegate<T> void D(int x,string y);
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Read more papers :