File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can't Understand... K&B Mock Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can Watch "Can New topic
Author

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

Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
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()


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





SCJP 1.5, SCWCD 1.4, SCBCD 5.0, next SCDJWS...
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
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....


Please Currect It...

Thank You..............
Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
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

Joined: Feb 12, 2007
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)

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
Chris Stann
Ranch Hand

Joined: Oct 10, 2006
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.



Concur, answer is B.


EXCEL IN ALL YOU DO
Ben Harrison
Greenhorn

Joined: Jan 26, 2006
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

Joined: Jan 26, 2006
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't Understand... K&B Mock Exam