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