Analyze the following code: public class Test {public static void main(String[] args) {int[] a = new int[4];a[1] = 1;a = new int[2];System.out.println("a[1] is " + a[1]);} }
A. The program has a compile error because new int[2] is assigned to A.
B. The program displays a[1] is 1.
C. The program has a runtime error because a[1] is not initializeD.
D. The program displays a[1] is 0.
查看答案
Analyze the following code: public class Test { public static void main(String[] args) { int[] x = {0, 1, 2, 3, 4, 5};xMethod(x, 5);} public static void xMethod(int[] x, int length) { for (int i = 0; i < length; i++)System.out.print(" " + x[i]);} }
A. The program displays 0 1 2 3 4.
B. The program displays 0 1 2 3 4 5.
C. The program displays 0 1 2 3 4 and then raises a runtime exception.
D. The program displays 0 1 2 3 4 5 and then raises a runtime exception.
The reverse method is defined in this section. What is list1 after executing the following statements? int[] list1 = {1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1);
A. list1 is 1 2 3 4 5 6
B. list1 is 0 0 0 0 0 0
C. list1 is 6 6 6 6 6 6
D. list1 is 6 5 4 3 2 1
In the following code, what is the printout for list1? public class Test {public static void main(String[] args) {int[] list1 = {1, 2, 3};int[] list2 = {1, 2, 3};list2 = list1;list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list1.length; i++)System.out.print(list1[i] + " ");} }
A. 0 1 3
B. 1 1 1
C. 0 1 2
D. 1 2 3
What is the representation of the third element in an array called a?
A. a[3]
B. a[2]
C. a(3)
D. a(2)