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