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