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