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

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

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


How do you assign a List<Apple> to a List<? super Apple> variable?

  1. Using direct assignment without any casts

  2. By casting List<Apple> to List<? super Apple>

  3. Using generic wildcard as method parameter

  4. It's not possible to assign List<Apple> to List<? super Apple>

The correct answer is: Using generic wildcard as method parameter

The question is asking how to assign a List of type Apple to a List that can contain either Apple or one of its superclasses (i.e. a List of type Fruit, for example). Option A and D are incorrect because direct assignment and casting are not involved here. Option B is incorrect because you do not need to cast the List of Apple to assign it to a List of wildcard type. Option C is the correct answer because using a generic wildcard as a method parameter allows us to specify that the List can contain either an Apple or one of its superclasses. This makes it possible to assign a List of Apple objects to a List of any supertype of Apple.