Nicola Carraro

Greenhorn
+ Follow
since Jun 17, 2019
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Nicola Carraro

I am referring to the book "OCP Java SE8 Programmer II Exam Guide" by Sierra,  Bates and Robson (Oracle Press 2018).

Question 4 in the self test of chapter 6 has the following code:

Answer C says "as the code stands, the output will be 3". According to the answers section, C is true because "if hashCode() is not overridden, then every entry will go into its own bucket, and the overridden equals() method will have no effect on determining equivalency".

Now, I get why this is true most of the time. However it seems to me that there are scenarios where the output would be 2, namely:
  • if the the hashCode() method of the Object class happens to assign the same hashcode to the two "Monday" ToDos objects
  • if their hashcodes happen to evaluate to the same bucket

  • While the first scenario is unlikely, the second might even happen in practice.

    So, my first question is: am I missing something?
    Second question: when am I allowed to ignore these exceptional cases in the exam? After all, it is not like the exam will never try to trick me with edge cases!

    Thank you in advance!