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