Which line causes a compilation error?

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?

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.



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Jazz

Jazz

“B”

Error: cannot find symbol
int x1 = x2; // line x2
^

James

James

The Answer is B. The variable x2 has not been declared yet.

James

James

If lines x2 and x3 were switched, the program would successfully compile. Running the program would print 0.