Govt Exams
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.
List list = new ArrayList();
list.add(10);
Object obj = list.get(0);
System.out.println(obj.getClass().getName());
Although obj is declared as Object, it holds an Integer instance. getClass() returns the actual runtime type, which is Integer.
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.
This causes a compilation error due to type mismatch. List<Integer> cannot hold ArrayList<String>. Generics do not support implicit type conversion.
'? super Integer' is a lower bounded wildcard that accepts Integer and all its superclasses like Number, Object. This allows write operations with Integer values.
Bounded type parameters like <T extends Number> restrict the types that can be passed as type arguments, ensuring type safety and enabling access to specific methods.