Given the following code:
public class Muson {
public static void main(String[] args) {
A
Muson m0 = new Muson();
Muson m1 = new Muson();
B
m0.tarit(1);
m1 = new Muson();
m0 = m1;
m1.tarit(10);
m0.tarit(100);
m1.tarit(1000);
}
private static int i = 0;
public void tarit(int e) {
int tror = 0;
so += e;
tror += e;
i += e;
System.out.println("so=" + so + " tror=" + tror + " i=" + i);
C
}
private int so = 0;
}
i, so, tror, m0, m1] are in scope at A ?Output:
i=1 so=1 tror=1 i=10 so=10 tror=11 i=110 so=100 tror=111 i=1110 so=1000 tror=1111
In scope at A : tror, m0
In scope at B : tror, m0, m1
In scope at C : tror, i
Explanation (which you do not need to write out in your submitted solution):
tror is a static variable, i is an instance variable, and so is a local variable.
At A , m1 is out of scope because it is not declared yet. i is out of scope because it is an instance variable, but main is a static method. so is out of scope because it is local to tarit.
At B , i is out of scope because it is an instance variable, but main is a static method. so is out of scope because it is local to tarit.
At C , so is out of scope because it is not declared yet. m0 and m1 out of scope because they are local to the main method.
Related puzzles: