Another cup of my favourite answers! This is question #53 from the first exam in K&B's CD.
The question presents a series of conditions over some object instances of unknow type, named x1, x2, x3, x4, x5 and x6. I'll skip the irrelevant stuff and post an equivalent question:
x5.equals(x6) == true
Which of the following statements will always be true? (Choose all that apply):
x5.hashCode() == x6.hashCode()
That answer was considered the correct and the others were all incorrect. But either the question is formulated wrongly or the answer is not correct.
We can clearly have a class where two equal objects have different hashCodes:
So the question should have rather been: "Which, according to the hashCode contract, should be always true?".Some questions are extremely picky on its language and others are leaving so much up to supposition. Not fair!
John Schubert wrote:So the question should have rather been: "Which, according to the hashCode contract, should be always true?".Some questions are extremely picky on its language and others are leaving so much up to supposition. Not fair!
I have to tell you, it is very reassuring to see someone else expressing the reactions I had after taking the mock exams. If it helps, I don't remember anything on the exam even remotely approaching the level of ambiguity we find in some of the mock questions.
On the one hand, I think the question should be worded better...
On the other hand, I think you're going overboard a bit. In the old days the real exam might have tried to "trick" you like that, but it's just not that way any more. The exam does sometimes focus on important details, but not in a "tricky" way. Perhaps you've looked at other mock exams that rely on tricks? The real exam focuses on important stuff, it doesn't rely on tricks.
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)