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