Given:
public class ScopeTest1 {
public static void main(String[] args) {
doStuff(); // line x1
int x1 = x2; // line x2
int x2 = j; // line x3
}
static void doStuff() {
System.out.println(j); // line x4
}
static int j;
}
Which line causes a compilation error?
A.
line x1
B.
line x2
C.
line x3
D.
line x4
Explanation:
The variable x2 is used before it has been declared.
“B”
Error: cannot find symbol
int x1 = x2; // line x2
^
The Answer is B. The variable x2 has not been declared yet.
If lines x2 and x3 were switched, the program would successfully compile. Running the program would print 0.
B