33. Consider the following implementation of a hashCode) method public class Poi
ID: 3596698 • Letter: 3
Question
33. Consider the following implementation of a hashCode) method public class Point2D Double x, y: public int hashCode f return x.hashCodey.hashCode ); Which of the following statements are true about two instances p and q of a Point2D? (a) p.hashCode )-0 if p.x p.y (b) p.hashCode()q.hashCode() ifp.x#4.y orp.,, q.x p.hashCod@ () =q.hashCode() ifp.x=q.y and p.ysq.x (d) Both (a) and (b) are true (e) Both (a) and (c) are true 34. Consider the following implementation of a hashCode) method public class Point2D Double x, y; public int hashCode ) return 37 xhashCodey.hashCode Which of the following statements are true about two instances p and q of a Point2D? (a) p.hashCode)-0 if p.x p.y X (b) p.hashCode()#4.hashCode() ifp.x#4.yor p.y#4.xx noa (c) p.hashCode()-q.hashCode() ifp.x=q.y and p.y=q.x X (d) Both (a) and (b) are true (e) Both (a) and (c) are true nswerExplanation / Answer
I feel the answers mentioned in the image are correct.
First question answer: C
You can guess the answer by substituting values.
hasCode() is implemented as x.hashCode() + y.hashCode()
Let hashCode() for double values are like below.
3.4 -> h1
2.1 -> h2
4.8 -> h3
6.7 -> h4
Note: hashCode() for every unique value is unique.
Option a) Let p(4.8, 4.8)
p.hashCode() will be (2 x h3) != 0. So False
Option b) Let p(4.8, 6.7) and q(4.8, 6.7)
p.hashCode() == q.hashCode(). So False
Option c) Let p(4.8, 6.7) and q(6.7, 4.8)
p.hashCode() == q.hashCode(). So True
Option d) False
Option e) False
Second question answer: Has no answer
hasCode() is implemented as 37 * x.hashCode() + y.hashCode()
Option a) Let p(4.8, 4.8)
p.hashCode() will be (38 x h3) != 0. So False
Option b) Let p(4.8, 6.7) and q(4.8, 6.7)
p.hashCode() == q.hashCode(). So False
Option c) Let p(4.8, 6.7) and q(6.7, 4.8)
p.hashCode() != q.hashCode(). So False
Option d) False
Option e) False
So, No answer is true for this question.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.