Given the following code:
public class Chles {
private int qi = 0;
public void doant(int a) {
int dio = 0;
A
qi += a;
goco += a;
dio += a;
System.out.println("qi=" + qi + " goco=" + goco + " dio=" + dio);
}
public static void main(String[] args) {
B
Chles c0 = new Chles();
Chles c1 = new Chles();
c0.doant(1);
c1 = c0;
c1.doant(10);
c0 = new Chles();
c0.doant(100);
c1.doant(1000);
C
}
private static int goco = 0;
}
dio, qi, goco, c0, c1] are in scope at A ?Output:
dio=1 qi=1 goco=1 dio=11 qi=11 goco=10 dio=100 qi=111 goco=100 dio=1011 qi=1111 goco=1000
In scope at A : qi, dio, goco
In scope at B : qi, c0
In scope at C : qi
Explanation (which you do not need to write out in your submitted solution):
qi is a static variable, dio is an instance variable, and goco is a local variable.
At A , 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. dio is out of scope because it is an instance variable, but main is a static method. goco is out of scope because it is local to doant.
At C , c0 and c1 are out of scope because they are not declared yet. dio is out of scope because it is an instance variable, but main is a static method. goco is out of scope because it is local to doant.
Related puzzles: