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