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

Generics, K&B Mock, what's the answer?

Ranch Hand
Posts: 30
Hibernate jQuery Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I was doing the K&M mock and i found this question

And the following options:

A - public static <T> List<T> backwards(List<T> input)
B - public static <T> List<T> backwards(List<? extends T> input)
C - public static <T> List<T> backwards(List<? super T> input)
D - public static <T> List<? extends T> backwards(List<T> input)
E - public static <T> List<? super t> backwards(List<T> input)
F - public static <? extends T> List<T> backwards(List<T> input)
G - public static <? super T> List<T> backwards(List<T> input)

And the mock says that the correct answers that inserted, COMPILE and RUN without error are A, B, D and E... can someone explain me why it says that B compile and why C doesn't compile ?
Thanks everybody...
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey this is a little tricky question ... i liked it
consider T as Integer & Number class satisfies the condition <? super Integer>

Now assume you are passing List<Number> to backwards method defined as in option C

the argument to the method is satisfied & also you are allowed to add objects to input variable (i think you know the reason being you are creating a new List inside the method)
coming to the main point you are declaring a new List of type Integer & returning it. but in the for loop you are declaring variable "t" to be of Integer but input variable will return Number class objects as you invoked the method with List<Number>.

But this is not the case in option a,b,d OR e. try evaluating it the way i did. If you did not understand any other options do let me know.( by private message)
[ August 09, 2008: Message edited by: Milan Sutaria ]
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, the permaculture playing cards
    Bookmark Topic Watch Topic
  • New Topic