Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

urgent help on K&B chapter 7 Self Test Questions

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q 3. Which two statements are true about comparing two instances of the same class, given that the
equals() and hashCode() methods have been properly overridden? (Choose two.)
A. If the equals() method returns true, the hashCode() comparison == must
return true.
B. If the equals() method returns false, the hashCode() comparison != must return
true.
C. If the hashCode() comparison == returns true, the equals() method must return
true.
D. If the hashCode() comparison == returns true, the equals() method might
return true.
E. If the hashCode() comparison != returns true, the equals() method might
return true.

As per table 418 of k&b book, options B and D are correct.But the book says only option b is correct
the explanation why D is not correct is nonsense

Q 10.Given the following,
1. public class X {
2. public static void main(String [] args) {
3. X x = new X();
4. X x2 = m1(x);
5. X x4 = new X();
6. x2 = x4;
7. doComplexStuff();
8. }
9. static X m1(X mx) {
10. mx = new X();
11. return mx;
12. }
13. }
After line 6 runs. how many objects are eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
E. 4

when i had solved this question previously i was quite convinced that the answer is B.
But as i solved it again today, i got confused and marked answer A. could anyone please
conveince me how come the answer is A.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Q 3 refer https://coderanch.com/t/252120/java-programmer-SCJP/certification/equals-hashcode


Q 10.Given the following,
1. public class X {
2. public static void main(String [] args) {
3. X x = new X();
4. X x2 = m1(x);
5. X x4 = new X();
6. x2 = x4;
7. doComplexStuff();
8. }
9. static X m1(X mx) {
10. mx = new X();
11. return mx;
12. }
13. }
After line 6 runs. how many objects are eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
E. 4



Answer B is correct.
The statements which are creating objects are at line 3, 10, 5. After line 6 runs Object created at line 10 is not referenced any more; Object created at line 3 is still refrenced by variable x, Object created at line 5 is still referenced by variables x2 and x4; But there is no variable holding refrence to object created at line 10
[ December 29, 2005: Message edited by: jiju ka ]
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Niranjan,

In response to Q3: I don't think that you copied it from the book correctly! Go back and make sure that what you wrote in your post matches what's in the book!

Thanks,

Bert
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanls a lot bert bates...

here is the corrected question and relecant stuff...please explain

1. 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)

As per table 418 of k&b book, options B and D are correct.But the book says only option b is correct
the explanation why D is not correct is nonsense

here is the explanation

1. B. By contract, if two objects are equivalent according to the equals() method, then
the hashCode() method must evaluate them to be ==.
A is incorrect because if the hashCode() values are not equal, the two objects must not
be equal. C is incorrect because if equals() is not true there is no guarantee of any result
from hashCode(). D is incorrect because hashCode() will often return == even if the
two objects do not evaluate to equals() being true. (this last line doesnt make sense to me)
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been studying for the SCJP cert myself and as I understand the hashcode section hashcodes can be legally computed so as to allow to not equal objects to have the same hashcode incidently. There was an example in the book that showed how this could happen.

Bottom line in my mind is that: if x.equals(y) is TRUE then x.hashcode() MUST EQUAL y.hashcode() BUT if x.hashcode() happens to EQUAL y.hashcode() THEN x.equals(y) might be true but isn't required to be.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...
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)
...


according to table 418 of k&b book, answer D, x8.equals(x7) is incorrect, cause although (x7.hashCode() == x8.hashCode()) is true, and it is allowed to have x7.equals(x8) == true, BUT it's not required..

chandra chan
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic