Lamp State Independence
Two separate Lamp objects demonstrate how instance fields maintain unique states independently of other instances.
What is printed when the following Java program is compiled and run?
public class QuestionTrace {
public static void main(String[] args) {
Lamp l1 = new Lamp();
Lamp l2 = new Lamp();
l1.toggle();
System.out.print(l1.isOn() + "" + l2.isOn());
}
}
class Lamp {
private boolean on;
public void toggle() {
on = !on;
}
public boolean isOn() {
return on;
}
}
A
truetrue
B
falsetrue
C
truefalse
D
falsefalse
Question Leaderboard
Not enough data yet to show leaderboard.
