This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics K&B Mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics K&B Mock exam question" Watch "Generics K&B Mock exam question" New topic

Generics K&B Mock exam question

megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Hi all,

I am a bit confused about the answer to the following question:

import java.util.*;
public class BackLister
List<T> output = new LinkedList<T>();
for (T t : input) output.add(0, t);
return output;


Which of the following can be inserted at // INSERT HERE to compile and run

Correct Answer:
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<? extends T> backwards(List<T> input)
D. public static <T> List<? super T> backwards(List<T> input)

Now in C and D , the return type as <? extends T> and <? super T> both is valid. I am confused as to how both are possible for the output variable which is of type T . I know both <? extends T> and <? super T> include the type T , thats fine, but when the method returns output it is of type <T> and it cannot be converted to a subtype of <T>, I guess so how does <?extends T> become a valid choice?

Thanks in advance for helping on this issue...
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Sorry , I guess its late so my mind was not working. I found the answer.

The method returns <T> so declaring its return type as <? extends T> or <? super T> is valid as both include <T>.

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Generics K&B Mock exam question
It's not a secret anymore!