Java Hashcode Value Quiz

Q1) In Java, can the hashCode() method return the same value for two different objects?

a) Always
b) Never
c) Depends
d) None of these


Ans:- c) depends

If hashCode() is overriden then two objects can have same hashcode value returned by hashCode() method.


Q2) What value is returned by hashCode() method of java.lang.Object class?

a) Null
b) Reference in integer
c) Reference in hexadecimal
d) None of these


Ans:- b) reference in integer

Q3) Are value returned by hashCode() method and reference of an object both are the same?

a) Always
b) Never
c) Depends
d) None of these


Ans:- c) depends
If hashCode() is overriden then hashCode() method reutn value based on its implementation, not the reference.

Q4) Find the possible output for the below Java program?

class A {

   private int x;

   A(int x){
      this.x = x;
   }

   @Override
   public int hashCode() {
      return x;
   }
}

public class Test {
   public static void main(String[] args) {

      Test t1 = new Test();
      System.out.print(t1.hashCode() + " ");

      A a1 = new A(10);
      System.out.println(a1.hashCode());
   }
}

a) 225534817 10
b) 10 225534817
c) null 0
d) null 10


Ans:- a) 225534817 10

Q5) Which of the following collection class data structure doesn’t use hashcode value for saving the data?

a) HashSet
b) HashMap
c) ArrayList
d) HashTable


Ans:- c) ArrayList

JVM uses the hashcode value while saving data in hashing related data structures like HashTable, HashSet, and HashMap.

If these questions looks difficult then you should read once again hashCode() method in Java.


Different ways to get reference-based hashcode value

Q6) In Java, which of the following by default returns JVM generated hashCode value?

a) hashCode()
b) System.identityHashCode()
c) super.hashCode()
d) All of these


Ans:- d) All of these.

Q7) Which of the following method can give address of an primitive variable?

a) hashCode()
b) System.identityHashCode()
c) Both
d) None of these


Ans:- b) System.identityHashCode()

The hashCode() method is a non-static method so, to call this method object should be there. The System.identityHashCode() is static method. It also gives hashcode of the primitive variables.


Q8) If we override hashCode() method in a Java class then JVM generated hashcode value also will be changed?

a) True
b) False
c) Depends on Operating System
d) None of these


Ans:- b) false

JVM always generates a unique value for every object and the default implementation of hashCode() in Object class is one of the ways to retrieve them. We can also get them using System.identityHashCode() or super.hashCode(). If we override the hashCode() method then it doesn’t mean JVM generated hashcode value is changed.


Q9) Find output of the below program?

public class Test {
   public static void main(String[] args) {
      Test t1 = new Test();
      long l1 = t1.hashCode();
      long l2 = System.identityHashCode(t1);
      System.out.println(l1 == l2);
   }
}

a) True
b) False
c) Depends on Operating System
d) None of these


Ans:- a) true

The hashCode() method is not overriden in the Test class. Therefor, both hashCode() and System.identityHashCode() method return same value.


Q10) Find the output of below Java program?

class A {

   private int x;

   A(int x){
      this.x = x;
   }

   @Override
   public int hashCode() {
      return x;
   }

   public int JVMHashCode() {
      return super.hashCode();
   }
}

public class Test {
   public static void main(String[] args) {
      A a1 = new A(10);

      long l1 = a1.hashCode();
      long l2 = a1.JVMHashCode();

      System.out.println(l1 == l2);
   }
}

a) True
b) False
c) Depends on Operating System
d) None of these


Ans:- b) false

The hashCode() method is overriden in the A class. Therefore, both hashCode() and super.hashCode() return different value.


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Also Learn,

Leave a Reply