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