Java Programming - Objects and Collections - Discussion

Discussion Forum : Objects and Collections - Pointing out the correct statements (Q.No. 1)
1.
class Test1 
{
    public int value;
    public int hashCode() { return 42; }
}
class Test2 
{
    public int value;
    public int hashcode() { return (int)(value^5); }
}
which statement is true?
class Test1 will not compile.
The Test1 hashCode() method is more efficient than the Test2 hashCode() method.
The Test1 hashCode() method is less efficient than the Test2 hashCode() method.
class Test2 will not compile.
Answer: Option
Explanation:

The so-called "hashing algorithm" implemented by class Test1 will always return the same value, 42, which is legal but which will place all of the hash table entries into a single bucket, the most inefficient setup possible.

Option A and D are incorrect because these classes are legal.

Option B is incorrect based on the logic described above.

Discussion:
14 comments Page 2 of 2.

Rupesh said:   9 years ago
There is no any public call for java file only one public class is needed.

Salim said:   9 years ago
@Karam

The instance variables are initialized to default, here 0(zero).

Karam said:   9 years ago
The int value is not initialized, so compilation fails.

Ken said:   8 years ago
It should be 'ideal' instead of efficient.


Post your comments here:

Your comments will be displayed after verification.