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