ProgrammingModerateProduct-company MCQ pattern

What is printed (default JVM settings)?

What is printed (default JVM settings)?
Integer x = 127, y = 127;
Integer p = 128, q = 128;
System.out.print((x == y) + " " + (p == q));
  1. A. true true
  2. B. false false
  3. C. true false
  4. D. false true

Correct answer: C. true false

Autoboxing uses Integer.valueOf, which returns cached objects for -128 to 127, so x and y are the same object. 128 is outside the cache, so p and q are different objects and == compares references. Use equals for wrapper values.

More OOP with C++ and Java questions

Exams that ask this topic