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?
K
N
T
V
Explanation:-
Answer: Option B. -> N
Question 2. Which of the following is Faster, StringBuilder or StringBuffer?
StringBuilder
StringBuffer
Both of the above
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?
Type Interface
Interface
Inner class
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?
Generics provide type safety by shifting more type checking responsibilities to the compiler
Generics and parameterized types eliminate the need for down casts when using Java Collections
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
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?
Generics make code more fast
Generics make code more optimised and readable
Generics add stability to your code by making more of your bugs detectable at compile time
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?
Anonymous inner class
Interface
Inner class
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?
Integer class
Float Class
Primitive Types
Collections
Explanation:-
Answer: Option C. -> Primitive Types
Question 8. What is the default value of byte variable?
0
0.5
null
undefined
Explanation:-
Answer: Option A. -> 0
Question 9. Which of these is a correct way of defining of a generic method?
name(T1, T2, "¦, Tn) { /* "¦ */ }
public name { /* "¦ */ }
class name[T1, T2, "¦, Tn] { /* "¦ */ }
name{T1, T2, "¦, Tn} { /* "¦ */ }
Explanation:-
Answer: Option C. -> class name[T1, T2, "¦, Tn] { /* "¦ */ }
Question 10. Which of these instances cannot be created?