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