Java Hashcode Quiz

Java Hashcode Quiz | Hashcode is an identity of an object. In Java, for every object, a unique number is generated by the JVM and it is nothing but a hashcode. It is a 32-bit unique integer number and is used to differentiate one object from another object and also used for differentiating one group of objects from other groups of objects. Also see:- Object Class in Java

If you are finding it difficult to solve these problems then you should learn- hashcode in Java, and System.identityHashCode() in Java.

Q1) Can the hashcode of two objects be the same in Java?

a) Yes
b) No

View Answer Answer:- a) Yes

If we override hashCode() then two objects can have same hashcode. But if we are using hashCode() of Object class then two different objects will not have same hashcode.

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

View Answer Answer:- b) Reference in integer

The hashCode() method of Object class returns reference of the object in integer form.

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

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

View Answer Answer:- c) Depends

If we are not overriding hashCode() method in our class then it returns reference of the object in integer form. But if hashCode() method is overriden then hashCode() method return value based on its implementation, not based on 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

View Answer Answer:- a) 225534817 10

In Test class hashCode() method is not overriden therefore it return hashcode value based on the object reference. But in “A” class hashCode() method is overriden and return the object state.

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

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

View Answer Answer:- c) ArrayList

JVM uses the hashcode value while saving data in hashing related data structures like HashTable, HashSet, and HashMap. ArrayList is not based on hashing therefore it doesn’t use hashCode() method to store the data.

Q6) Find the output of the below Java program?

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

    Test t1 = new Test();
    Test t2 = new Test();
    Test t3 = t1;
    
    System.out.print(t1.hashCode() == t2.hashCode()); 
    System.out.print(", ");
    System.out.print(t1.hashCode() == t3.hashCode()); 
  }
}

a) false, true
b) true, false
c) true, true
d) false, false

View Answer Answer:- a) false, true

The hashCode() method of java.lang.Object class return reference of the object in integer form. If two objects have same reference then they will have same hashcode value.

In this program, t1 and t2 are two different objects so they contain different reference. Therefore their hashcode values also will be different. But t1 and t3 objects have same reference, so their hashcode value will be exactly same.

Q7) Find the output of the below Java program?

public class Test {
  
  @Override
  public int hashCode() {
    return 100;
  }
  
  public static void main(String[] args) {
    Test t1 = new Test();
    Test t2 = new Test();
    Test t3 = t1;
    
    System.out.print(t1.hashCode() == t2.hashCode()); 
    System.out.print(", ");
    System.out.print(t1.hashCode() == t3.hashCode()); 
  }
}

a) false, true
b) true, false
c) true, true
d) false, false

View Answer Answer:- c) true, true

When the hashCode() method is overridden in the program, then it returns the hashcode value based on its implementation.

In the above program whenever we call hashCode() on Test class object then it returns 100. Here, t1.hashCode() gives 100, t2.hashCode() gives 100, and similarly t3.hashCode() gives 100. Hence all these three values are equal.

Q8) Find the output of the below Java program?

public class Test {
  
  private int i;
  
  Test(int i) {
    this.i = i;
  }
  
  @Override
  public int hashCode() {
    return i;
  }
  
  public static void main(String[] args) {
    Test t1 = new Test(10);
    Test t2 = new Test(20);
    Test t3 = new Test(30);
    
    System.out.print(t1.hashCode() == t2.hashCode()); 
    System.out.print(", ");
    System.out.print(t1.hashCode() == t3.hashCode()); 
  }
}

a) false, true
b) true, false
c) true, true
d) false, false

View Answer Answer:- d) false, false

In the above program, hashCode() method return value based on the state of the object. The t1.hashCode() returns 10, t2.hashCode() returns 20, and t2.hashCode() returns 30. These three values are different from each other therefore we get false.

Q9) Will the hashcode value changed if we change the state of the object (while we override the hashCode() method)? Find the output of the below program?

public class Test {
  
  int i;
  
  Test(int i) {
    this.i = i;
  }
  
  @Override
  public int hashCode() {
    return i;
  }
    
  public static void main(String[] args) {
    Test t1 = new Test(10);
    System.out.print(t1.hashCode());
    System.out.print(", ");
    
    t1.i = 20;
    System.out.print(t1.hashCode());
  }
}

a) 10, 10
b) 10, 20
c) 10, 0
d) Compile time error or Exception raised at runtime.

View Answer Answer:- b) 10, 20

If the hashCode() method is overridden then on changing the state of the object the hashcode value returned by the hashCode() method will also be changed.

Q10) Find the output of the below Java program?

public class A {
  int i;
  
  A(int i) {
    this.i = i;
  }
  
  @Override
  public int hashCode() {
    return i;
  }
}
public class Test {
  int i;
  
  Test(int i) {
    this.i = i;
  }
    
  public static void main(String[] args) {
    Test t1 = new Test(10);
    int h1 = t1.hashCode();   
    t1.i = 20;
    int h2 = t1.hashCode();
    System.out.print(h1 == h2);
    System.out.print(", ");
    
    A a1 = new A(30);
    int h3 = a1.hashCode();
    a1.i = 50;
    int h4 = a1.hashCode();
    System.out.println(h3 == h4);
  }
}

a) false, true
b) true, false
c) true, true
d) false, false

View Answer Answer:- b) true, false

If the hashCode() method is not overridden then after changing the value of an object the hashcode value returned by the hashCode() method won’t change because it is reference-based. But if the hashCode() method is overridden then the hashcode value returned by the hashCode() method will also be changed.

In the above program, the Test class doesn’t override the hashCode() method therefore hashcode value will not change after changing t1.i = 20, hence h1 and h2 hold same value. Due to this reason, h1 == h2 becomes true.

The “A” class overrides the hashCode() method therefore hashcode value also will be changed after changing the state. The h3 holds 30, after updating a1.i = 50 the h4 will hold 50. Now, h3 and h4 contain different values, 30 == 50 becomes false.

Q11) 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

View Answer Answer:- d) All of these

If we don’t override hashCode() method then all these three returns the JVM generated referenced based hashcode value.

Q12) Which of the following method can give the address of a primitive variable?

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

View Answer Answer:- 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 can give hashcode of the primitive variables.

Q13) If we override the 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

View Answer Answer:- 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.

Q14) Find the 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

View Answer Answer:- a) true

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

Q15) Find the output of the 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

View Answer Answer:- b) False

The hashCode() method is overriden in the A class. Therefore, both hashCode() and super.hashCode() return different value. The a1.hashCode() will return 10, but a1.JVMHashCode() returns reference in integer form.

Q16) In the below program at which line we will get what result/value?

The result/value generated by each line is separated by a comma. Assume “error” means Compile-time error, and “exception” means an Exception raised at runtime.

String str = null;
    
// line-1
System.out.println(System.identityHashCode(null));
    
// line-2
System.out.println(null.hashCode());
    
// line-3
System.out.println(System.identityHashCode(str));
    
// line-4
System.out.println(str.hashCode());

a) error, error, error, error
b) null, error, null, exception
c) 0, error, 0, exception
d) 0, exception, 0, exception

View Answer Answer:- c) 0, error, 0, exception

We can’t call hashCode() method on null, or null reference variable. Calling identityHashCode() on null, or null reference variable returns 0. Therefore line-1 and line-3 gives result 0.

We can’t call any method on null, it will give the compile-time error. Therefore line-2 gives a compile-time error. We can call any method on null reference variable, it will not give a compile-time error but it will give NullPointerException. Therefore line-4 gives exception at runtime.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *