• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

OCP Java SE8 Study Guide Chapter 6 Question 4

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!