File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections" Watch "Collections" New topic


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();
public void a19(){
TreeMap tm = new TreeMap();
Iterator it = tm.keySet().iterator();

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:
subject: Collections
It's not a secret anymore!