About Question enthuware.ocpjp.v7.2.1298 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Alina_Lapina
Posts: 15
Joined: Tue Jan 13, 2015 12:10 pm
Contact:

About Question enthuware.ocpjp.v7.2.1298 :

Post by Alina_Lapina »

In this line

Code: Select all

((Book)o).isbn.equals(...)
the private variable isbn is accessed in stead of getter.
Is it correct?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1298 :

Post by admin »

Yes, it is ok because it is being accessed from the same class.
If you like our products and services, please help us by posting your review here.

Alexey Berezkin
Posts: 20
Joined: Fri Jun 19, 2015 9:17 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1298 :

Post 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.

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1298 :

Post 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.
If you like our products and services, please help us by posting your review here.

Alexey Berezkin
Posts: 20
Joined: Fri Jun 19, 2015 9:17 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1298 :

Post 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” :)

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1298 :

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 239 guests