File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Murach's Java Servlets and JSP this week in the Servlets 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 agree. Here's the link:
subject: Generics K&B Mock exam question
Similar Threads
How to interpret Generics using wildcard and type parameters
Solution needed for Generics Question
Doubt in K&B exam
generics question