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 is required to add an Apple object to a List in the example provided?

  1. The List must be of type Apple

  2. The List must use a wildcard extending Apple

  3. The List must be of type Fruit

  4. Any of the listed types can be used

The correct answer is: The List must use a wildcard extending Apple

To add an Apple object to a List, the List must use a wildcard extending Apple. This is because Lists are type-specific and can only contain objects of the specified type or its subclasses. Option A is incorrect because it limits the List to only contain objects of type Apple, but not its subclasses. Option C is incorrect because it would not allow for other types of Fruit besides Apple to be added to the List. Option D is incorrect because it does not specify the type, which would result in a compilation error. Therefore, the only correct option is B, as it allows for any subclass of Apple to be added to the List.