Page 1 of 1
About Question enthuware.ocpjp.v7.2.1298 :
Posted: Tue Feb 10, 2015 7:22 am
by Alina_Lapina
In this line
the private variable
isbn is accessed in stead of getter.
Is it correct?
Re: About Question enthuware.ocpjp.v7.2.1298 :
Posted: Tue Feb 10, 2015 9:12 am
by admin
Yes, it is ok because it is being accessed from the same class.
Re: About Question enthuware.ocpjp.v7.2.1298 :
Posted: Tue Jun 23, 2015 7:18 am
by Alexey Berezkin
Though Book will use hashCode() implemented in Object class, it's not guaranteed that the hashcode will be always different for different instances. Remember a contract — for 'non-equal' objects it's OK to have same hashcode, and this is named a 'hash collision'. Of course, there is very little probability of facing this behaviour in a single run, but it's indeed greater than zero. To expose it, you may wrap main() contents to an infinite loop and check what second bs.getNumberOfCopies(b) returns in each iteration. If you get patient and wait several minutes you'll see returning 10 somewhat on 10,000,000 or 100,000,000 iteration. Thus, the program will not always throw an exception and sometimes will print 10 10.
Re: About Question enthuware.ocpjp.v7.2.1298 :
Posted: Tue Jun 23, 2015 11:41 am
by admin
You are right. In situation that you have described, a hashcode may be repeated.
Yet, out of all the options, this is the best option.
Re: About Question enthuware.ocpjp.v7.2.1298 :
Posted: Thu Jun 25, 2015 10:13 am
by Alexey Berezkin
It's interesting that the official OCA/OCP JavaSE7 preparation book (Sierra K., Bates B. — OCA OCP Java SE 7 Programmer I & II Study Guide - 2015) contains a question on the same behaviour (Question 4 from Chapter 11 Selftest, page 668), and the answer implies the same — disregarding Object.hashCode() collision. Unfortunately book author ignored my mistake report, so it's unknown if an exam expects from candidates taking into consideration this effect. However, I suppose it would be nice to add at least a remark about this into a question explanation. Like, “but when you successfully passed an exam, welcome to the real life with Object.hashCode() collisions”

Re: About Question enthuware.ocpjp.v7.2.1298 :
Posted: Thu Jun 25, 2015 8:57 pm
by admin
For the purpose of the exam, you can assume that it is unique.
The question is already linked to this discussion and so anyone who clicks on it will see this useful information.
HTH,
Paul.