• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic, self test example

 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Haibo Jia
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What errata is there? Your method signature is same as in the book.
 
Chandramouli Ram
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic