• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()


Please explain..........



 
Damodar Mukhopadhyay
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....


Please Currect It...

Thank You..............
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

answer is b

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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



Concur, answer is B.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic