Win a copy of Spring Boot in Practice this week in the Spring forum!
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
• Tim Cooke
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Liutauras Vilda
• Henry Wong
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Mikalai Zaikin
• Himai Minh

# Mock Question from K&B Master Exam

Ranch Hand
Posts: 112
• Number of slices to send:
Optional 'thank-you' note:
Hi,

Question is as follows :

Given:
11. x = 0;
12. if (x1.hashCode() != x2.hashCode() ) x = x + 1;
13. if (x3.equals(x4) == false ) x = x + 10;
14. if (x5.equals(x6) == true ) x = x + 100;
15. if (x7.hashCode() == x8.hashCode() ) x = x + 1000;
16. System.out.println("x = " + x);
If the output is "x = 1111", which of the following statements will always be true? (Choose all that apply.)

There are 4 answer options and correct one is given below.(Other options I could not remember as ans guide dont provide them and its painful to go thru all the questions again looking for one question.)

x5.hashCode() == x6.hashCode()

Question says that output is X=1111 . As per the equals() contract given ans is correct. But my doubt is then why is it mentioning about 1111 output?

Does that mean we should not consider that point?

If someone has already solved this Q then they can understand why I am asking this.

Any Help would be appreciated .

Thanks,
vivian

Ranch Hand
Posts: 37
• Number of slices to send:
Optional 'thank-you' note:
It means all the conditions are true: 1000+100+10+1=1111.

Vivian Josh
Ranch Hand
Posts: 112
• Number of slices to send:
Optional 'thank-you' note:
No, I fell for the same trap

I checked all 4 answers because it evaluated to 1111. But only ans true is the given one and in that case x becomes 100 . (Initial x =0 and all other cases were in if() statement.)

Yeming Hu
Ranch Hand
Posts: 37
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Vivian Josh:
No, I fell for the same trap

I checked all 4 answers because it evaluated to 1111. But only ans true is the given one and in that case x becomes 100 . (Initial x =0 and all other cases were in if() statement.)

Not really, It's not the trap. please give the options. I can explain them

Ranch Hand
Posts: 34
• Number of slices to send:
Optional 'thank-you' note:
Hi,
The question was trying to figure out your understanding on hashcode and equals contract.

All the conditions are valid, choose the one that is correct from the answers

x1.hashCode() != x2.hashCode() x = x + 1;
x3.equals(x4) == false x = x + 10;
x5.equals(x6) == true x = x + 100;
x7.hashCode() == x8.hashCode() x = x + 1000;
16. System.out.println("x = " + x); ----> 1111

1111 can be got only if all the conditions are met, indirectly hinting that the above comparisons on hashcode and equals result in true, Now choose from the answers which one is correct.

Going condition by condition,

HashCode and Equals Contract
------------------------------
The hashcode and equals contract states for any two objects x,y that
if (x.hashcode() == y.hashcode() ){
x.equals() MAY_BE_EQUALS_TO y.equals()
}

if (x NOT_EQUALS y))){
x.hashcode() MAY_BE_EQUALS_TO y.hascode()
}

if (x EQUALS y)){
x.hashcode() SHOULD_BE_EQUALS_TO y.hascode()
}

if (x.hashcode() != y.hashcode() ){
x.equals() SHOULD_NOT_EQUALS_TO y.equals()
}

CASE 1:

x1.hashCode() != x2.hashCode()

SOL: x NOT_EQUALS y

CASE 2:

x3.equals(x4) = false

SOL : x.hashCode() MAY_BE_EQUALS y.hashCode()

CASE 3:
x5.equals(x6) = true

x5.hashCode() MUST_BE_EQUALS x6.hashCode()

CASE 4
x7.hashCode() == x8.hashCode()

x7 MIGHT_BE_EQUALS x8

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?