Mastering Java: The Ultimate Quiz for 'Thinking in Java

Image Description

Question: 1 / 510

Why does List<? extends Fruit> not accept new Apple() when calling add()?

Because the list is bound to Fruit objects only

Because the compiler cannot determine what specific subtype of Fruit should be accepted

List<? extends Fruit> means that the elements in the list can be any subtype of Fruit. When calling add(), the compiler cannot determine which specific subtype is expected in the list, so it will not allow adding a new Apple object. Option A is incorrect because the list is not limited to only Fruit objects, but rather any subtype of Fruit. Option C is incorrect because Apple is a subtype of Fruit. Option D is incorrect because any non-null object can be added to the list, not just null.

Get further explanation with Examzify DeepDiveBeta

Because Apple is not a subtype of Fruit

Because you can only add null to such a List

Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy