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