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