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