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