Given List<? extends Number> x, which statement compiles?
Correct answer B. Number n = x.get(0);
Explanation
The wildcard says x holds some unknown subtype of Number, perhaps Integer or Double. Reading is safe because whatever comes out is a Number. Adding is rejected, as the compiler cannot prove the value fits the unknown element type, so only null may be added. This is the producer side of the PECS guideline.
Answer checked How we write and check questions
Discussion