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

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

Prepare for your Java exam with our ultimate quiz based on 'Thinking in Java'. Engage with expertly crafted questions that enhance your learning process. Perfect for Java enthusiasts looking to solidify their knowledge!

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.


What restriction does a List<? extends Fruit> impose?

  1. You cannot add any object to the List

  2. You can only add Fruit objects to the List

  3. You can add any subtype of Fruit to the List

  4. No restrictions, any object can be added to the List

The correct answer is: You cannot add any object to the List

A List<? extends Fruit> imposes the restriction that only objects of type Fruit or its subtypes can be added to the list. Option B and C suggest that only objects of type Fruit can be added to the list, but this is incorrect as subtypes of Fruit are also allowed. Option D states that there are no restrictions, but this is incorrect as the list can only contain objects of type Fruit or its subtypes. Option A is the correct answer as it accurately states that no objects can be added to the list, regardless of whether they are of type Fruit or its subtypes. This is because when using the wildcard ? extends Fruit, the compiler cannot determine the specific type of objects that can be added to the list, so it prevents any objects from being added to avoid potential type errors.