• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Question from KS&BB about Generic Methods?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Correct Answer is:
B
E
F
<E extends Number> is defined as type of Method! and the return type is <? super E> so, the output should be of type Number or Object, hoe it is Integer. And Similarly as argument of Type is defined as <E> it should be of Type Number.
So, i was thinking the answer is E. I am really Confused with this Generic typed Class and Methods.
KS&BB describes Thread as toughest Chapter, but i suppose its the toughest Chapter on Exam!
Thanks,
Shivey
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Shivey,

There is an error in that Question. The correct method declaration is
public static <E extends Number> List<E> process(List<E> nums)

Please look into K&B Errata if you come across any unusual things.
Hope this helps.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain why this is an error. I don't understand. It seems to be fine what is given in K&B book.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic