Govt Exams
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.
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.
public class Pair { }
Which declaration is invalid?
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'.
List list = Arrays.asList("a", "b", "c");
The compiler infers the most specific common type of the arguments. Since all arguments are String literals, T is inferred as String.
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.
List strings = new ArrayList();
List raw = strings;
raw.add(123);
String str = strings.get(0);
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.
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.
public static T findMax(T[] array)
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.
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.
public T processData(List
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.