It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics, K&B Mock, what's the answer? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Generics, K&B Mock, what Watch "Generics, K&B Mock, what New topic
Author

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

Gabriel Ozeas
Ranch Hand

Joined: Jul 04, 2008
Posts: 30

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

Joined: Jul 10, 2008
Posts: 118
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 ]

SCJP 6 83%
Working on SCWCD/OCPJWCD
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Generics, K&B Mock, what's the answer?
 
Similar Threads
Help me understand
Generics
Doubt
Doubt in K&B exam
Solution needed for Generics Question