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