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