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