AP CSA Unit 3 Day 12: Object Equality
Share
Unit 3, Classes & Objects • Cycle 2
Day 12 Advanced Practice • Harder Difficulty
Focus: Object Equality
Hard
Object Equality
Advanced Practice Question
Format: Object Equality
What is the output?
What is the output?
public class Person {
private String name;
public Person(String n) {
name = n;
}
}
// In another class:
Person p1 = new Person("Alice");
Person p2 = new Person("Alice");
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));
Difficulty: Hard |
Topic: Object Equality |
Cycle: 2 (Advanced)
Why This Answer?
== compares references (different objects = false). equals() uses Object's default implementation which also compares references (false). Would need to override equals() to compare names.
Common Mistake
Watch Out!
Thinking equals() automatically compares field values.
AP Exam Strategy
Without overriding equals(), it behaves like == (reference comparison).
Master This Topic
This Cycle 2 HARD question tests object equality. Review Unit 3 concepts to build mastery of classes & objects.
- Understanding object equality
- Tracing code execution accurately
- Avoiding common pitfalls
Ready for More Challenges?
Cycle 2 questions prepare you for the hardest AP CSA exam questions.
Study Games Practice FRQs