面向对象编程基础习题及答案

double, but assigned with integer value 0.

B. The program never stops because d is always 0.1 inside the loop. C. The program may not stop because of the phenomenon referred

to as numerical inaccuracy for operating with floating-point numbers. D. After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9 61. What is y after the following for loop statement is executed?

int y = 0;

for (int i = 0; i < 10; ++i) {

y += 1; }

A. 9 B. 10 C. 11

D. 12

E. None of the above.

62. What balance after the following code is executed?

int balance = 10; while (balance >= 1) {

if (balance < 9) continue; balance = balance - 9; }

A. -1 B. 0 C. 1 D. 2

E. The loop does not end

63. What is the value of balance after the following code is executed?

int balance = 10;

while (balance >= 1) {

if (balance < 9) break; balance = balance - 9; }

A. –1 B. 0 C. 1

第 13 页 共 34 页

D. 2

E. None of the above 64. What is x after evaluating

x = (2 > 3) ? 2 : 3;

A. 2 B. 3 C. 4

D. None of the above

65. The signature of a method consists of ____________.

A. method name

B. method name and parameter list

C. return type, method name, and parameter list D. parameter list

66. A variable that is declared inside a method is called ________ variable.

A. a static B. an instance C. a local

D. a global E. a class

67. Each Java class must contain a main method.

A. true B. false 68. What is (int)Math.random()?

A. 0 B. 1

C. both 0 and 1 are possible D. None of the above 69. Analyze the following code.

public class Test {

public static void main(String[] args) { System.out.println(m(2)); }

public static int m(int num) { return num; }

public static void m(int num) { System.out.println(num); }

第 14 页 共 34 页

}

A. The program has a syntax error because the two methods m have

the same signature.

B. The program has a syntax error because the second m method is defined, but not invoked in the main method. C. The program runs and prints 2 once. D. The program runs and prints 2 twice. 70. Analyze the following code:

class Test {

public static void main(String[] args) { System.out.println(xMethod((double)5)); }

public static int xMethod(int n) { System.out.println(\ return n; }

public static long xMethod(long n) { System.out.println(\ return n; } }

A. The program displays int followed by 5.

B. The program displays long followed by 5.

C. The program runs fine but displays things other than given in

A and B. D. The program does not compile. E. None of the above. 71. Analyze the following code:

public class Test {

public static void main(String[] args) { System.out.println(xMethod(5, 500L)); }

public static int xMethod(int n, long l) { System.out.println(\ return n; }

第 15 页 共 34 页

public static long xMethod(long n, long l) { System.out.println(\ return n; } }

A. The program displays int, long followed by 5. B. The program displays long, long followed by 5.

C. The program runs fine but displays things other than given in

a and b.

D. The program does not compile because the compiler cannot distinguish which xmethod to invoke.

72. You may have a return statement in a void method.

A. true B. false

Note: Questions 73–75 are based on the following method:

static void nPrint(String message, int n) { while (n > 0) {

System.out.print(message); n--; } }

73. What is the printout of the call nPrint(\

A. aaaaa B. aaaa C. aaa

D. invalid call

74. What is k after invoking nPrint(“A message”, k+1)?

int k = 2;

nPrint(\ A. 0 B. 1 C. 2

D. 3

E. None of the above.

75. Analyze the following code.

第 16 页 共 34 页

联系客服:779662525#qq.com(#替换为@)