Java Programming - Objects and Collections - Discussion
Discussion Forum : Objects and Collections - Pointing out the correct statements (Q.No. 3)
3.
Which of the following statements about the hashcode() method are incorrect?
- The value returned by hashcode() is used in some collection classes to help locate objects.
- The hashcode() method is required to return a positive int value.
- The hashcode() method in the String class is the one inherited from Object.
- Two new empty String objects will produce identical hashcodes.
Answer: Option
Explanation:
(2) is an incorrect statement because there is no such requirement.
(3) is an incorrect statement and therefore a correct answer because the hashcode for a string is computed from the characters in the string.
Discussion:
6 comments Page 1 of 1.
Jjj said:
10 years ago
Two new empty String objects will produce identical hash codes.
The above will produce unique hash codes right and therefore its incorrect or am I wrong?
The above will produce unique hash codes right and therefore its incorrect or am I wrong?
Radistao said:
10 years ago
Damn, guys, hashCode(), not hashcode() - JAVA METHODS ARE CASE SENSITIVE!
HEPBO3AH said:
9 years ago
@Jjj you are correct.
Two empty strings do produce identical hashCode(). Also, some Collections DO use hashCode() to store help locate the data, namely HashMap. Its even has a "hash" in its name. Terrible question
Two empty strings do produce identical hashCode(). Also, some Collections DO use hashCode() to store help locate the data, namely HashMap. Its even has a "hash" in its name. Terrible question
Ford Prefect said:
9 years ago
The question is asked in a confusing way. The text should be changed from "incorrect" to "correct" and the answer should be inverted. Also, it's hashCode () not hashcode ().
Avinash said:
9 years ago
Every object uses hashCode, why only for some collections?
Sat said:
9 years ago
The hashCode always return positive int value, not a negative value, or float or double etc then how the #2 is wrong?
As #3 as for giving string class inherit hashCode() from object class that is correct because it is mentioned to inheritance not for how the hashCode is calculated for a string?
As #3 as for giving string class inherit hashCode() from object class that is correct because it is mentioned to inheritance not for how the hashCode is calculated for a string?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers