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