Home / Java Programming / Objects and Collections :: Discussion

Discussion :: Objects and Collections

  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?

  2. 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.

    View Answer

    Workspace

    Answer : Option C

    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.


Be The First To Comment