A method takes a List with a lower-bounded wildcard and writes into it. What does this print?
```java
import java.util.*;
public class Main {
static void fill(List<? super Integer> sink) {
sink.add(1);
sink.add(2);
}
public static void main(String[] args) {
List<Number> nums = new ArrayList<>();
fill(nums);
Object first = nums.get(0);
System.out.println(nums + " " + first);
}
}
```
A. [1, 2] 1Correct answer
? super Integer is the consumer side of PECS, so adding an Integer is always legal; the writes mutate the caller's nums, and reading the boxed Integer 1 back and printing it invokes Integer's toString, giving 1.
B. Compilation fails because add(...) cannot be called on a List<? super Integer>
Applies the ? extends rule to a ? super wildcard. It is the producer form, List<? extends Number>, that refuses add; lower-bounded lists accept writes, which is their whole purpose.
C. [1, 2] java.lang.Integer@1b6d3586
Assumes that declaring the variable as Object makes println call Object.toString. The declared type selects the method signature, but the dynamic type selects the implementation; the object is an Integer, which overrides toString, so 1 prints.
D. Compilation fails because List<Number> is not a subtype of List<? super Integer>
Reads the wildcard backwards. List<? super Integer> accepts List<Integer>, List<Number>, List<Serializable> and List<Object>, so List<Number> is a legal argument.
Explanation
Trace: `? super Integer` is the CONSUMER side of PECS — whatever the wildcard captures, it is some supertype of Integer, so an Integer is always a legal element to add. `sink.add(1)` and `sink.add(2)` compile and mutate the caller's list, which is the same object as `nums`. `List<Number>` matches `List<? super Integer>` because Number is a supertype of Integer. Reading back, the only thing the compiler can promise about an element of a `? super Integer` list is that it is an Object, but here we read through `nums`, typed `List<Number>`, and assign to an Object anyway. `println` on the list gives [1, 2], and `first` holds the boxed Integer 1, whose toString gives 1.
Why the others are wrong:
`Compilation fails because add(...) cannot be called...` applies the `? extends` rule to a `? super` wildcard. It is the PRODUCER form, `List<? extends Number>`, that refuses add — the compiler cannot know which subtype the list actually holds. Lower-bounded lists accept writes; that is the whole point of them.
`Compilation fails because List<Number> is not a subtype...` reads the wildcard backwards. `List<? super Integer>` accepts `List<Integer>`, `List<Number>`, `List<Serializable>` and `List<Object>` — every list whose element type is Integer or above.
`[1, 2] java.lang.Integer@1b6d3586` assumes that declaring the variable as Object makes `println` call `Object.toString()`. The declared type selects the method signature, but the DYNAMIC type selects the implementation: the object is an Integer and Integer overrides toString, so 1 is printed.
Exam tip: PECS — Producer Extends, Consumer Super. `? extends T` lets you read a T but not write (except null); `? super T` lets you write a T but only read back an Object. The reverse trap of this question is a method declared `List<? extends Number>` whose body calls add — that one really does fail to compile.