JAVA题库——数组知识练习

目录

一. 单选题

1. (单选题)How many elements are in array matrix (int[][] matrix = new int[5][5])?

2. (单选题)When you create an array using the following statement, the element values are automatically initialized to 0.int[][] matrix = new int[5][5];

3. (单选题)Analyze the following code.int[] list = new int[5]; list = new int[6];

4. (单选题)Analyze the following code:public class Test {public static void main(String[] args) { int[] x = new int[5];int i;for (i = 0; i < x.length; i++) x[i] = i; System.out.println(x[i]);}}

5. (单选题)Assume double[][][] x = new double[4][5][6], what are x.length, x[2].length, and x[0][0].length?

6. (单选题)Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))?

7. (单选题)How can you get the word "abc" in the main method from the following call?java Test "+" 3 "abc" 2

8. (单选题)What is the output of the following program?public class Test {public static void main(String[] args) { int[][] values = { {3, 4, 5, 1}, {33, 6, 1, 2 } } ;int v = values[0][0]; for (int[] list : values) for (int element : list)if (v > element) v = element;System.out.print(v);}}

9. (单选题)What is the output of the following code? public class Test5 {public static void main(String[] args) {int[][] matrix ={ {1, 2, 3, 4},{4, 5, 6, 7},{8, 9, 10, 11},{12, 13, 14, 15 } } ;for (int i = 0; i < 4; i++) System.out.print(matrix[1][i] + " ");}}

10. (单选题)Which correctly creates an array of five empty Strings?

11. (单选题)What is the index variable for the element at the first row and first column in array a?

12. (单选题)Assume int[][] x = { {1, 2}, {3, 4, 5}, {5, 6, 5, 9}}, what are x[0].length, x[1].length, and x[2].length?

13. (单选题)What would be the result of attempting to compile and run the following code?public class Test {public static void main(String[] args) { double[] x = new double[]{1, 2, 3}; System.out.println("Value is " + x[1]);}}

14. (单选题)Analyze the following code:public class Test {public static void main(String[] args) { int[] oldList = {1, 2, 3, 4, 5}; reverse(oldList);for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " ");}public static void reverse(int[] list) { int[] newList = new int[list.length];for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i];list = newList;}}

15. (单选题)What is output of the following code:public class Test {public static void main(String[] args) { int[] x = {120, 200, 016};for (int i = 0; i < x.length; i++) System.out.print(x[i] + " ");}}

16. (单选题)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 < y.length; i++) System.out.print(y[i] + " ");}}

17. (单选题)What is the output of the following program?public class Test {public static void main(String[] args) { int[][] values = { {3, 4, 5, 1}, {33, 6, 1, 2 } } ;for (int row = 0; row < values.length; row++) { System.out.print(m(values[row]) + " ");}}public static int m(int[] list) { int v = list[0];for (int i = 1; i < list.length; i++) if (v < list[i])v = list[i]; return v;}}

18. (单选题)Suppose a method p has the following heading. What return statement may be used in p()? public static int[][] p()

19. (单选题)What is the output of the following code?public class Test {public static void main(String[] args) {int[][][] data = { { {1, 2}, {3, 4}},{ {5, 6}, {7, 8} } } ;System.out.print(ttt(data[0]));}public static int ttt(int[][] m) { int v = m[0][0];for (int i = 0; i < m.length; i++)for (int j = 0; j < m[i].length; j++) if (v < m[i][j])v = m[i][j];

return v;}}

20. (单选题)Assume double[][] x = new double[4][5], what are x.length and x[2].length?

21. (单选题)When you return an array from a method, the method returns .

22. (单选题)Analyze the following code:public class Test {public static void main(String[] args) { boolean[][] x = new boolean[3][];x[0] = new boolean[1]; x[1] = new boolean[2]; x[2] = new boolean[3];System.out.println("x[2][2] is " + x[2][2]);}}

23. (单选题)In the following code, what is the output for list2?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 < list2.length; i++) System.out.print(list2[i] + " ");}}

24. (单选题)The method copies the sourceArray to the targetArray.

25. (单选题)When you pass an array to a method, the method receives .

26. (单选题)Given the following program:public class Test {public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.print(args[i] + " ");}}}What is the output, if you run the program using java Test 1 2 3

27. (单选题)Which of the following statements are correct?

28. (单选题)Assume int[][] x = { {1, 2}, {3, 4}, {5, 6}}, what are x.length are x[0].length?

29. (单选题)What is the output of the following program?public class Test {public static void main(String[] args) { int[][] values = { {3, 4, 5, 1}, {33, 6, 1, 2 } } ;int v = values[0][0];for (int row = 0; row < values.length; row++)for (int column = 0; column < values[row].length; column++) if (v < values[row][column])v = values[row][column];System.out.print(v);}}

二. 多选题

30. (多选题)Which of the following declarations are correct?

一. 单选题

1. (单选题)How many elements are in array matrix (int[][] matrix = new int[5][5])?

  • A. 14
  • B. 20
  • C. 25
  • D. 30

我的答案: C

2. (单选题)When you create an array using the following statement, the element values are automatically initialized to 0.
int[][] matrix = new int[5][5];

  • A. True
  • B. False

我的答案: A

3. (单选题)Analyze the following code.

int[] list = new int[5]; list = new int[6];

  • A. The code has compile errors because the variable list cannot be changed once it is assigned.
  • B. The code has runtime errors because the variable list cannot be changed once it is assigned.
  • C. The code can compile and run fine. The second line assigns a new array to list.
  • D. The code has compile errors because you cannot assign a different size array to list.

我的答案: C

4. (单选题)Analyze the following code:
public class Test {
public static void main(String[] args) { int[] x = new int[5];
int i;
for (i = 0; i < x.length; i++) x[i] = i; System.out.println(x[i]);
}
}

  • A. The program displays 0 1 2 3 4.
  • B. The program displays 4.
  • C. The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.
  • D. The program has a compile error because i is not defined in the last statement in the main method.

我的答案: C

5. (单选题)Assume double[][][] x = new double[4][5][6], what are x.length, x[2].length, and x[0][0].length?

  • A. 4, 5, and 6
  • B. 6, 5, and 4
  • C. 5, 5, and 5
  • D. 4, 5, and 4

我的答案: A

6. (单选题)Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))?

  • A. {1, 20, 30, 40, 50}
  • B. [1, 20, 30, 40, 50]
  • C. {1 20 30 40 50}
  • D. [1 20 30 40 50]

我的答案: B

7. (单选题)How can you get the word "abc" in the main method from the following call?
java Test "+" 3 "abc" 2

  • A. args[0]
  • B. args[1]
  • C. args[2]
  • D. args[3]

我的答案: C

8. (单选题)What is the output of the following program?
public class Test {
public static void main(String[] args) { int[][] values = { {3, 4, 5, 1}, {33, 6, 1, 2 } } ;
int v = values[0][0]; for (int[] list : values) for (int element : list)
if (v > element) v = element;
System.out.print(v);
}
}

  • A. 1
  • B. 3
  • C. 5
  • D. 6
  • E. 33

我的答案: A

9. (单选题)What is the output of the following code? public class Test5 {
public static void main(String[] args) {
int[][] matrix ={ {1, 2, 3, 4},{4, 5, 6, 7},{8, 9, 10, 11},{12, 13, 14, 15 } } ;
for (int i = 0; i < 4; i++) System.out.print(matrix[1][i] + " ");
}
}

  • A. 1 2 3 4
  • B. 4 5 6 7
  • C. 1 3 8 12
  • D. 2 5 9 13
  • E. 3 6 10 14

我的答案: B

10. (单选题)Which correctly creates an array of five empty Strings?

  • A. String[] a = new String [5];
  • B. String[] a = {"", "", "", "", ""};
  • C. String[5] a;
  • D. String[ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);

我的答案: B

11. (单选题)What is the index variable for the element at the first row and first column in array a?

  • A. a[0][0]
  • B. a[1][1]
  • C. a[0][1]
  • D. a[1][0]

我的答案: A

12. (单选题)Assume int[][] x = { {1, 2}, {3, 4, 5}, {5, 6, 5, 9}}, what are x[0].length, x[1].length, and x[2].length?

  • A. 2, 3, and 3
  • B. 2, 3, and 4
  • C. 3, 3, and 3
  • D. 3, 3, and 4
  • E. 2, 2, and 2

我的答案: B

13. (单选题)What would be the result of attempting to compile and run the following code?
public class Test {
public static void main(String[] args) { double[] x = new double[]{1, 2, 3}; System.out.println("Value is " + x[1]);
}
}

  • A. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by
    {1, 2, 3}.
  • B. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3};
  • C. The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0};
  • D. The program compiles and runs fine and the output "Value is 1.0" is printed.
  • E. The program compiles and runs fine and the output "Value is 2.0" is printed.

我的答案: E

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sweep-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值