aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections 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 "Collections" Watch "Collections" New topic
Author

Collections

deshdeep divakar
Ranch Hand

Joined: Apr 19, 2004
Posts: 91
What will happen when you attempt to compile and run the following code?

import java.util.*;
public class Thirsk{
public static void main(String argv[]){
Thirsk t = new Thirsk();
t.a19();
}
public void a19(){
TreeMap tm = new TreeMap();
tm.put("a","Bill");
tm.put("b","Kathy");
tm.put("c","Carl");
Iterator it = tm.keySet().iterator();
while(it.hasNext()){
System.out.print(tm.get(it.next()));
}
}
}



1) Compile time error, a19 cannot be used as a method name
2) Compilation and output of BillKathyCarl
3) Compilation and output of BillCarlKathy
4) Compilation and output of abc

Ans according to me have to be option 2,but the correct answer is becoz
put(Object Key, Object Value)//Associates the specified value with the specified key in this map

get(Object Key)//Returns the value to which this map maps the specified key
plz guide where I went wrong.


"Do not be afraid of going slow, be afraid of standing still"
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The answer is 2. Run the code yourself.

A TreeMap stores the keys in a tree in sorted order, which defaulkts to alphabetical order. Therefore, the iterator retrieves "a" "b" "c" in that order. These keys retrieve the corresponding objects "Bill" "Kathy" "Carl" in that order.


Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections