Java Generics(Engineering > Computer Science And Engineering ) Questions and Answers

Question 1. Which of these type parameters is used for a generic class to return and accept a number?
  1.    K
  2.    N
  3.    T
  4.    V
Explanation:-
Answer: Option B. -> N





Question 2. Which of the following is Faster, StringBuilder or StringBuffer?
  1.    StringBuilder
  2.    StringBuffer
  3.    Both of the above
  4.    None of the above
Explanation:-
Answer: Option A. -> StringBuilder





Question 3. Which of the following allows us to call generic methods as a normal method?
  1.    Type Interface
  2.    Interface
  3.    Inner class
  4.    All of the mentioned
Explanation:-
Answer: Option A. -> Type Interface





Question 4. Which of the following is incorrect statement regarding the use of generics and parameterized types in Java?
  1.    Generics provide type safety by shifting more type checking responsibilities to the compiler
  2.    Generics and parameterized types eliminate the need for down casts when using Java Collections
  3.    When designing your own collections class (say, a linked list), generics and parameterized types allow you to achieve type safety with just a single class definition as opposed to defining multiple classes
  4.    All of the mentioned
Explanation:-
Answer: Option C. -> When designing your own collections class (say, a linked list), generics and parameterized types allow you to achieve type safety with just a single class definition as opposed to defining multiple classes





Question 5. Why are generics used?
  1.    Generics make code more fast
  2.    Generics make code more optimised and readable
  3.    Generics add stability to your code by making more of your bugs detectable at compile time
  4.    Generics add stability to your code by making more of your bugs detectable at a runtime
Explanation:-
Answer: Option C. -> Generics add stability to your code by making more of your bugs detectable at compile time





Question 6. Which of the following reference types cannot be generic?
  1.    Anonymous inner class
  2.    Interface
  3.    Inner class
  4.    All of the mentioned
Explanation:-
Answer: Option A. -> Anonymous inner class





Question 7. Which of these types cannot be used to initiate a generic type?
  1.    Integer class
  2.    Float Class
  3.    Primitive Types
  4.    Collections
Explanation:-
Answer: Option C. -> Primitive Types





Question 8. What is the default value of byte variable?
  1.    0
  2.    0.5
  3.    null
  4.    undefined
Explanation:-
Answer: Option A. -> 0





Question 9. Which of these is a correct way of defining of a generic method?
  1.    name(T1, T2, "¦, Tn) { /* "¦ */ }
  2.    public name { /* "¦ */ }
  3.    class name[T1, T2, "¦, Tn] { /* "¦ */ }
  4.    name{T1, T2, "¦, Tn} { /* "¦ */ }
Explanation:-
Answer: Option C. -> class name[T1, T2, "¦, Tn] { /* "¦ */ }





Question 10. Which of these instances cannot be created?
  1.    Integer Instance
  2.    Generic Class Instance
  3.    Generic Type Instance
  4.    Collection Instances
Explanation:-
Answer: Option C. -> Generic Type Instance