ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
class Box {
    int value;
    Box(int v) { value = v; }
}
public class Main {
    public static void main(String[] args) {
        Box b1 = new Box(5);
        Box b2 = new Box(5);
        System.out.print(b1.equals(b2) + " " + (b1 == b2));
    }
}
  1. A. true false
  2. B. true true
  3. C. false false
  4. D. false true

Correct answer: C. false false

Box never overrides equals(), so it inherits Object.equals(), which is purely identity comparison (==). b1 and b2 are two distinct objects, so both b1.equals(b2) and b1 == b2 are false, regardless of having the same value field.

More OOP with C++ and Java questions

Exams that ask this topic