Independent Object States
Two distinct objects are created and modified separately, demonstrating the independence of their instance variables.
What is printed when the following Java program is compiled and run?
public class QuestionTrace {
public static void main(String[] args) {
Item i1 = new Item(10);
Item i2 = new Item(20);
i1.add(5);
System.out.print(i1.get() + i2.get());
}
}
class Item {
private int val;
public Item(int v) {
val = v;
}
public void add(int v) {
val = val + v;
}
public int get() {
return val;
}
}
A
35
B
30
C
40
D
15
Question Leaderboard
Not enough data yet to show leaderboard.
