Java Programming - Objects and Collections - Discussion


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?

[A]. class Test1 will not compile.
[B]. The Test1 hashCode() method is more efficient than the Test2 hashCode() method.
[C]. The Test1 hashCode() method is less efficient than the Test2 hashCode() method.
[D]. class Test2 will not compile.

Answer: Option C


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.

Kalai said: (Jul 27, 2011)  
In this class "value" is not initialized then how the operation (value^5) can be performed.

class Test2
public int value;
public int hashcode() { return (int)(value^5); }

Nafeez Abrar said: (Dec 12, 2011)  
It is not mentioned here that, the function hashcode() is return actually a hash value for a hash table or other purpose.

Jimbo said: (Jul 10, 2012)  
This question is not ideal.

In terms of efficiency, Test1's hashCode () method is marginally more efficient since no arithmetic is performed.

For the purpose of a hash table, they are both useless methods since each always returns the same value - 42 and 0 respectively (we never know if Test2's "value" member will ever be changed, thus we can only assume it always has the default value of 0).

Nickc-88 said: (Jul 31, 2013)  
Since variable value initialised with default value=0 class Test1 hashCode will be more efficient, because it only returns 42, but in Test2 it always performs arithmetic operation (0^5) and then returns value.

Vikesh said: (Sep 18, 2013)  
@Nickc-88, totally agreed. hasCode() for Test1 is more efficient since no computation is required. The value returned by both are obviously useless since they both return "a constant" value in all cases - only difference is one returns 42 without computing it, other returns 5 after some arithmetic operation.

Alex said: (Feb 28, 2014)  
The answer choices don't state that the hashCode/hashcode functions are more efficient hashing algorithms. Within the context of the code given, B should be the correct answer. Strictly speaking though, none of the answers are correct, because Test2 does not have a "hashCode" method.

Peter said: (Sep 29, 2015)  
Here is my suggestion to fix this contextual problem, open for further improvement.

In order to make it clear that the test is about knowing the purpose of the hashCode() method, answers B and C could be changed to:

[B]. The Test1 hashCode() method is a more efficient hashing method than the Test2 hashCode() method.

[C]. The Test1 hashCode() method is a less efficient hashing method than the Test2 hashCode() method.

What do you think?

Rupesh said: (Apr 27, 2016)  
There is no any public call for java file only one public class is needed.

Karam said: (Jul 26, 2016)  
The int value is not initialized, so compilation fails.

Salim said: (Sep 13, 2016)  

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

Robin said: (Feb 17, 2017)  
Exact int are initialized at 0, and don't forget that : 0^n = 1.

One return al the time 42 and the other one always 1. (Assuming we never change the parameter "value".

Fixit said: (Nov 7, 2017)  
This is a terrible question.

There is no mention of hash tables so you simply can't make that bit up - the methods have to be assessed on their merits.

Value is never initialised in Test2 so the method will always return 0. Hardly a model method!

The method in Test2 is called hashcode not hashCode so the "correct" option makes no sense.

Ken said: (Feb 17, 2018)  
It should be 'ideal' instead of efficient.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.