Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generic, self test example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generic, self test example" Watch "generic, self test example" New topic
Author

generic, self test example

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
This is a K & B book example, from chap:7, page620.
I am not getting i anyways? Can anybody please explain?
I appreciate your help.



Given a method declared as:
public static <E extends Number> List<? super E> process(List<E> nums)

A programmer wants to use this method like this:
// INSERT DECLARATIONS HERE
output = process(input);

Which pairs 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.


Answer:
B, E, and F are correct.


SCJP-1.5<br />SCWCD-1.4
Haibo Jia
Greenhorn

Joined: Aug 22, 2007
Posts: 6
Hi,

The answer K&R book provided also make me confused. I make following test code

All the right anwser he gives can't be compiled. Anyone can give an explanation? Thanks.
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi dolly,

hava a look at the errata
The correct definition for process is:
public static <E extends Number> List<E> process(List<E> nums)
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
What errata is there? Your method signature is same as in the book.
Chandramouli Ram
Ranch Hand

Joined: Mar 07, 2005
Posts: 65
What's in the book is:

public static <E extends Number> List<? super E> process(List<E> nums)


The corrected version is:

public static <E extends Number> List<E> process(List<E> nums)

Only with the corrected version, B, E & F are correct.


Thanks & Regards,<br />Chandramouli Ram
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generic, self test example
 
Similar Threads
SCJP Chapter 7, Question #16
Generics Question
SCJP 5 K&B question problem
doubt abt Generics mock question - from K&B
answer to this question