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