Java练习题1(有答案) 下载本文

第 49题 Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2:

even = (number % 2 == 0) ? true: false; Code 3: even = number % 2 == 0;

1、 Code 2 has a compile error, because you cannot have true and false literals in the conditional expression.

2、 Code 3 has a compile error, because you attempt to assign number to even.

3、 All three are correct, but Code 1 is preferred. 4、 All three are correct, but Code 2 is preferred. 5、 All three are correct, but Code 3 is preferred.

答案 5 第 50题 The statement System.out.printf(\1234.56) outputs ___________. 1、 0.1e+04 2、 0.123456e+04 3、 0.123e+04

4、 1.2e+03 5、 1.23+03 答案 4 第 51题 Analyze the following code: int i = 3434; double d = 3434; System.out.printf(\

1、 The code compiles and runs fine to display 3434.0 3434.0. 2、 The code compiles and runs fine to display 3434 3434.0.

3、 i is an integer, but the format specifier %5.1f specifies a format for double value. The code has an error.

答案 3 第 52题 What is the value of the following expression?

true || true && false 1、 true

2、 false 答案 1 第 53题 Which of the following statements are true? 1、 (x > 0 && x < 10) is same as ((x > 0) && (x < 10))

2、 (x > 0 || x < 10) is same as ((x > 0) || (x < 10))

3、 (x > 0 || x < 10 && y < 0) is same as (x > 0 || (x < 10 && y < 0)) 4、 (x > 0 || x < 10 && y < 0) is same as ((x > 0 || x < 10) && y < 0) 答案 1 2 3 第 54题 How many times will the following code print \ int count = 0; while (count < 10) {

System.out.println(\count++; } 1、 8 2、 9 3、 10

4、 11 5、 0 答案 3 第 55题 Analyze the following code. int count = 0; while (count < 100) { // Point A

System.out.println(\count++; // Point B }

// Point C

1、 count < 100 is always true at Point A 2、 count < 100 is always true at Point B 3、 count < 100 is always false at Point B

4、 count < 100 is always true at Point C 5、 count < 100 is always false at Point C 答案 1 5 第 56题 How many times will the following code print \ int count = 0; do {

System.out.println(\ } while (count++ < 10); 1、 8 2、 9 3、 10

to

Java!\

to

Java\

4、 11 5、 0 答案 4 第 57题 What is the value in count after the following loop is executed? int count = 0; do {

System.out.println(\ } while (count++ < 9); System.out.println(count); 1、 8 2、 9 3、 10

4、 11 5、 0 答案 3 第 58题 Do the following two statements in (I) and (II) result in the same value in sum? (I): for (int i = 0; i<10; ++i) { sum += i;

} (II): for (int i = 0; i<10; i++) { sum += i; }

1、 Yes 2、 No 答案 1 第 59题 Is the following loop correct? for (; ; ); 1、 Yes 2、 No 答案 1 第 60题 Analyze the following code: public class Test { public static void main (String args[]) { int i = 0; for

(i

=

0;

i

<

10;

i++);

System.out.println(i + 4); } }

1、 The program has a compile error because of the semicolon (;) on the for loop line.

2、 The program compiles despite the semicolon (;) on the for loop line, and displays 4.

3、 The program compiles despite the semicolon (;) on the for loop line, and displays 14.

4、 The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4); 答案 3 4

第 61题 To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?

1、 add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.

2、 add 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose initial value is 0.

答案 1 第 62题 What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; sum += item; if (sum > 4) break;

} while (item < 5); 1、 5 2、 6

3、 7 4、 8 答案 2 第 63题 After the continue outer statement is executed in the following loop, which statement is executed? outer: for (int i = 1; i < 10; i++) { inner:

for (int j = 1; j < 10; j++) { if (i * j > 50) continue outer; System.out.println(i * j); } } next:

1、 The control is in the outer loop, and the next iteration of the outer loop is executed.

2、 The control is in the inner loop, and the next iteration of the inner loop is executed.

3、 The statement labeled next.

4、 The program terminates. 答案 1 第 64题 Suppose the input for number is 9. What is the output from running the following program? import java.util.Scanner; public class Test {