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