Question 1
What is the result of compiling the following program?
public class Main {
public static void main(String[] args) {
int[] x = new int[];
System.out.println(x.length);
}
}A. 0
Assumes the code compiles and prints a default length of 0; without a size or initializer the array creation never compiles.
B. Compilation failsCorrect answer
new int[]supplies neither a dimension nor an initializer, so the compiler cannot determine a length and rejects the code (JLS 8 §15.10.1).C. It compiles and length defaults to 0
There is no default-to-zero-length rule for an array creation missing its size; the code fails to compile rather than defaulting.
D. An exception is thrown at runtime
The error is caught at compile time, so the program never runs and no exception is thrown.
Explanation
new int[] must be told its size — either an explicit dimension (new int[3]) or an initializer (new int[]{1, 2}). With neither, the array's length is unknowable and the compiler rejects it.