Java Object Reference Comparison
Consider the following compareStudents method.
public boolean compareStudents(Student s1, Student s2) {
return s1 == s2 && s1.getName().equals(s2.getName());
}
Assume that s1 and s2 are non-null Student objects and that the getName method returns a non-null String. Under which of the following conditions will the method return true?
A
When s1 and s2 are different objects but have the same name
B
Only when s1 and s2 reference the same Student object
C
Never, because the expression contains a logical error
D
When s1 and s2 have the same name, regardless of whether they’re the same object
Question Leaderboard
Not enough data yet to show leaderboard.
