wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics example, need help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics example, need help." Watch "Generics example, need help." New topic
Author

Generics example, need help.

Xyz Abc
Greenhorn

Joined: Aug 24, 2007
Posts: 27
Hi, I am posting this same question again because I still didn't get my doubt cleared. In this question, if B,E and F are correct then why is C not correct? Is it because both the input and output types should match in addition to the return type being an integer or a number?
I'm just thinking, if you feed in an ArrayList of integers according to answer C and get a List of numbers, it doesnt make sense since a number could be a Float too? Is that why C is incorrect?
Please help me out.
Thanks

This is a K & B book example, from chap:7, page620.




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.


Answer:
B, E, and F are correct.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Sue, please check your private messages. You can see them by clicking My Profile.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

"Sue M" - please have a look at your private messages again.
 
jQuery in Action, 2nd edition
 
subject: Generics example, need help.