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 tricky ques. 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 "tricky ques." Watch "tricky ques." New topic

tricky ques.

cs singh
Ranch Hand

Joined: Dec 28, 2005
Posts: 36

Answer is 3. , can anybody explain??
Alex Khvatov
Ranch Hand

Joined: Nov 07, 2005
Posts: 37
The answer is that HashMap uses the integer returned from calling a method Object.hashCode(). hashCode of an Integer(1) is always 1. By the definition HashMap cannot have duplicate keys, it replaces the value with the duplicate key with the new one; and returns either null if no previous value has been mapped with this key or the previous value that was mapped with this key.


SCJP 5<br />SCWCD 5
Murali Mohan
Ranch Hand

Joined: Jan 09, 2006
Posts: 66
Ya Alex is right.

Thanks,<br />Murali...
Ruchir Ghildiyal

Joined: Feb 26, 2006
Posts: 2
Also, to print out the result, the program uses System.out.println where gives an object of DoorKeys. To print out, this object is implicitly converted to String using toString() and since toString() method is overriden here to print out the result between "[" and "]", the value is printed out as [2]. Had the toString() method not been overriden, the result would have been plain vanilla 2.
[ February 26, 2006: Message edited by: Ruchir Ghildiyal ]
It is sorta covered in the JavaRanch Style Guide.
subject: tricky ques.
It's not a secret anymore!