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