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