• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in one question of K & B Mock exam on CD

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The question give is:

x = 0
if(x1.hashCode() != x2.hashCode()) x = x +1
if(x3.equals(x4) == false) x = x+ 10;
if (x5.equals(x6) == true ) x = x + 100;
if (x7.hashCode() == x8.hashCode()) x = x + 1000
System.out.println("x = " + x)
If output is "x = 1111", which of following statements will always be true?

Answer given: x2.equals(x1)
Explaination: By Contract if two objects are equal then their hashcode must evaluate thenm to be ==

But, according to me, since o/p is 1111, all the above if condition will evaluate to true and hence x.hashCode() != x2.hashCode() which is contradicting the explaination.

Can someone tell me that whether there is an error of mock exam, or am I missing something important?
 
Hamid Virani
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain the reason for the answer?
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I'm concerned you are right. Different hashCodes lead to unequal objects unless inappropriate implementation of hashCode/equals methods.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not the only ones to have this doubt. Earlier thread on same issue.
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that mock was perfect. That will be one of my latest mocks before the exam, so it's good to know it.
[ February 05, 2007: Message edited by: Javier Sanchez Cerrillo ]
 
Hamid Virani
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for confirming my doubt.
 
reply
    Bookmark Topic Watch Topic
  • New Topic