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