*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Questions about Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Questions about Generics" Watch "Questions about Generics" New topic
Author

Questions about Generics

Engin OKUCU
Greenhorn

Joined: Jun 29, 2007
Posts: 13
I stumbled upon a question(in McGraw-Hill/Osborne - Katty Sierra).
Chapter 7 , Questions 16.

I did a copy and paste but it does not compiles and the worst, the given anwser seems not to be correct.
Can you help me ? Thanks.


16. 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.


Here is what i did :

public class Test {

public static <E extends Number> List<? super E> process(List<E> nums){

return nums;
}

public static void main(String[] args) {
// INSERT DECLARATIONS HERE
ArrayList<Integer> input1 = new ArrayList<Integer>();
ArrayList List<Integer> output = process(input1);

ArrayList<Integer> input2 = new ArrayList<Integer>();
List<? super Number> output2 = process(input2);

ArrayList<Integer> input3 = new ArrayList<Integer>();
List<? super Number> output3 = process(input3);

List<Number> input4 = null;
ArrayList<Integer> output4 = process(input4);

List<Number> input5 = null;
List<Number> output5= process(input5);

List<Integer> input6 = null;
List<Integer> output6 = process(input6);
}


}
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Looks like someone else had already posted a similar question here.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Check K&B Errata, the correct method signature is:
public static <E extends Number> List<E> process(List<E> nums)
Engin OKUCU
Greenhorn

Joined: Jun 29, 2007
Posts: 13
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions about Generics
 
Similar Threads
Study guide 5 Self Test error
Generics Problem
Generic problem
Generics Question
doubt abt Generics mock question - from K&B