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