C. obj1 and obj2 are different objects, so test should pass
D. obj1 and obj2 refer to the same object, so assertNotSame fails
Solution
Step 1: Check references of obj1 and obj2
Both variables point to the same object because obj2 = obj1 assigns the reference.
Step 2: Understand assertNotSame behavior
assertNotSame expects different objects; since references are same, assertion fails.
Final Answer:
obj1 and obj2 refer to the same object, so assertNotSame fails -> Option D
Quick Check:
Same reference fails assertNotSame [OK]
Hint: assertNotSame fails if references point to same object [OK]
Common Mistakes:
Assuming assertNotSame checks content equality
Thinking assertNotSame syntax is wrong
Believing assertNotSame cannot test Object instances
5. Given the following code:
class Person {
String name;
Person(String name) { this.name = name; }
}
Person p1 = new Person("Alice");
Person p2 = new Person("Alice");
Person p3 = p1;
assertSame(p1, p3);
assertNotSame(p1, p2);
Which statement about these assertions is true?
hard
A. assertSame(p1, p3) fails because objects have same content but different references; assertNotSame(p1, p2) passes
B. assertSame(p1, p3) passes because p1 and p3 reference the same object; assertNotSame(p1, p2) passes because p1 and p2 are different objects even if names match
C. assertSame(p1, p3) passes; assertNotSame(p1, p2) fails because names are equal
D. Both assertions fail because Person objects are not immutable
Solution
Step 1: Analyze references of p1 and p3
p3 is assigned from p1, so both point to the same object; assertSame(p1, p3) passes.
Step 2: Analyze references of p1 and p2
p2 is a new Person object with same name but different reference; assertNotSame(p1, p2) passes.
Final Answer:
assertSame(p1, p3) passes; assertNotSame(p1, p2) passes because references differ -> Option B
Quick Check:
assertSame checks references, not content [OK]
Hint: assertSame checks references; assertNotSame passes if objects differ [OK]
Common Mistakes:
Confusing object content equality with reference equality
Assuming assertNotSame fails if object fields match
Thinking assertSame fails if objects are mutable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.