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