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