Independent Point Trackers
A Tracker object maintains an instance variable representing its location. Two objects are created to ensure that changes to one do not affect the other.
What is printed when the following Java program is compiled and run?
class Tracker {
private int pos;
public Tracker(int p) {
pos = p;
}
public void move(int step) {
pos += step;
}
public int getPos() {
return pos;
}
}
public class QuestionTrace {
public static void main(String[] args) {
Tracker t1 = new Tracker(10);
Tracker t2 = new Tracker(20);
t1.move(5);
System.out.print(t1.getPos() + " " + t2.getPos());
}
}
A
15 20
B
10 20
C
10 25
D
15 25
Question Leaderboard
Not enough data yet to show leaderboard.
