What does the following generic method signature indicate?
public static <T extends Comparable<T>> T findMax(T[] array)
Answer: A
The bounded type parameter 'T extends Comparable<T>' enforces that T must implement Comparable and be comparable with its own type. This is the F-bounded polymorphism pattern.
Q.62Medium
Given: Map<String, ? extends List<?>> map = new HashMap<>();
Which operation is valid?
Answer: C
We can read from map.get() and assign to List<?>, but cannot add to List<?> or put into the map due to wildcard restrictions. Option C only reads the value.
Q.63Medium
What is the output of this code?
List<String> strings = new ArrayList<>();
List raw = strings;
raw.add(123);
String str = strings.get(0);
Answer: C
The code compiles (with warnings about raw types) but throws ClassCastException at runtime when trying to cast Integer to String. Type safety is lost with raw types.
Q.64Hard
Which statement correctly implements a generic factory method?
Answer: B
Cannot instantiate generic type T directly. Using Class<T>.newInstance() is a valid pattern (though deprecated in newer Java versions in favor of getConstructor()). Option A is invalid.
Q.65Easy
What is the compiler-inferred type parameter in this call?
List<String> list = Arrays.asList("a", "b", "c");
Answer: B
The compiler infers the most specific common type of the arguments. Since all arguments are String literals, T is inferred as String.
Advertisement
Q.66Medium
Consider:
public class Pair<T, U> { }
Which declaration is invalid?
Answer: D
Option D is invalid because we cannot instantiate a generic class with wildcard type parameters. Wildcards are only for variable declarations, not for instantiation with 'new'.
Q.67Easy
What does the wildcard in List<?> represent?
Answer: B
List<?> represents unknown type with read-only capabilities. Elements can only be read as Object. You cannot add elements (except null) due to type safety.
Q.68Hard
Which generic wildcard usage follows the consumer pattern correctly?
Answer: B
Consumer pattern uses '? super T' (lower bound). This allows adding elements safely. Option A (producer pattern) only allows reading, and Options C and D restrict type flexibility.
Q.69Hard
What is the compiled signature of this generic method?
public <T extends Number & Comparable<T>> void sort(T[] array)
Answer: D
With multiple bounds using '&', type erasure replaces T with the first bound. Here, Number is the first bound, so T is replaced with Number.
Q.70Medium
What is the correct way to declare a generic method that accepts a list and returns the maximum element?
Answer: D
Option D correctly ensures T is comparable with itself using F-bounded polymorphism. Option C has inverted logic with '? super'. Option B is unnecessarily complex.
Q.71Medium
Given the declaration: List<List<String>> listOfLists;
Which assignment is valid?
Answer: D
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.
Q.72Medium
What happens when you try to cast a generic object?
List<String> strings = (List<String>) getSomeList();
Answer: B
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.
Q.73Hard
Which of the following represents valid bounded wildcard usage for a method that processes collections?
Answer: B
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.
Q.74Easy
What is the relationship between generics and arrays in Java?
Answer: B
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.
Q.75Medium
In the context of generics, what does 'invariance' mean?
Answer: B
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.
Q.76Hard
How would you correctly use generics in a recursive type bound scenario?
Answer: A
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.
Q.77Easy
What will be the output of the following code?
java
List<String> list = new ArrayList<String>();
list.add("Java");
Object obj = list;
System.out.println(list.getClass() == obj.getClass());
Answer: A
Generic type information is erased at runtime. Both list and obj refer to the same ArrayList class, so getClass() returns the same class object.
Q.78Easy
Which of the following correctly declares a generic method that returns the first element of any collection?
Answer: B
Generic methods require type parameter declaration before return type. The collection parameter must be of type Collection<T> to extract element of type T.
Q.79Easy
Which declaration is valid in Java Generics?
Answer: B
Generics only work with reference types, not primitive types. Integer is the wrapper class for int, making option B valid.
Q.80Medium
What does the following generic declaration mean?
public <T extends Number & Comparable<T>> T findMax(T a, T b)
Answer: B
Multiple bounds in generics use '&' to specify that the type must satisfy all constraints. T must extend Number AND implement Comparable<T>.