Infosys Interview Question for Analysts


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
1
of 1 vote

The output is :
true
true
true

The concept here is the same as String constant pool
Java maintains an integer constant pool for integers upto -128 to 127.
So if you do
{{ Integer i1 =127;
Integer i2 = 127;
System.out.println(i1 == i2);
}}
Then it will give true as it is returning the same object from the constant pool.

In the above Question
{{
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
}}
Both are different object not created in the pool but somewhere else in the heap. So they have different addresses and so hashcode generated for them is different due to which the answer will be false.

For {{System.out.println(i1 <= i2);}}, actual unboxing is happening due to which addresses are not being compared but 1<=1 is being compared.

Instead of ==, we should use equals() method as it properly matches the contents of the two objects and then makes it decisions

- dhirajb1989 May 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(1)Comparing the two refs using '!=' is comparing the two objects' addresses.
(2)Comparing the two refs using '>=' or '<=' is comparing the two integer values(auto unboxing).

- uuuouou May 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But if >= returns true how the other <= can return false if it is comparing the two integer values?

- onlinesoumitra May 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

becuase 1==1

- Anonymous May 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thats pretty pathetic question or may be testing basic java / datatypes.

Here basically all comparison's are object comparison and NOT the content of the object comparison which obviously would be True ( unless you are using Singleton Design Pattern )

- hprem991 May 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The concept here is the same as String constant pool
Java maintains an integer constant pool for integers upto -128 to 127.
So if you do
{{ Integer i1 =127;
Integer i2 = 127;
System.out.println(i1 == i2);
}}
Then it will give true as it is returning the same object from the constant pool.

In the above Question
{{
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
}}
Both are different object not created in the pool but somewhere else in the heap. So they have different addresses and so hashcode generated for them is different due to which the answer will be false.

For {{System.out.println(i1 <= i2);}}, actual unboxing is happening due to which addresses are not being compared but 1<=1 is being compared.

Instead of ==, we should use equals() method as it properly matches the contents of the two objects and then makes it decisions

- dhirajb1989 May 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The output is :
true
true
true

The concept here is the same as String constant pool
Java maintains an integer constant pool for integers upto -128 to 127.
So if you do
{{ Integer i1 =127;
Integer i2 = 127;
System.out.println(i1 == i2);
}}
Then it will give true as it is returning the same object from the constant pool.

In the above Question
{{
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
}}
Both are different object not created in the pool but somewhere else in the heap. So they have different addresses and so hashcode generated for them is different due to which the answer will be false.

For {{System.out.println(i1 <= i2);}}, actual unboxing is happening due to which addresses are not being compared but 1<=1 is being compared.

Instead of ==, we should use equals() method as it properly matches the contents of the two objects and then makes it decisions

- dhirajb1989 May 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hashcode will not be in picture as we aren't talking about hashset or linkedhashset or any collection that uses hashing!

- sharmanalin59 November 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The simple answer is:
- Integer is an Object so when you use == or != it will compare reference. As they are two different instance, the first comparison is true.
- When you use >= or <= operators, the Integer is "unboxed" (meaning that it's value is compared instead of the reference) so both second and third cases would return true.

- Sehs May 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i1 != i2
i1 and i2 are objects created on different memory locations in the heap as new operator is used. As the hashCode's are different, != returns true.

i1 <= i2 and i1 >= i2
Here, i1 is Integer and so is i2. While comparison, they are unboxed to int and hence, return true.

- vijayinani May 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

because the above three conditions the if will evaluates non zero value

- Anonymous May 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

because both i1 and i2 ,hashcode are equal
so all case are equal

- bhanu pratap May 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Wrapper classes behaves same as String class in java when it comes to .equals(),== or != operations.

The wrapper classes(Integer in this case) will behave exactly the same as the String class would have behaved.
Please imagine the String in-place of Integer, and the answer will become crystal clear.

For <= or >= operator, the values will be compared as these operations cannot be done on objects (Common sense :))

- Milind May 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

first line its compares memory address so its always true ,2nd and 3rd also true because it compares instance value.

- swethagovardhan8 May 10, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More