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