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