• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Doubt in Generics Q16 Self Test Book:Kathy Sierra

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

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:
Correct: 3 B, E, and F are correct.
Wrong: 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.


I tried to make a simple program for testing above but I found something different. Can anyone tell me that if I am making a mistake in my program to test above example? And how to write a program so that above results are true ?
My Program:

import java.util.*;
public class NitGenericsTest2
{

public static <E extends Number> List<? super E> process(List<E> nums)
{
List <Number> a=null;
return a;
}

public static void main(String args[])
{
System.out.println("Hi Nitin Inside main");
//ArrayList<Integer> input = null; // Option A
//ArrayList<Integer> output = null; // Option A

//ArrayList<Integer> input = null; // option B
//List<Integer> output = null;// option B

//ArrayList<Integer> input = null;// option C
//List<Number> output = null;// option C

//List<Number> input = null;// option D
//ArrayList<Integer> output = null;// option D

List<Number> input = null; // option E
List<Number> output = null; // option E

//List<Integer> input = null;// option F
//List<Integer> output = null;// option F

//List<Number> input = null;// This works
//List<? super Number> output = null;// This works


output = process(input);
}

}

As Options B, E and F are correct so I've used option E as my test case in above program. But I got the following compilation error:
can't convert from List<capture-of ? super Number> to List<Number>

I believe that since option E is: List<Number> output = null; but return type of method process() is List<? super Number> so the given compilation error is coming.
If I change List<Number>output to List<? super Number>output then no compilation error.

Is Q 16 correct or are the answers B,E,F correct? Should the answer for Q16 not be G?

Thanks & Regards,
Nitin
[ August 14, 2007: Message edited by: Nitin Bhardwaj ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this question has been discussed in the forum just search the forum for this topic you will find. I think there is a mistake in question.
 
Ameen khan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/264343/java-programmer-SCJP/certification/SCJP-Generics-questions-form-SCJP
 
Nitin Bhardwaj
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ameen !!
Regards,
Nitin
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic