Java Equals() Method Quiz

Java.lang.Object.equals() Method Quiz | Prerequisite:- Equals Method in Java | Another Quiz on Object class:- Object Class Quiz Set-1, Object Class Quiz Set-2,  Java Hashcode Value QuizJava Equals() Method Quiz

Q1) 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();
     System.out.println(t1.equals(t2));
  }
}

a) true
b) false
c) null
d) error


Ans:- b) false

Q2) Find the output of the below Java program?

public class Test {
  public static void main(String[] args) {
     Test t1 = new Test();
     Test t2 = t1;
     System.out.println(t1.equals(t2));
  }
}

a) true
b) false
c) null
d) error


Ans:- a) true

Q3) Find the output of the below Java program?

public class Test {
  public static void main(String[] args) {
     int x = 10;
     int y = 10;
     System.out.println(x.equals(y));
  }
}

a) true
b) false
c) error
d) Exception


Ans:- c) error

The equals() method of java.lang.Object class is a non-static method and to call equals() method object must be there.


Q4) Find the output of the below Java program?

class Student {
  int id;
  Student(int id) {
     this.id = id;
  }
}

public class Test {
  public static void main(String[] args) {
     Student s1 = new Student(1001);
     Student s2 = new Student(1001);
     System.out.println(s1.equals(s2));
  }
}

a) true
b) false
c) error
d) Exception


Ans:- b) false

Every class is a direct or indirect subclass of java.lang.Object class. Since the equals() method is not defined in the Student class therefore it is executed from java.lang.Object class. Object class equals() method is implemented to compare objects based on their references. The s1 and s2 objects have different reference hence it returns false.


Q5) Find the output of the below Java program?

class Student {
  int id;
  Student(int id) {
     this.id = id;
  }

  @Override
  public boolean equals(Object obj) {
     if(this == obj) return true;
        if(obj instanceof Student) {
           Student s = (Student)obj;
           return this.id == s.id;
        }
     return false;
  }
}

public class Test {
  public static void main(String[] args) {
     Student s1 = new Student(1001);
     Student s2 = new Student(1001);
     System.out.println(s1.equals(s2));
  }
}

a) true
b) false
c) error
d) Exception


Ans:- a) true

The equals() method is overridden in the Student class. Hence whenever the equals() method is called on the Student object then it is executed from the Student class rather than java.lang.Object class, which is meant for content comparison.


Q6) Whenever we are overriding equals() method then it is recommended to override which method of the java.lang.Object class?

a) toString()
b) hashCode()
c) clone()
d) getClass()


Ans:- a) hashCode()

Two equivalent objects should have the same hashcode. We should follows this relation between hashCode() and equals() method. Hence it is recommended to override the hashCode() method. Based on which parameter we override equals() method, it is highly recommended to use the same parameter while overriding hashCode() also.


Q7) Find the output of the below Java program?

public class Test {
  public static void main(String[] args) {
     System.out.println(null.equals(null));
  }
}

a) true
b) false
c) error
d) Exception


Ans:- c) error

We can’t dereference null, else we will get compile-time error: < null > cannot be dereferenced.


Q8) Find the output of the below Java program?

class Student {
  int id;
  Student(int id) {
     this.id = id;
  }
}

public class Test {
  public static void main(String[] args) {
     Student s1 = new Student(1001);
     System.out.println(null.equals(s1));
  }
}

a) true
b) false
c) error
d) Exception


Ans:- c) error

It is similar to the previous question. We can’t dereference null, else we will get compile-time error: < null > cannot be dereferenced.


Q9) Find the output of the below Java program?

class Student {
  int id;
  Student(int id) {
     this.id = id;
  }
}

public class Test {
  public static void main(String[] args) {
     Student s1 = new Student(1001);
     System.out.println(s1.equals(null));
  }
}

a) true
b) false
c) error
d) Exception


Ans:- b) false

The reference of s1 object is not null and hence we get results as false.


Q10) Find the output of the below Java program?

class Student {}
class Collage {}
public class Test {
  public static void main(String[] args) {
     Student s1 = new Student();
     Collage c1 = new Collage();
     System.out.println(s1.equals(c1));
  }
}

a) true
b) false
c) error
d) Exception


Ans:- b) false

For two incompatiable objects java.lang.equals() method always returns false.


Q11) Find the output of the below Java program?

class Student {}
public class Test {
  public static void main(String[] args) {
     Student s1 = null;
     Student s2 = new Student();
     System.out.println(s1.equals(s2));
  }
}

a) true
b) false
c) error
d) Exception


Ans:- d) Exception

The s1 referenced to null literal and equals() method can’t invoke it. We will get NullPointerException.


Q12) Find the output of the below Java program?

class Student {}
public class Test {
  public static void main(String[] args) {
     Student s1 = null;
     Student s2 = null;
     System.out.println(s1.equals(s2));
  }
}

a) true
b) false
c) error
d) Exception


Ans:- d) Exception

Similar to the previous one.


Q13) Find the output of the below Java program?

class Student {}
public class Test {
  public static void main(String[] args) {
     Student s1 = new Student();
     Student s2 = null;
     System.out.println(s1.equals(s2));
  }
}

a) true
b) false
c) error
d) Exception


Ans:- b) false

Please write comments if you find anything incorrect, or you want to share more information about Java Equals() Method Quiz the topic discussed above.


Also Learn,

Leave a Reply