ProgrammingVery hardTCS NQT · Infosys pattern

What is printed?

What is printed?
// in main:
Integer a = 100;
Integer b = 100;
Integer c = 200;
Integer d = 200;
System.out.println((a == b) + " " + (c == d));
  1. A. true true
  2. B. true false
  3. C. false true
  4. D. false false

Correct answer: B. true false

Autoboxing uses Integer.valueOf, which caches instances for values -128 to 127. 100 falls in that range, so a and b are the same cached object (==true). 200 is outside the cache range, so c and d are distinct boxed objects (==false), even though their .equals() would agree.

More OOP with C++ and Java questions

Exams that ask this topic