With lower-bounded wildcard (? super Integer), you can add Integer but retrieval returns Object. Casting Object to Integer without proper type causes ClassCastException.
What is the difference between List and List in practical usage?
AThey are identical
BList can accept any type; List cannot accept anything
CList can add Objects; List can only add null
DList is deprecated in favor of List
Correct Answer:
C. List can add Objects; List can only add null
EXPLANATION
List<Object> accepts Object and its subclasses for adding. List<?> (unbounded wildcard) is more flexible for reading but restrictive for writing (only null).
What will happen when you compile and run this code?
java
List strings = Arrays.asList("a", "b", "c");
List raw = strings;
raw.add(123);
String s = strings.get(3);
ACompiles successfully and runs without error
BCompilation error due to raw type usage
CCompiles with warning, throws ClassCastException at runtime
DCompiles and throws ArrayIndexOutOfBoundsException
Correct Answer:
C. Compiles with warning, throws ClassCastException at runtime
EXPLANATION
Assigning generic type to raw type suppresses type checking. The Integer added bypasses the type system, causing ClassCastException when retrieved as String.
How would you correctly use generics in a recursive type bound scenario?
Aclass Node { Node next; }
Bclass Node { Node next; }
Cclass Node extends T { }
Dclass Node { }
Correct Answer:
A. class Node { Node next; }
EXPLANATION
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.
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.