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