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