ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
Boolean b1 = true,  b2 = true;
Character c1 = 'a', c2 = 'a';
Short s1 = 100,     s2 = 100;
System.out.print((b1 == b2) + " " + (c1 == c2) + " " + (s1 == s2));
  1. A. true false false
  2. B. true true true
  3. C. false false false
  4. D. true true false

Correct answer: B. true true true

The JLS guarantees that boxing true/false, a char in \u0000–\u007f, or a short/int in −128..127 always yields the same object, so == compares equal references here.

More OOP with C++ and Java questions

Exams that ask this topic