Practice mode Java

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.