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...
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 ]