This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Can't Understand... K&B Mock Exam

Ranch Hand
Posts: 98
The code in K&B exam test 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 the out put is " x = 1111"

it is saying that x2.equals(x1) == true

Why???

I think the answe should x5.hashCode() == x6.hashCode()

Ranch Hand
Posts: 98
Ok Ok In Reference It is saying that ans C is Correct But when I do next it says Incorrect...

Dear K&B this is a Bug....

Thank You..............

Anvi Dixit
Ranch Hand
Posts: 45
Hi Damodar,

If you can send us the full code, so it will be useful for us to know exact problem in the code.

Thanks
Anvi

Premak Rajan
Greenhorn
Posts: 18
Hi Damodar,

The question code is:

Given the following,
11. x = 0;
12. if (x1.hashCode() != x2.hashCode() ) x = x + 1;
13. if (x3.equals(x4) ) x = x + 10;
14. if (!x5.equals(x6) ) x = x + 100;
15. if (x7.hashCode() == x8.hashCode() ) x = x + 1000;
16. System.out.println("x = " + x);
and assuming that the equals () and hashCode() methods are property implemented, if the
output is �x = 1111�, which of the following statements will always be true?
A. x2.equals(x1)
B. x3.hashCode() == x4.hashCode()
C. x5.hashCode() != x6.hashCode()
D. x8.equals(x7)

Here they did not said x2.equals(x1)==true.Only option B is always true for the condition given in the code.In the code they said x1.hashode()!=x2.hascode(),so x2.equals(x1) must be unequal

Chris Stann
Ranch Hand
Posts: 49
I missed this question on the K&B mock, so I had to break it down. I'll post it here for the benefit of others.

Given the following,
11. x = 0;
12. if (x1.hashCode() != x2.hashCode() ) x = x + 1;
13. if (x3.equals(x4) ) x = x + 10;
14. if (!x5.equals(x6) ) x = x + 100;
15. if (x7.hashCode() == x8.hashCode() ) x = x + 1000;
16. System.out.println("x = " + x);
and assuming that the equals () and hashCode() methods are property implemented, if the
output is �x = 1111�, which of the following statements will always be true?

Look below and keep in mind that this must ALWAYS be true

A. x2.equals(x1) -- If hasCode() is not equal, equals() CAN NEVER be true
A does not meet condition in line 12.

B. x3.hashCode() == x4.hashCode() -- If equals() is true, then hashCode() will ALWAYS be equal
B ALWAYS always meets the condition in line 13.

C. x5.hashCode() != x6.hashCode() -- if equals() is not true, hasCode() MIGHT be equal or not equal
C does NOT ALWAYS meet condition in line 14.

D. x8.equals(x7) -- if hasCode() is equal, then equals() MIGHT be true
D does NOT ALWAYS meet condition in line 15.

Ben Harrison
Greenhorn
Posts: 27
I'm glad others seem to be confused by this as well. But I think the answer is C, not B.

Because, if you know that in line 14 x5.equals(x6) then you know their hashcode must be equal, correct? So I think the text on the explaination of the error is correct, just not the Correct Answer portion. Too bad you can't copy paste from the exam...

Ben Harrison
Greenhorn
Posts: 27
oh wait, I see one problem...maybe there are two questions on the exam with basically teh same?

My question looks like Damodar's:
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);

but Chris Stann's question looks identical but has a ! on line 14...so I would think that would change the answer...also Chris's line 13 is not the same, as the line 13 I have, it checks to see == false, as opposed to Chris's which checks to see if the code is true, not explicitly checking...

Either way, I think C is correct in my example, but B is correct in Chris Stanns example, and the cd saying A is correct is way off...So what is up with this? I have my exam monday, I don't need this confusing me...

 Consider Paul's rocket mass heater.