Which of the following correctly declares a generic method that returns the first element of any collection?
Apublic T getFirst(Collection collection)
Bpublic T getFirst(Collection collection)
Cpublic T getFirst(Collection collection)
Dpublic Collection getFirst(T collection)
Correct Answer:
B. public T getFirst(Collection collection)
EXPLANATION
Generic methods require type parameter declaration before return type. The collection parameter must be of type Collection<T> to extract element of type T.
What will be the output of the following code?
java
List list = new ArrayList();
list.add("Java");
Object obj = list;
System.out.println(list.getClass() == obj.getClass());
Atrue
Bfalse
CCompilation error
DRuntime exception
Correct Answer:
A. true
EXPLANATION
Generic type information is erased at runtime. Both list and obj refer to the same ArrayList class, so getClass() returns the same class object.
How would you correctly use generics in a recursive type bound scenario?
Aclass Node { Node next; }
Bclass Node { Node next; }
Cclass Node extends T { }
Dclass Node { }
Correct Answer:
A. class Node { Node next; }
EXPLANATION
Option A demonstrates recursive type bound (also called F-bounded polymorphism), which allows a type to reference itself in its bound. This is commonly used in builder patterns and comparators.
In the context of generics, what does 'invariance' mean?
AA generic type List is a subtype of List
BA generic type List is not a subtype of List, despite Integer being a subtype of Number
CInvariance allows covariance and contravariance together
DInvariance means the type cannot change
Correct Answer:
B. A generic type List is not a subtype of List, despite Integer being a subtype of Number
EXPLANATION
Generics are invariant in Java. List<Integer> is NOT a subtype of List<Number> to maintain type safety. This is a key feature preventing runtime errors.
What is the relationship between generics and arrays in Java?
AGeneric arrays can be created freely like List[]
BGeneric arrays are not supported; you cannot create arrays of parameterized types
CGeneric arrays work only with unbounded wildcards
DArrays of generics work only for primitive types
Correct Answer:
B. Generic arrays are not supported; you cannot create arrays of parameterized types
EXPLANATION
Java does not allow creation of arrays with parameterized types due to type erasure. List<String>[] would be unsafe. You must use List<String>[] or List[] instead.
Only option B allows adding elements safely. '? super Integer' means the collection can hold Integer or any supertype. Option A prevents adding, Option C prevents adding, Option D restricts to exactly Number.
What happens when you try to cast a generic object?
List strings = (List) getSomeList();
AThe cast is fully checked at compile time
BThe cast is unchecked; generic type information is lost due to erasure
CThe cast always succeeds because of type erasure
DA compile-time error is raised
Correct Answer:
B. The cast is unchecked; generic type information is lost due to erasure
EXPLANATION
Due to type erasure, casts involving generic types are unchecked. The compiler warns about this but allows it. The actual cast check only verifies the raw type.
Given the declaration: List listOfLists;
Which assignment is valid?
AlistOfLists = new ArrayList();
BlistOfLists = new ArrayList
ClistOfLists = new ArrayList();
DlistOfLists = new ArrayList();
Correct Answer:
D. listOfLists = new ArrayList();
EXPLANATION
Nested generics must match exactly or use appropriate wildcards. Only option D provides exact type match. Option B would work with wildcards, but that's not listed correctly here.