*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B SJCP 5.0 Study Guide p. 634 - generics and collections 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 "K&B SJCP 5.0 Study Guide p. 634 - generics and collections question" Watch "K&B SJCP 5.0 Study Guide p. 634 - generics and collections question" New topic
Author

K&B SJCP 5.0 Study Guide p. 634 - generics and collections question

Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Given a method declared as:

public static <E extends Number> List<? super E> process(List<E> nums)

A programmer wants to use this method like this:

//INSERT DECLARATION HERE
output = process(input);

Which pairs of declaration could be placed at //INSERT DECLARATION HERE to allow the code to compile? (Choose all that apply.)

A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;

B. ArrayList<Integer> input = null;
List<Integer> output = null;

C. ArrayList<Integer> input = null;
List<Number> output = null;

D. List<Number> input = null;
ArrayList<Integer> output = null;

E. List<Number> input = null;
List<Number> output = null;

E. List<Integer> input = null;
List<Integer> output = null;

G. None of the above.


Try to figure out the answer...
.
.
.
.
.
.
.
Ok, according to the book correct answers are B,E and F.

My opinion is G. Code will not complie with any of given pairs. Why? Return type of the method is List<? super E>, so in my opinion regardless of what we really get from the method it cannot be assigned to any given in aswers references.


What we are getting from the method is a List of ANY type that is E or super of E. As we know polymorpshim applies only to the "base" type:

List<Dog> list = new ArrayList<Dog>(); // OK

Not the generic type:

ArrayList<Animal> list2 = new ArrayList<Dog>(); // WRONG

So, generic types must be the same both in reference declaration and when creating an object using new. In this case all references declarations given in answers are in my opinion wrong.



Let`s reffer to the correct answers:

B. ArrayList<Integer> input = null;
List<Integer> output = null;

Then what we can get as a return is a List that is a generic type Integer or ANY type that is super of Integer. So we can get a List that is a generic type Number as well. How it is possible then to assign it to List<Integer> reference if the generic types MUST be the same?

Next one:

E. List<Number> input = null;
List<Number> output = null;

We can get as a return is a List that is a generic type Number or ANY type that is super of Number. For example List<Object>, no way to assing it to List<Number>

E. List<Integer> input = null;
List<Integer> output = null;

Same reason as in B.


My opinion is that there are only two posibilities which are in fact the same:

List<?> output = null;
List<? extends Object> output = null;



Ofcourse I might be wrong and then would be very appreciated for claryfing the issue as my exam is coming closer Thank you.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This topic may help


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Thank you for link Barry. I was right Not that I`m happy that there`s a mistake in the question It just makes me feel better when thinking about passing the SCJP 5.0 exam.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B SJCP 5.0 Study Guide p. 634 - generics and collections question
 
Similar Threads
doubt in generics example from kathy sierra
Doubt related to use of wild card character ( ? super E)
Study Gude Chap 7 Q16 (errata applied)
doubt abt Generics mock question - from K&B
Study Gude Chap 7 Q16