面向对象编程基础习题及答案 下载本文

public static void main(String[] args) { for (int i = 1; i < 10; i++) { int k = 3;

nPrint(\

}

System.out.println(\ }

A. The code has a syntax error because k is not defined in

System.out.println(\B. The code prints k is 0. C. The code prints k is 1. D. The code prints k is 2. E. The code prints k is 3.

76. Analyze the following code.

public class Test {

public static void main(String[] args) { int n = 2;

xMethod(n);

System.out.println(\ }

void xMethod(int n) { n++; } }

A. The code has a syntax error because xMethod does not return a value.

B. The code has a syntax error because xMethod is not declared static.

C. The code prints n is 1. D. The code prints n is 2.

E. The code prints n is 3.

77. Which of the following is not an advantage of using methods.

A. Using methods makes program run faster. B. Using methods makes reusing code easier.

C. Using methods makes programs easier to read.

D. Using methods hides detailed implementation from the clients.

第 17 页 共 34 页

78. Which of the following is a possible output for 50 * Math.random()?

A. 0 B. 50 C. 100 D. 500

E. A and B.

79. Which of the following method results in 8.0?

A. Math.round(8.5) B. Math.rint(8.5)

C. Math.ceil(8.5) D. Math.floor(8.5) E. b and d.

80. Which of the following method returns the sine of 90 degree?

A. Math.sine(90)

B. Math.sin(90) C. Math.sin(PI)

D. Math.sin(Math.toRadian(90)) E. Math.sin(Math.PI)

81. Suppose array a is int[] a = {1, 2, 3}, what is a[0] - a[2]?

A. 1 B. 2

C. 3

D. None of the above

82. An array can be used in which of the following ways?

A. As a local variable

B. As a parameter of a method C. As a return value of a method D. All of the above

83. Which of the following are valid array declarations?

A. char[] charArray = new char[26];

B. int[] words = new words[10];

C. char[] charArray = \D. double[3] nums = {3.5, 35.1, 32.0}; E. None of the above 84. Consider the following code fragment:

int[] list = new int[10];

for (int i = 0; i <= list.length; i++) { list[i] = (int)(Math.random() * 10); }

Which of the following statements is true? A. list.length must be replaced by 10

第 18 页 共 34 页

B. The loop body will execute 10 times, filling up the array with random numbers.

C. The loop body will execute 10 times, filling up the array with

zeros.

D. The code has a runtime error indicating that the array is out

of bound.

85. Assume the signature of the method xMethod is as follows.

public static void xMethod(double[] a)

Which of the following could be used to invoke xMethod? A. xMethod(5);

B. xMethod({3, 4});

C. xMethod(new int[2]); D. xMethod(new double[2]); E. None of the above.

86. Given the following statement

int[ ] list = new int[10]; list.length has the value

A. 10 B. 9

C. The value depends on how many integers are stored in list. D. None of the above.

87. Given the following statement

int[ ] list = new int[10];

A. The array variable list contains a memory address that refers to an array of 10 int values.

B. The array variable list contains a memory address that refers

to an array of 9 int values. C. The array variable list contains ten values of type int. D. The array variable list contains nine values of type int. E. None of the above. 88. 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]); }

第 19 页 共 34 页

}

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 syntax error because i is not defined in the last statement in the main method.

89. Given the following declaration:

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

Which of the following statements is true?

A. The name m represents a two-dimensional array of 30 int values. B. m[2][4] represents the element stored in the 2nd row and the 4th column of m.

C. m.length has the value 6. D. m[0].length has the value 5. 90. In the following code, what is the printout for list2?

class Test {

public static void main(String[] args) { int[] list1 = {3, 2, 1}; int[] list2 = {1, 2, 3}; list2 = list1;

list1[0] = 0; list1[1] = 1; list2[2] = 2;

for (int i = list2.length - 1; i >= 0; i--) System.out.print(list2[i] + \ } }

A. 1 2 3 B. 3 2 1 C. 0 1 2 D. 2 1 0 E. 0 1 3.

91. Analyze the following code:

public class Test {

public static void main(String[] args) { int[] x = {0, 1, 2, 3, 4, 5}; xMethod(x, 5); }

第 20 页 共 34 页