• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Study Gude Chap 7 Q16

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K & B, SCJP 5 - Errata
 
Jacky Zhang
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone help me with this?
my exam day is getting close ...
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please someone help me...
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic