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