ProgrammingVery hardProduct-company placement · GATE CS

What is printed?

What is printed?
Long a = 1L;
System.out.print(a.equals(1) + " " + a.equals(1L) + " " + (a == 1));
  1. A. true true true
  2. B. false true false
  3. C. false true true
  4. D. true true false

Correct answer: C. false true true

a.equals(1) boxes 1 to an Integer, and Long.equals returns false for anything that is not a Long. a == 1 compares a boxed value with a primitive, so a is unboxed and the numeric comparison is true.

More OOP with C++ and Java questions

Exams that ask this topic