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