This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gabriel Ozeas
Ranch Hand
Posts: 30
Google App Engine Hibernate jQuery
  • Mark post as helpful
  • send pies
  • 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...
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic