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