This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
Similar Threads
Marcus Green's Mock Exam 1 Question.
Will Collection class methods come in SCJP1.4 exam?
Thread - TreeSet Iterator
for containsKey it works but for containsValue not...What is the reason?
Another question from mock exam