Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Dive into the depths of Java with our quiz based on "Thinking in Java, Fourth Edition" by Bruce Eckel. Test your knowledge, solidify concepts, and prepare for certification with challenging questions and insightful feedback.

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


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

  1. Because the list is bound to Fruit objects only

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

  3. Because Apple is not a subtype of Fruit

  4. Because you can only add null to such a List

The correct answer is: 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.