• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Possibly an errata for John Meyer's Mock Exam

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I just tried the John Meyer's SCJP 5 Mock Exam and I am a little bit unsure about one of the questions:



One of the answers that is supposedly correct is:

If hashCode() is not overridden then regardless of whether line 1 returns true or false 2 will be printed on the screen.



Is this really correct? If line 1 is changed to return true, then all keys to the map that end up in the same hash-bucket will be considered equal. While the default hashCode() says that the hashcodes of distinct objects will be distinct "as much as is reasonably practical", there is no guarantee. E.g., if you generate new keys in a for-loop you will soon get overlap:



Running this for a few different MAX values will result in random equal keys:


Am I missing something in the original question?

Janko
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Janko, welcome to javaranch

Janko Heilgeist wrote:One of the answers that is supposedly correct is:

If hashCode() is not overridden then regardless of whether line 1 returns true or false 2 will be printed on the screen.



Is this really correct?



Did you try to run the program. That is the best way to confirm if this is correct or not.


Janko Heilgeist wrote:If line 1 is changed to return true, then all keys to the map that end up in the same hash-bucket will be considered equal.



Yes that's right. But if you don't override the hashCode method, all the keys will end up in different buckets so the equals method will have no effect in that case...

[edit: added welcome message]
 
Janko Heilgeist
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,

thanks for the welcome

I did try to run the program but my question was nitpicking on the fact that his answer is correct only most of the time. The default hashCode() method does not guarantee that the hashcodes of distinct objects will be distinct. And even if that guarantee were given, any hashing container will have only a limited number of buckets and you will end up with two distinct objects in the same bucket eventually.

Let's continue the nitpicking for a second: I ran his original source in a loop. For simplicity, I only changed the System.out.println() to a return (lines A and B). Thus, I can check the number of runs where his assumption holds:



On my computer this results in "2 out of 100000000 runs fail!". I'll admit that I expected more fails but, nevertheless, in my opinion the answer is wrong. The size of the map depends solely on the location of the keys on the heap. (... if we assume that the default hashCode() of an objects is its address on the heap.)
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well buddy for SCJP you'll have to go with JLS and API Documentation. The documentation of hashCode method mentions this

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.



So as you can see, you'll have to accept the fact that "2 out of 100000000" probability that you are getting doesn't apply here. I think you'll have to accept that the answer is right.

Now there is a clash here. Usually in Threading questions, we don't take even a 1 : 1000000000000 possibility as granted. If there is a chance that out of 10000000000 runs, once the code will behave differently, then we consider that the code's behavior is unpredictable. You are right at your stand that the answer is wrong. But what my experience with SCJP questions goes, it is always thought of that hashCode method of Object class will return unique value for different objects. May be this is because this thing is documented ...
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I am three posts late ! I have to agree that given a HUGE number of objects it is going to be difficult to avoid collisions, unless you have perfect hashing. I ran the program on my machine and got this

0 out of 100000000 runs fail!



JRE My eclipse 6.0 running on Windows XP. Your results surprise me a little though.

The answer assumes that for 2 new objects that are added to the hash map, the hashcode that is generated will be distinct. It is possible however as you have pointed out that collisions may occur even for 2 objects (I am still not able to digest that ). Perhaps I should change the explanation a little and emphasize the assumption. Thanks for writing that little test and sharing those results.

Oh and welcome to java ranch
 
Janko Heilgeist
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help, Ankit and Deepak
 
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic