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