Meaningless Drivel is fun!*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
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
 
Consider Paul's rocket mass heater.
 
subject: Generics, K&B Mock, what's the answer?
 
Similar Threads
Doubt
Generics
Solution needed for Generics Question
Doubt in K&B exam
Help me understand