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