ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
public class Main {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();
        System.out.print(o1.equals(o2) + " " + (o1.hashCode() == o2.hashCode()));
    }
}
  1. A. false true
  2. B. true true
  3. C. false false
  4. D. true false

Correct answer: C. false false

Object's default equals() is identity comparison (==), and two separately constructed Objects are never the same reference, so equals() is false. Object's default hashCode() is typically derived from the object's identity/memory address, so two distinct objects (almost certainly) get different hash codes too.

More OOP with C++ and Java questions

Exams that ask this topic