ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
Integer a = 1000;
Integer b = a;
a++;
System.out.print(a + " " + b + " " + (a == b));
  1. A. 1001 1001 true
  2. B. 1001 1000 false
  3. C. 1000 1000 true
  4. D. 1001 1001 false

Correct answer: B. 1001 1000 false

Integer is immutable: a++ unboxes, adds 1 and boxes a NEW Integer into a. b still refers to the old object holding 1000.

More OOP with C++ and Java questions

Exams that ask this topic