Independent Counter States
Consider a system where two objects track their own unique integer values independently using a shared class definition.
What is printed when the following Java program is compiled and run?
public class QuestionTrace {
public static void main(String[] args) {
Counter c1 = new Counter(10);
Counter c2 = new Counter(20);
c1.increment();
System.out.print(c1.getValue() + "" + c2.getValue());
}
}
class Counter {
private int value;
public Counter(int start) {
value = start;
}
public void increment() {
value++;
}
public int getValue() {
return value;
}
}
A
1021
B
1120
C
1020
D
1121
Question Leaderboard
Not enough data yet to show leaderboard.
