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