File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Study Gude Chap 7 Q16

 
Jacky Zhang
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K & B, SCJP 5 - Errata
 
Jacky Zhang
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone help me with this?
my exam day is getting close ...
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please someone help me...
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic