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