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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Sujatha Musunuri
Greenhorn

Joined: Oct 23, 2007
Posts: 16
11. public void addStrings(List list) {
12. list.add(�foo�);
13. list.add(�bar�);
14. }

What must you change in this method to compile without warnings?

A. add this code after line 11:
list = (List<String> list;
B. change lines 12 and 13 to:
list.add<String>(�foo�);
list.add<String>(�bar�);
C. change the method signature on line 11 to:
public void addStrings(List<? extends String> list) {
D. change the method signature on line 11 to:
public void addStrings(List<? super String> list) {
E. No changes are necessary. This method compiles without warnings


The Ans is D. But I think the answer is E. please correct me if I'm wrong.

-Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39537
    
  27
Please QuoteYourSources.

I'll move this to the SCJP forum, as this forum is for certification results.


Ping & DNS - updated with new look and Ping home screen widget
Tomek Kaczmarek
Greenhorn

Joined: Nov 02, 2007
Posts: 4
I think that response depends on type of list declared to use in function
public void addStrings(List list)

When list will be declared for example as:
List<String> list = new ArrayList<String>();
The only way to change it in function is to use:
public void addStrings(List<? super String> list) {


-Tomek
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
sandeep kapse
Greenhorn

Joined: Jun 07, 2007
Posts: 25
Hi Sujatha,

I think 'D' is the correct option to choose as Tomek said.

Because, by changing the method signature as per option D,
you are telling compiler that the method can take anything that is
super type of String class. And since you are manipulating the list thats the only way. Then this code will compile without any compilation error.
[ November 09, 2007: Message edited by: Sandeep Misal ]

[ SCJP6, SCWCD5 ]
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 396

It's just a badly worded question D) works but E) is the simplest.

The issue it says what 'must you change' to which the answer is you don't have to change anything, I guess.

Bad question in my opinion.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics
 
Similar Threads
Generic method
Generics adding to List (? extends Object)
Compilation error with ? extends String
book K&B self test q16........problem
Why anonumous inner class is not take the ";"