ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
System.out.print((List.of(1, 2).hashCode() == List.of(2, 1).hashCode()) + " "
    + (Set.of(1, 2).hashCode() == Set.of(2, 1).hashCode()));
  1. A. false true
  2. B. true true
  3. C. false false
  4. D. true false

Correct answer: A. false true

List.hashCode is specified as an order-sensitive polynomial (31·h + element), so [1, 2] (994) and [2, 1] (1024) differ. Set.hashCode is the SUM of element hashes, independent of order, consistent with set equality.

More OOP with C++ and Java questions

Exams that ask this topic