ProgrammingModerateTCS NQT · Infosys

What does this Java code print? Integer a = 127, b = 127, c = 128, d = 128; System.out.println((a == b) + " " + (c == d) + " " + c.equals(d));

  1. A. true false true
  2. B. true true true
  3. C. false false true
  4. D. true false false

Correct answer: A. true false true

Autoboxing uses Integer.valueOf, and the language guarantees a cache for values -128 to 127, so a == b is true. 128 creates two separate objects, so c == d is false, while equals compares the values and is true.

More OOP with C++ and Java questions

Exams that ask this topic