Core Java - Interview Questions and Answers for '==' | 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

   



Core Java - Interview Questions and Answers for '==' - 10 question(s) found - Order By Rating

  Q1. Difference between == and === ?JavaScript
Ans. == compares values === is used in scripting languages for comparing two values as well as there data tpe. like in js,php.

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

   Like         Discuss         Correct / Improve     ==  ===  == vs ===     Asked in 13 Companies      basic        frequent


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


 Q3. 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  ==


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


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


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


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


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


 Q9. What will this code print ?

String a = new String ("TEST");
String b = new String ("TEST");
if(a == b) {
System.out.println ("TRUE");
} else {
System.out.println ("FALSE");
}
Core Java
Ans. FALSE. == operator compares object references, a and b are references to two different objects, hence the FALSE. .equals method is used to compare string object content.

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

   Like         Discuss         Correct / Improve     string   string class   java   ==   object references   coding     Asked in 2 Companies      basic        frequent

Try 1 Question(s) Test


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


 Q11. 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: