Govt. Exams
Entrance Exams
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.
Both A and C define valid generic methods with multiple type parameters. C is more explicit with 'public' modifier, but both are functionally equivalent.
List strings = new ArrayList();
List list = strings;
list.add(123);
String str = strings.get(0);
System.out.println(str);
Adding Integer 123 to raw type reference bypasses generics. When retrieving as String, a ClassCastException occurs at runtime.
Using raw types bypasses generic type checking, losing type safety. Elements are treated as Object at runtime, potentially causing ClassCastException when casting.
Option B directly specifies String, while Option C makes the class generic. Both are valid approaches to implement a generic interface.
Option D fails because List<Object> cannot reference ArrayList<String>. Generics are invariant. Options A, B, C are valid (covariance with extends, contravariance with super).
List<?> is an unbounded wildcard that represents a list of unknown type. You can read from it (get Object), but cannot write to it (except null). Both A, B, and C are correct interpretations.
The type parameter T cannot simultaneously be int and double. The compiler cannot infer a single type T that satisfies both arguments, resulting in a compilation error.
Multiple type parameters with bounds must be comma-separated and each can have its own upper bound. Option C shows correct syntax with recursive bound on T and bound on K.
'? super Integer' is a lower bounded wildcard that accepts Integer and all its superclasses like Number, Object. This allows write operations with Integer values.