Generics



1. What are generics in C# ?  

  • A) Generics in C# are reusable methods useful for eliminating parameters .
  • B) Generics in C# are static classes useful for eliminating boxing and un-boxing conversions .
  • C) Generics in C# are type safe reusable types .
  • D) None of the above


2. What is the Benefit of generics in C# ?  

  • A) Generics avoid type safety issues in C# .
  • B) Generic useful for avoiding boxing and unboxing conversions .
  • C) Generics useful for avoiding upcasting or down casting issues .
  • D) All the above


3. How to create a generic class in C# ?  

  • A)
    public  class A {   }
  • B)
    public class A(T1,T2) { }
  • C)
    public class A{  }
  • D)
    public class A {  }


4. How to create an object of a generic class ( identify correct syntax for below given sample generic class )?
 Public class A {  }
 

  • A)
     A a=new A();
  • B)
    A a=new A(); 
  • C)
     A a=new A();
  • D)
     All of the above



5. How to create a generic method in C# ( identify the correct syntax ) ?  

  • A)
    public class A{ public void M() {}  } 
  • B)
    public class A{   public void M(T t) {  } } 
  • C)
    public class A{ public T M() {  } } 
  • D) None of the above


6. How to create a generic interface ?  

  • A)
    public interface I1 {      }
  • B)
    public interface I1(T1 t1,T2 t2) {  }
  • C)
    public interface I1 {  }
  • D) None of the above


7. Which of the following syntax is incorrect ?
 public interface I1{}
 

  • A)
    public interface I2 : I1 { }
  • B)
    public interface I3 : I1 { }
  • C)
    public interface I4 : I1 { }
  • D)
    public interface I5 : I1 { }


8. How to implement generic interface in C# (identify correct syntax which is implementing the below given interface ) ?
public interface I1 {   }
 

  • A)
    public class A:I1 {  } A a=new A();
  • B)
    public class A:I1{  }
  • C)
    public class A:I1 {  }
  • D) None of the above


9. Which keyword is used for specifying constraints for generics type parameters ?  

  • A) constraint
  • B) where
  • C) type
  • D) is


10. How to restrict a generic type parameter to use only a specific class ?  

  • A) public class A where T is ClassName { }
  • B) public class A where T:ClassName { }
  • C) use class name as Type parameter while declaring a class as shown below . public class A { }
  • D) public class A where T as ClassName { }


11. How to restrict a generic type argument to have a public parameter less constructor ?  

  • A) No way to restrict .
  • B) declare a generic class as followed. Public class A where T:ClassName() { }
  • C) public class A where T:new() { }
  • D) public class A { }


12. How to restrict a generic type argument must be a reference type ?  

  • A) No way to restrict .
  • B) public class A where T is ClassName { }
  • C) public class A where T:ReferenceType { }
  • D) public class A where T:class { }


13. What is an unbound type parameter in C# generics ?  

  • A) A type parameter without any restrictions .
  • B) A type parameter with more than one constraint can be called as unbound type parameter .


14. what is the use of default keyword ?  

  • A) default keyword useful for assigning default value to a generic type parameter ( default will give null when type parameter T is a refernce 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 memebers are assigned to zero and all reference type memebers to null ) .
  • B) default keyword usefull for identifying the default value of primitive types in C# .
  • C) First two statements are correct .
  • D) First two statements are incorrect .


15. Which of the following types will not support Generics ?  

  • A) Interface
  • B) delegate
  • C) struct
  • D) enum


16. How to create a generic delegate in C# ?  

  • A) public delegate D(int x,string y);
  • B) public delegate void D(int x,string y);
  • C) public delegate void D(int x,int y) ;
  • D) public delegate void D(int x,string y);


17. Generics are introduced in which of the C# version?  

  • A) C# 1.0
  • B) C# 2.0
  • C) C# 3.5
  • D) C# 4.0


.Net Interview Question

PHP Interview Question

Java Interview Question

AngularJS Interview Questions