File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes S&B 1.5, Chapter 7, Question 2, answer C Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "S&B 1.5, Chapter 7, Question 2, answer C" Watch "S&B 1.5, Chapter 7, Question 2, answer C" New topic

S&B 1.5, Chapter 7, Question 2, answer C

David Wooddall-Gainey

Joined: Dec 13, 2007
Posts: 16

which can be inserted

A.List<List<Integer>> table = new List<List<Integer>>();
B.List<List<Integer>> table = new List<ArrayList<Integer>>();
C.List<List<Integer>> table = new ArrayList<ArrayList<Integer>>();
D.List<List,Integer>> table = new List<List,Integer>>();
E.List<List,Integer>> table = new ArrayList<List,Integer>>();
F.List<List,Integer>> table = new ArrayList<ArrayList,Integer>>();

Why is c wrong? Given explanation "the type argument <List<Integer>> must be the same on both sides of the assignment even though the constructor new ArrayList() on the right side is a subtype of the declared type List on the left side." Why does the type arg have to be the same?

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
C. List<List<Integer>> table = new ArrayList<ArrayList<Integer>>();

Genrics doesn't allow you to write subclasses like that - meaning
you can't write :
List<Animal> l=new ArrayList<Dog>();

it should be
List<Dog> l=new ArrayList<Dog>();
List<Animal> l=new ArrayList<Animal>();

Both the types should be the same.

You cant even write
List<Number> n=new ArrayList<Integer>();

Hope you got the picture....

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Ankit Garg

Joined: Aug 03, 2008
Posts: 9462

So does that mean only A is the correct answer right???

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Manju Kavi
Ranch Hand

Joined: Sep 25, 2008
Posts: 33
Yes Nabila is right. But here none of the option is right. Option "A" and "B" are wrong because List is an interface and you cannot call constructor on it. "C" is wrong according to what Nabila said, which is correct.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
If C was modified a little as
List<List<Integer>> table = new ArrayList<List<Integer>>();
it would have been correct.
David Wooddall-Gainey

Joined: Dec 13, 2007
Posts: 16
Thanks and apologies to all. The correct answer is B which SHOULD read

(I am tying while riding on a bus to work!)

Nabila, do you mean since the left side of the assignment contains
List<List<Integer>> the base class is List while the generic class is List<Integer>. Base and Generic are terms from S&B Chap 7. If this is the case then, yes I understand you explanation. And thank you very much.
I agree. Here's the link:
subject: S&B 1.5, Chapter 7, Question 2, answer C
It's not a secret anymore!