What happens when you use raw type List instead of List<String>?
Answer: D
Using raw types bypasses generic type checking, losing type safety. Elements are treated as Object at runtime, potentially causing ClassCastException when casting.
Q.742Hard
Which generic declaration allows a method to accept List of any type?
Answer: D
All three approaches allow processing lists of any type, though they differ: raw type (unsafe), unbounded wildcard (read-only), and type parameter (flexible).
Q.743Hard
What is the difference between <T extends Comparable<T>> and <T extends Comparable>?
Answer: B
<T extends Comparable<T>> is a recursive bound ensuring T implements Comparable of its own type. <T extends Comparable> uses raw type, losing type information.
Q.744Hard
Can you create an instance of generic type parameter directly like: T obj = new T();?
Answer: B
Due to type erasure, T is replaced with Object at runtime, and you cannot instantiate Object with new T(). You need reflection or pass Class<T> as parameter.
Q.745Hard
What is the PECS principle in generics?
Answer: A
PECS (Producer Extends, Consumer Super) is a mnemonic for wildcard bounds: use '? extends' when reading (producing), use '? super' when writing (consuming).
Advertisement
Q.746Hard
Which statement about generic constructors is TRUE?
Answer: D
Generic constructors can exist in non-generic classes. Constructor's type parameters are separate from and independent of the class's type parameters.
Q.747Medium
What is the output?
List<String> strings = new ArrayList<String>();
List list = strings;
list.add(123);
String str = strings.get(0);
System.out.println(str);
Answer: C
Adding Integer 123 to raw type reference bypasses generics. When retrieving as String, a ClassCastException occurs at runtime.
Q.748Medium
How would you define a generic method that works with a pair of objects and returns the first one?
Answer: D
Both A and C define valid generic methods with multiple type parameters. C is more explicit with 'public' modifier, but both are functionally equivalent.
Q.749Easy
What is the advantage of using generics over raw types in terms of 2024-25 Java standards?
Answer: D
Generics provide compile-time safety, code reusability through parameterization, and clearer API contracts. These are essential for modern Java development standards.
Q.750Easy
Which of the following is a valid generic class declaration in Java?
Answer: A
Valid generic class declarations allow multiple type parameters with upper bounds. Option A is syntactically correct. Option C uses invalid 'super' keyword in class declaration. Option D has malformed syntax.
Q.751Easy
What is the result of type erasure in Java generics?
Answer: B
Type erasure removes all generic type parameters during compilation. For unbounded types, they're replaced with Object; for bounded types, they're replaced with the upper bound.
Q.752Medium
Consider the following code snippet:
public <T> T processData(List<? extends T> list) { return list.get(0); }
What is the relationship between the wildcard and type parameter T?
Answer: B
The wildcard '? extends T' means the list can contain any type that is a subtype of T. This allows reading from the list safely but restricts writing.
Q.753Medium
Which of the following will cause a compile-time error?
Answer: B
Java does not allow covariance with generics. List<Integer> cannot be assigned to List<Number> because you could add a Double to the latter, breaking type safety.
Q.754Medium
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.755Medium
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.756Medium
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.757Hard
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.758Easy
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.
Q.759Medium
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.760Easy
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.