• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static <E extends Number> List<? super E> addAll(List<E> nu)
{
return null;
}
// insert code here line 4
output =addAll(input);

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;



answers is B,E and F and i agree , but can you explain why C is not applicable.
Incase of C , we are expecting return type List with object of type Integer or any of it's supertypes ( which means Number is elgible )
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rule here is :

Type of the variable declarion must match the type you pass to the actual object type.

ie if you declare List<Integer> i then whatever you assign to the reference "i" must be List<Integer> but not supertype of <Integer> or not subtype <Integer>

only List<Integer> i=new ArrayList<Integer> is correct
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Few more questions on generics :

Generics Mock Questions - SCJP 5.0
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, none of these options compile. Did you try them?

For the correction to the question see the K & B 5.0 Errata link at the top of this forum.

Next time please mention which mock exam or book the question is to be found.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic