题库 java_Java题库——Chapter4 循环

1)How many times will the following code print "Welcome to Java"?

int count = 0;while (count < 10) {

System.out.println("Welcome to Java");

count++;

}

A)8 B) 9 C) 0 D) 11 E) 10

2)Analyze the following code. (Choose all that apply.)

int count = 0;while (count < 100) {//Point A

System.out.println("Welcome to Java!");

count++;//Point B

}//Point C

A)count < 100 is always true at Point A

B)count < 100 is always true at Point C

C)count < 100 is always true at Point B

D)count < 100 is always false at Point B

E)count < 100 is always false at Point C

3)How many times will the following code print "Welcome to Java"?

int count = 0;while (count++ < 10) {

System.out.println("Welcome to Java");

}

A)0 B) 9 C) 8 D) 11E) 10

4)How many times will the following code print "Welcome to Java"?

int count = 0;do{

System.out.println("Welcome to Java");

count++;

}while (count < 10);

A)9 B) 0 C) 10D) 11 E) 8

5)How many times will the following code print "Welcome to Java"?

int count = 0;do{

System.out.println("Welcome to Java");

}while (count++ < 10);

A)8 B) 9 C) 0 D) 10 E) 11

6)How many times will the following code print "Welcome to Java"?

int count = 0;do{

System.out.println("Welcome to Java");

count++;

}while (count < 10);

A)10 B) 8 C) 0 D) 11 E) 9

7)What is the value in count after the following loop is executed?

int count = 0;do{

System.out.println("Welcome to Java");

}while (count++ < 9);

System.out.println(count);

A)8 B) 0 C) 10D) 11 E) 9

8)Analyze the following statement:

int count = 0;do{

System.out.println("Welcome to Java");

}while (count++ < 10);

A)The program has a compile error because the adjustment is missing in the for loop.

B)The program compiles and runs fine.

C)The program has a compile error because the control variable in the for loop cannot be of the double type.

D)The program runs in an infinite loop because d<10 would always be true.

9)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;

}

A)Yes B) No

10)What is the output for y?

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

sum+=i;

}

A)12 B) 45 C) 13 D) 11 E) 10

11)What is i after the following for loop?

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

y+=i;

}

A)11  B) 10  C)9  D) undefined

i是局部变量被定义在循环体中,循环体结束后被释放。

12)Is the following loop correct?

for (;  ; );

A)Yes B) No

13)Analyze the following fragment:

double sum = 0;double d = 0;while (d != 10.0) {

d+= 0.1;

sum+= sum +d;

}

A)After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9

B)The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.

C)The program never stops because d is always 0.1 inside the loop.

D)The program does not compile because sum and d are declared double, but assigned with integer value 0.

14)Analyze the following code: (Choose all that apply.)

public classTest {public static voidmain (String args[ ]) {int i = 0;for (i = 0; i < 10; i++);

System.out.println(i+ 4);

}

}

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

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

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

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

尽管for循环行上有分号(;),程序仍然编译,并显示14。

15)How many times are the following loops executed?

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

System.out.println(i* j)

A)10 B) 20C) 45D) 100

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

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

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

初始值为0的求和变量。

17)Will the following program terminate?

int balance = 10;while (true) {if (balance < 9) break;

balance= balance - 9;

}

A)Yes B) No

18)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);

A)7 B) 6C) 5 D) 8

19)What is sum after the following loop terminates?

int sum = 0;int item = 0;do{

item++;

sum+=item;if (sum >= 4) continue;

}while (item < 5);

A)17 B) 15 C) 18 D) 16

20)Will the following program terminate(终止)?

int balance = 10;while (true) {if (balance < 9) continue;

balance= balance - 9;

}

A)Yes B) No

21)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)continueouter;

System.out.println(i*j);

}

}

next:

A)The program terminates.

B)The statement labeled next.

C)The control is in the inner loop, and the next iteration of the inner loop is executed.

D)The control is in the outer loop, and the next iteration of the outer loop is executed.

控件位于外部循环中,执行外部循环的下一个迭代。

22)What is the number of iterations in the following loop:

for (int i = 1; i < n; i++) {//iteration

}

求迭代次数

A)n + 1 B) 2*n C) n - 1 D) n

23)What is the number of iterations in the following loop:

for (int i = 1; i <= n; i++) {//iteration

}

A)n - 1 B) 2*n C) nD) n + 1

24)Suppose the input for number is 9. What is the output from running the following program?

importjava.util.Scanner;public classTest {public static voidmain(String[ ] args) {

Scanner input= newScanner(System.in);

System.out.print("Enter an integer: ");int number =input.nextInt();inti;boolean isPrime = true;for (i = 2; i < number && isPrime; i++) {if (number % i == 0) {

isPrime= false;

}

}

System.out.println("i is " +i);if(isPrime)

System.out.println(number+ " is prime");elseSystem.out.println(number+ " is not prime");

}

}

A)i is 4 followed by 9 is prime

B)i is 4 followed by 9 is not prime

C)i is 3 followed by 9 is prime

D)i is 3 followed by 9 is not prime

本题如果输入的不是素数,那么要找的第一个与输入的数不互质的数的后一个数;如果是素数,就返回输入的数本身。注意i++!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 什么是 Java 异常? Java 异常是指程序执行期间可能发生的错误或异常情况,例如除以零、数组越界、空指针引用等。当这些异常发生时,Java 虚拟机会抛出一个异常对象,并且程序的执行流程将被中断。 2. Java 异常处理机制有哪些关键字和语句? Java 异常处理机制包括以下关键字和语句: - try:用于包含可能会抛出异常的代码块。 - catch:用于捕获指定类型的异常,并在捕获到异常时执行相应的处理代码。 - finally:用于包含无论是否发生异常都需要执行的代码块。 - throw:用于抛出指定的异常对象。 - throws:用于声明可能会抛出指定类型异常的方法。 3. Java 中的异常分为哪几类? Java 中的异常分为两大类:Checked Exception 和 Unchecked Exception。 Checked Exception 是指在编译时就能够检查出来的异常,例如 IOException、ClassNotFoundException 等。程序必须显式地处理这些异常,否则编译不通过。 Unchecked Exception 是指在运行时才能检查出来的异常,例如 NullPointerException、ArrayIndexOutOfBoundsException 等。程序可以选择处理这些异常,但不处理也不会导致编译错误。 4. 请简要说明 try-catch-finally 的执行流程。 当程序执行到 try 块时,Java 会尝试执行其中的代码。如果在 try 块中抛出了异常,则会将异常对象传递给 catch 块进行处理。catch 块会匹配异常类型,如果匹配成功,则执行相应的处理代码。如果 catch 块处理完异常后,程序需要继续执行,则会执行 finally 块中的代码。如果 finally 块中也抛出了异常,则该异常会覆盖 try 或 catch 块中的异常。 如果 try 块中没有抛出异常,则 catch 块不会被执行。如果 finally 块中抛出异常,则该异常会覆盖 try 块中的异常。 5. 什么是异常链? 异常链是指在处理异常时,将一个异常对象作为另一个异常的原因,并将它们组合成一个异常链。这样做的好处是,在抛出异常时可以同时传递多个异常信息,从而更加清晰地表示异常发生的原因。 6. 请简要说明 try-with-resources 的作用和使用方法。 try-with-resources 是 Java 7 中引入的语法,用于自动关闭实现了 AutoCloseable 接口的资源。在 try 块中声明需要使用的资源,Java 会在 try 块执行完毕后自动关闭这些资源,无需手动调用 close 方法。 try-with-resources 的语法如下: ``` try (Resource1 r1 = new Resource1(); Resource2 r2 = new Resource2()) { // 使用资源 } catch (Exception e) { // 处理异常 } ``` 7. 请简要说明 Java 中的文本 IO。 Java 中的文本 IO 主要包括两种类:Reader 和 Writer。Reader 用于读取字符流,而 Writer 用于写入字符流。 Java 中常用的 Reader 类包括 InputStreamReader、FileReader 和 BufferedReader,常用的 Writer 类包括 OutputStreamWriter、FileWriter 和 BufferedWriter。这些类提供了各种方法来读取和写入字符流,并且可以处理多种编码格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值