Analyze the following code. public class Test {public static void main(String[] args) {int[] x = new int[3];System.out.println("x[0] is " + x[0]);} }
A. The program has a runtime error because the array element x[0] is not defineD.
B. The program runs fine and displays x[0] is 0.
C. The program has a compile error because the size of the array wasn't specified when declaring the array.
D. The program has a runtime error because the array elements are not initializeD.
查看答案
Analyze the following code: public class Test1 { public static void main(String[] args) {xMethod(new double[]{3, 3});xMethod(new double[5]);xMethod(new double[3]{1, 2, 3});} public static void xMethod(double[] a) { System.out.println(A.length);} }
A. The program has a runtime error because a is null.
B. The program has a compile error because xMethod(new double[5]) is incorrect.
C. The program has a compile error because xMethod(new double[]{3, 3}) is incorrect.
D. The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect.
Analyze the following code: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for (int i = 0; i < x.length; i++)System.out.print(x[i] + " ");} }
A. The program displays 1 2 3 4
B. The program displays 0 0 3 4
C. The program displays 0 0 0 0
D. The program displays 0 0
Analyze the following code: public class Test {public static void main(String[] args) {double[] x = {2.5, 3, 4};for (double value: x)System.out.print(value + " ");} }
A. The program has a syntax error because value is undefineD.
B. The program displays 2.5 3.0 4.0
C. The program displays 2.5, 3, 4
D. The program displays 2.5, 3.0 4.0
Analyze the following code: public class Test { public static void main(String[] args) { int[] x = new int[5]; for (int i = 0; i < x.length; i++)x[i] = i;System.out.println(x[i]);} }
A. The program displays 4.
B. The program has a syntax error because i is not defined in the last statement in the main methoD.
C. The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBounds exception.
D. The program displays 0 1 2 3 4.