aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Study Gude Chap 7  Q16 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Study Gude Chap 7  Q16" Watch "Study Gude Chap 7  Q16" New topic
Author

Study Gude Chap 7 Q16

Jacky Zhang
Greenhorn

Joined: Sep 17, 2006
Posts: 18
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 DECLARATIONS HERE
output = process(input);

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

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

my comments: E replaced by Integer and ? replaced by Integer,
which meets the return collection type bound <Integer super Integer>
and the generic declaration <Integer extend Number>.
ArrayList<Integer> should be campatible with List<Integer>
so this is a correct answer.


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

my comments: same reasoning as A, this is a correct answer.


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

my comments: E replaced by Integer and ? replaced by Number,
which meets the return collection type bound <Number super Integer>
and the generic declaration <Integer extends Number>.
so this is a correct answer.

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

my comments: E replaced by Number and ? replaced by Integer,
which violates because Integer is not a super of Number.


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

my comments: E replaced by Number and ? replaced by Number,
which meets the return collection type bound <Number super Number>
and the generic declaration <Number extends Number>.
so this is a correct answer.


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

my comments: E replaced by Integer and ? replaced by Integer,
which meets the return collection type bound <Integer super Integer>
and the generic declaration <Integer extends Number>.
so this is a correct answer.

So my answer would be: ABCEF.

Is this correct?
[ September 18, 2006: Message edited by: Jacky Zhang ]
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
K & B, SCJP 5 - Errata
Jacky Zhang
Greenhorn

Joined: Sep 17, 2006
Posts: 18
can we just take it as it is now and what would the answer be?
I ll post up an errata applied edition too.
Jacky Zhang
Greenhorn

Joined: Sep 17, 2006
Posts: 18
can anyone help me with this?
my exam day is getting close ...
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178

Q-16: method declaration s/b:
public static <E extends Number> List<E> process(List<E> nums)

for the above method declaration:
No. since the output is ArrayList while it s/b List.
Yes
No, since the output is Number while it s/b something that extends Integer.
No, same reason as A
Yes, <Number extends Number> for both input and output.
Yes
[ September 19, 2006: Message edited by: M Krishnan ]
Jacky Zhang
Greenhorn

Joined: Sep 17, 2006
Posts: 18
what does s/b stand for?
I really don't understand why option A is wrong.

as normally we can declare:

List<String> list=new ArrayList<String>();

so why when the return type is List<String>,
we can not return a ArrayList<String>?
Jacky Zhang
Greenhorn

Joined: Sep 17, 2006
Posts: 18
please someone help me...
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
s/b --> "should be"


Why option A is wrong?

The method returns 'List' and not 'ArrayList'.


Try placing the option A above the line 'output = process(input)' (as given above). You're actually assigning 'output' (which is of type ArrayList) with the value returned from the method (which is of type List ).

You can assign an ArrayList to a List and not the other way around. (ArrayList 'is a' List but not the other way ).
[ September 20, 2006: Message edited by: M Krishnan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Study Gude Chap 7 Q16