Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on a K & B SCJP5 guide question: GENERICS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on a K & B SCJP5 guide question: GENERICS" Watch "Doubt on a K & B SCJP5 guide question: GENERICS" New topic
Author

Doubt on a K & B SCJP5 guide question: GENERICS

Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Dear All,

The following is a question from K & B SCJP5.0 guide:

Given a method declared as:

A programmer wants to use this method like this:

Which pair of declarations could be placed at // INSERT DECLARATIONS HERE to allow the code to compile? (Choose all that apply.)

A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;

B. ArrayList<Integer> input = null;
List<Integer> output = null;

C. ArrayList<Integer> input = null;
List<Number> output = null;

D. List<Number> input = null;
ArrayList<Integer> output = null;

E. List<Number> input = null;
List<Number> output = null;

F. List<Integer> input = null;
List<Integer> output = null;

G. None of the above.

Correct Answers given are: B, E, F

But I thought the answer would be G because the method returns a List<? super E> where E is the element type of the List which was passed as argument to the method process & hence if:
'input' is List<Integer> then 'output' should be a List<? super Integer>.
Someone please help me with this question I think I got confused somewhere; but dont understand where (
Thanks in advance!


SCJP, SCWCD, SCBCD
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
HI,

There is a little typo in your declaration its supposed to be:


I am having the same confusion about this question. The correct answer is B,E,F and the explanation in the book states:
"The return type is definitely declared as List, NOT ArrayList so A,D are wrong. ......"

This is what I don't get...why it is that the return type MUST be List only and not ArrayList?? Just like the
argument can be ArrayList then why cant return type also be arrayList?
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
Actually this is interesting , I found out there is a mistake in the question specified in teh K&B errata link which
is present once we are in the SCJP forum, the below is what it mentions:

620....bug.......Q-16: method declaration s/b:
& 634
public static <E extends Number> List<E> process(List<E> nums)

In my version of the book the method is declared as I have specified in my previous post...can someone
please let me know what the errata is?? There appears to be a mistake in the question itself. Please
help SCJP is complicated enough as it is, we don't need errors in questions to make matter worse
Alexander Danilou
Greenhorn

Joined: May 08, 2009
Posts: 28
correct declaration is public static <E extends Number> List <E> process(List <E> nums)
BEF will compile
with ... List <? extends E>... nothing will compile

to Meghna:
you can return ArrayList if return type is List, but in this case you need cast it to (ArrayList) before you can assign it to variable of type ArrayList, but output = process(input); doesn't provide any casting, so it will not compile in this case.
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Alexander Danilou wrote:correct declaration is public static <E extends Number> List <E> process(List <E> nums)
BEF will compile
with ... List <? extends E>... nothing will compile


The book I have, has the question as I stated initially: public static <E extends Number> List<? super E> process(List<E> nums)

So in this case, the answer should be G. None of the above; Am I correct?
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

Meghna Bhardwaj wrote:HI,

There is a little typo in your declaration its supposed to be:




Hi Meghna, I have corrected my initial post; it was typing mistake the question in my version of the book has the method process return List<? super E> not List<E>; but I think the answer given (B E & F) are for List<E>
 
jQuery in Action, 2nd edition
 
subject: Doubt on a K & B SCJP5 guide question: GENERICS