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