Interview Questions and Answers for 'Equals' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Equals' - 23 question(s) found - Order By Newest

Very frequently asked. Among first few questions in almost all interviews. Among Top 5 frequently asked questions. Frequently asked in Indian service companies (HCL,TCS,Infosys,Capgemini etc based on multiple feedback ) and Epam Systems
  Q1. Difference between == and .equals() ?Core Java
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality, whereas "==" operator evaluate to see if the object handlers on the left and right are pointing to the same object in memory.

x.equals(y) means the references x and y are holding objects that are equal. x==y means that the references x and y have same object.

Sample code:

String x = new String("str");
String y = new String("str");

System.out.println(x == y); // prints false
System.out.println(x.equals(y)); // prints true

  Sample Code for equals

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   string comparison   string   object class   ==    equals   object equality  operator   == vs equals   equals vs ==     Asked in 294 Companies      basic        frequent

Try 6 Question(s) Test


 Q2. What will be the output of following Code ?

class BuggyBread {
   public static void main(String[] args) {
      String s2 = "I am unique!";
      String s5 = "I am unique!";

      System.out.println(s2 == s5);
   }
}
Core Java
Ans. true, due to String Pool, both will point to a same String object.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   code   coding   tricky questions   interesting questions   string   string pool   .equal   ==      intermediate        frequent


 Q3. Can we compare Integers by using equals() in Java ?Core Java
Ans. Yes for the Wrapper class Integer but not for the primitive int.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   wrapper classes   equals


Very Frequently asked. Favorite question in walkins and telephonic interviews. Usually among first few questions. Asked in different variants. Must know for intermediate and expert professionals.Among Top 10 frequently asked questions.
  Q4. What is rule regarding overriding equals and hashCode method ?Core Java
Ans. A Class must override the hashCode method if its overriding the equals method.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   hashcode  hash code   equals   collections     Asked in 44 Companies      intermediate        frequent

Try 1 Question(s) Test


 Q5. Why is null == null returns true ?Core Java
Ans. Any reference in java that doesn't point to any object , gets assigned null i.e is a reference to null. Two object references in java are treated equal if they point to the same memory. That's Why null == null results true.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   null   equality   references


 Q6. What will be the output ?Integer a = 10, b =10;Integer c = 10, d = 1000;System.out.println(a == b);System.out.println(c ==d);Core Java
Ans. truefalse

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   coding   equality   object equality


 Q7. What will the following code print ?

Integer a = 100, b =100;
Integer c = 1000, d = 1000;
System.out.println(a == b);
System.out.println(c ==d);
Core Java
Ans. false
false

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     object equality  code  coding        frequent


 Q8. What is wrong with the following if statement ?

if(x==y && x.equals(y) {
}
Core Java
Ans. x==y means that both references have same type and are pointing to same memory location and hence would always mean that they have same value.

x.equals(y) is not required in this case.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     if statement  control statements  == and equals  ==  .equals   code optimization

Try 2 Question(s) Test


 Q9. What is wrong with the following if statement ?

if(x==y || x.equals(y) {
}
Core Java
Ans. if x==y turns out to be true x.equals(y) will be true too. If x.equals(y) could be true even if x==y is true or not.

So the only possible outcomes are

1 || 1 = 1
0 || 1 = 1
0 || 0 = 0

i.e the outcome of x.equals(y)

check for x==y is not required in this if statement.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     if statement  control statements  == and equals  ==  .equals   code optimization

Try 1 Question(s) Test


 Q10. What is the difference between = and == in Java ?Core Java
Ans. = is the assignment operator that assigns the result of the expression on the right to the variable on the left, whereas

== is the operator to check object equality to see if the reference on left and right are pointing to the same object. For primitive types, its used to check if both variables holds the same value.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     =  ==  assignment operator  object equality  difference between      Basic


 Q11. How is == operator different for objects and primitive types ? Core Java
Ans. For objects or references, == operator check if the reference on left and right points to the same object.

For primitive types or variables, == operator check if the variable on left and right holds the same value.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     ==  object equality  operator      Basic


 Q12. In Java, if object A contains same the same data of object B, are they equal?Core Java
Ans. it depends on the implementation of equals method of the respective class. If no definition is provided and it uses the default definition of the object class, two references are equal only if they point to the same object.

We can have such an equality defined for a particular class objects if we provide appropriate implementation of equals method comparing all those fields.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     equals  equals method


 Q13. What will be the output of following code

Integer x = 1;
Integer y = 2;
System.out.println(x == y);

What if you change 1 to "1" and Integer to String?
Core Java
Ans. It will print "true" with integers as well as strings. The reason is "Integer constant pool" and "String pool"

String pool maintains pool of string literals. When a string literal is used for the first time, a new string object is created and is added to the pool. Upon it's subsequent usage , the reference for the same object is returned. Similarly java uses integer constant pool.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string pool  object equality  ==


 Q14. What is equals method in Java ?Core Java
Ans. equals is the method of Object class that is overridden by the classes to specify object equality criteria.

As every class extends Object class, the default implementation of equals is carried to them. Default implementation specified in the Object class is that two objects are treated equal if they are same.

i.e

Object x = new Object();
Object y = new Object();
x.equals(y); // false
x=y;
x.equals(y); // true

i.e x.equals(y) if only x==y

Now every class has the option to specify their object equality by overriding equals method. For example - String class has implemented in a manner if the string value contained in them is exactly same.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     equals


 Q15. Which of the following is valid greater than and equal to operator in Java ?

>=
=>
Core Java
Ans. >=

=> will result in error.

=> somewhat looks like lambda operator "->"

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     operators  >= operator  greater than and equal to operator  comparison operators


 Q16. What will be the output of following code

class TestMain {
public static void main(String[] args) {
String s1 = "ravi";
String s2 = new String("ravi");
Integer i = 10;
Integer a1 = new Integer("10");
System.out.println(s1 == s1);
System.out.println(i == a1);
}
}
Core Java
Ans. false
false

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     object equality  ==  coding  code     Asked in 1 Companies


 Q17. What is a reflexive equals contract ?Core Java
Ans. An equal contract is said to be reflexive is equality between 2 objects always returns to be true.

For example -

Object o1;
Object o2;

o1.equals(o2); // returns true
o2.equals(o1); //returns true

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     reflexive equals contract


 Q18. Explain Transitive equals contract.Core Java
Ans. The equals contract is said to be transitive if

obj1.equals(obj2); and obj2(.equals(obj3)

then obj1.equals(obj3) also returns true.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Transitive equals contract        rare


 Q19. why is "".equals(str); safer than str.equals("")?Core Java
Ans. str.equals("") this statement will throw NullPointerException if str is null where as "".equals(str) works fine even if str is null

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     equals method     Asked in 1 Companies


 Q20. Difference between equals and compareTo in Java ?Core Java
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality. x.equals(y) means the references x and y are holding objects that are equal.

The compareTo() method is used for comparing two objects in Java. It is usually defined for the classes whose objects needs to be ordered through Comparable interface or need to be part of an ordered collection like TreeSet or TreeMap.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     equals  compareTo  equals vs compareTo


 Q21. Difference between equals and hashcode in java ?Core Java
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality. x.equals(y) means the references x and y are holding objects that are equal with the equality defined by the definition of equals method.

Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. The value received from hashcode() is used as bucket number for storing elements. This bucket number is the address of the element inside the set/map. when you do contains() then it will take the hashcode of the element, then look for the bucket where hashcode points to and if more than 1 element is found in the same bucket (multiple objects can have the same hashcode) then it uses the equals() method to evaluate if object are equal, and then decide if contain() is true or false, or decide if element could be added in the set or not.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     equals  hascode  equals vs hascode


 Q22. Difference between equals and equalsignorecase in Java ?Core Java
Ans. "equals" method is the method of object class that needs to be overridden to check object equality. This is not specific to any class like String.

equalsignorecase is the method of String class that provides a definition that ignores the case of characters during comparison.

The only difference between them in String class is that the equals() methods considers the case while equalsIgnoreCase() methods ignores the case during comparison.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     equals  equalsignorecase  equals vs equalsignorecase


 Q23. What is the use of defining equals , compareTo and hashcode methods in a class ? Where are they used ?Core Java
Ans. equals, compareTo and hashcode are of use when the objects are used within collections.

Equals helps with collections that helps maintaining only unique objects ( like Set )

compare and compareTo helps with collections that helps maintaining objects in order ( TreeSet, TreeMap etc )

hascode helps with collections that facilitates hash searching ( like hashSet, hashMap etc )

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     equals    compareTo   hashcode method


 Q24. What will be the output of following ?

String str1 = new String("String1");
String str2 = new String("String1");
System.out.print(str1 == str2);
System.out.print(str1.equals(str2));
str1 = str2;
System.out.print(str1 == str2);
Core Java
a. falsetruetrue
b. truetruetrue
c. truetruefalse
d. falsetruefalse

Ans.a. falsetruetrue

 Q25. What will be the output of following code ?

String str1 = "String1";
String str2 = "String1";
System.out.print(str1 == str2);
System.out.print(str1.equals(str2));
str1 = str2;
System.out.print(str1 == str2);
Core Java
a. falsetruetrue
b. falsefalsetrue
c. truetruetrue
d. falsefalsefalse

Ans.c. truetruetrue

 Q26. Which of the following is false ?Core Java
a. A Class cannot override both hashcode and equals method.
b. A class can override both hashcode and equals method.
c. A Class must override hashCode method if its overridding equal method.
d. A Class can override hashCode even if its not overridding equals method.

Ans.a. A Class cannot override both hashcode and equals method.

 Q27. Which of the following is true for == operator ?Core Java
a. For primitives, == checks if the variables on left and right have same data type
b. For primitives, == checks if the variables on left and right have same value
c. For Objects, == checks if the references on left and right have same data type
d. For Objects, == checks if the references on left and right have same value

Ans.b. For primitives, == checks if the variables on left and right have same value


Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: