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