What is the result of compiling and running the following code?
public class Test{
public static void main(String[] args){
int[] a = new int[0];
System.out.print(a.length);
}
}
public class Test{
public static void main(String[] args){
int[] a = new int[0];
System.out.print(a.length);
}
}
A. 0
B. Compilation error, arrays cannot be initialized to zero size.
C. Compilation error, it is a.length() not a.length
D. None of the above
Answer: Option A
Join The Discussion
Comments ( 1 )
Which one of the following is a valid statement?
A. char[] c = new char();
B. char[] c = new char[5];
C. char[] c = new char(4);
D. char[] c = new char[];
A. 0
B. Compilation error, arrays cannot be initialized to zero size.
C. Compilation error, it is a.length() not a.length
D. None of the above
A. The program has a compile error because the size of the array wasn't specified when declaring the array.
B. The program has a runtime error because the array elements are not initialized.
C. The program runs fine and displays x[0] is 0.
D. The program has a runtime error because the array element x[0] is not defined.
As we know counting is done from 1 when length is calculated. then why answer is 0.