posted 16 years ago
I feel multiple answers are correct here.
A. The type List<Animal> is assignable to List.
List rawList = null;
List<Animal> animalList = null;
rawList = animalList;
This works fine because you are assigning a list that contains only Animal objects to a raw list that can contain anything.
This is legal , because JDK 1.5 supports assiging a generic list to a raw list.
A- Correct
B. The type List<Bear> is assignable to List<Animal>.
This is wrong, a list of type List<Bear> can be assigned only to lists of type List<Bear> or List<?> [Assign me anything am happy] or List<? extends Bear> [Assing me a list that can contain Bear or anything that extends Bear]
B- InCorrect
C. The type List<Object> is assignable to List<?>.
Since List<?> will accept any type of list this is legal.
C- Correct
D. The type List<D> is assignable to List<? extends Bear>.
List<? extends Bear>: A list that can contain Bear or anything that extends Bear. Since D extends Bear List<D> can be assigned to a reference of type List<? extends Bear>
D- Correct
E. The type List<? extends Animal> is assignable to List<Animal>.
List<Animal>: Can be assinged List<Animal> only. Now List<? extends Animal> means that this list can contain objects of type Animal or anything that extends Animal which can be Dog, Cat and so on. And hence this list cannot be assigned to a List of type List<Animal>.
E- InCorrect
F. The type List<Object> is assignable to any List reference.
If any list reference refers to a raw list then this statement is correct but otherwise its not correct and hence this answer is wrong.
F- InCorrect
G. The type List<? extends Bear> is assignable to List<? extends Animal>.
List<? extends Animal> = List<? extends Bear> ???
Here <? extends Animal> means that this list can contain objects of type Animal or anything that extends Animal which can be Bear , Cow or D.
[Bear, Cow, D]
Now List<? extends Bear> means that this list can contain Bear or D since D extends D.
[Bear, D]
Since [Bear, D] is a subset of [Bear, Cow, D], This legal.
G- Correct
Hence correct answer is
A- Correct
C- Correct
D- Correct
G- Correct
[ January 11, 2008: Message edited by: Deepak Jain ]