Aliased Score Tracking
A program uses a ScoreKeeper class to manage player points, where two different references are assigned to the same object instance.
What is printed when the following Java program is compiled and run?
class ScoreKeeper {
private int points;
public void add(int p) {
points += p;
}
public int getPoints() {
return points;
}
}
public class QuestionTrace {
public static void main(String[] args) {
ScoreKeeper s1 = new ScoreKeeper();
ScoreKeeper s2 = s1;
s1.add(5);
s2.add(3);
System.out.print(s1.getPoints());
}
}
A
3
B
0
C
5
D
8
Question Leaderboard
Not enough data yet to show leaderboard.
