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