Given the following code:
public class Sahis {
public static void main(String[] args) {
A
Sahis s0 = new Sahis();
Sahis s1 = new Sahis();
s0.preal(1);
s1.preal(10);
s0 = new Sahis();
s0.preal(100);
s1 = s0;
s1.preal(1000);
B
}
private int acco = 0;
public void preal(int o) {
C
int e = 0;
e += o;
i += o;
acco += o;
System.out.println("e=" + e + " i=" + i + " acco=" + acco);
}
private static int i = 0;
}
acco, e, i, s0, s1] are in scope at A ?Output:
acco=1 e=1 i=1 acco=10 e=11 i=10 acco=100 e=111 i=100 acco=1000 e=1111 i=1100
In scope at A : e, s0
In scope at B : e
In scope at C : e, i, acco
Explanation (which you do not need to write out in your submitted solution):
e is a static variable, i is an instance variable, and acco is a local variable.
At A , s1 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. acco is out of scope because it is local to preal.
At B , s0 and s1 are out of scope because they are not declared yet. i is out of scope because it is an instance variable, but main is a static method. acco is out of scope because it is local to preal.
At C , s0 and s1 out of scope because they are local to the main method.
Related puzzles: