Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

answer to this question

 
Mike Rudiger
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me what this answer is? I think its C but thats not what the answer is.

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;
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;
F. List<Integer> input = null;
List<Integer> output = null;
G. None of the above.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Joe Usenet", welcome to JavaRanch.

First, please check your private messages. You can see them by clicking My Private Messages.

If you copy a question from a book or mock exam then please quote your sources.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike

Just curious to know what is the answer. Is it "G. None of the above."? Also what makes you think that C should be the answer? Also could you please quote your source?
 
Mike Rudiger
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer is b, e, and f and it also says this:

"The return type of process is definitely declared as a List, not an ArrayList, so A and D
are wrong. C is wrong because the return type evaluates to List<Integer>, and that can't
be assigned to a variable of type List<Number>. Of course all these would probably cause a
NullPointerException since the variables are still null´┐Żbut the question only asked us
to get the code to compile."

It is from the Cathy Sierra
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer for this should be "None of the above" unless something is seriously wrong with my logic. The compiler(1.5.11) agrees with the discussion below:



Method Argument is of type List<E> where <E extends Number>. Hence the method argument can be a List of objects which extend number. For e.g. List<Float>, List<Double>, List<Integer> are all valid.

Method Return Type is List<? super E> where <E extends Number>. Hence the method return type is a list of objects which are superclass of an object which itself extends number. Hence return type is undefined due to ambiguity(it may be Number or some superclass of Number or Object etc. etc.)

Now moving to the options:

A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;
This is obviously wrong as return type of process is List which can never be assigned to an ArrayList without casting.

B. ArrayList<Integer> input = null;
List<Integer> output = null;
This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Integer>

C. ArrayList<Integer> input = null;
List<Number> output = null;
This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Number>

D. List<Number> input = null;
ArrayList<Integer> output = null;
This is obviously wrong as return type of process is List which can never be assigned to an ArrayList without casting.

E. List<Number> input = null;
List<Number> output = null;
This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Number>. Also the method argument is also wrong as it can be a List of objects which extend number but here it is List<Number>.

F. List<Integer> input = null;
List<Integer> output = null;
This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Integer>

G. None of the above.
Correct!!

Please comment if this resoning is correct/incorrect.

Regards
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satya,

Am pasting this from the K&B errata:

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



 
Siri Naray
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still I dont understand the output. return type List<E> so we can return only a List and E could be an Integer. But how can answer B be right. the method argument is a List and in option B we are sending ArrayList. Can we send subtype of a Collection as an argument??

I have exam tomorrow morning and I am banging my head with Generics.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the method argument is a List and in option B we are sending ArrayList. Can we send subtype of a Collection as an argument??


Yes of course, an ArrayList is always a List.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic