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