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) 11 E) 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) 10 D) 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) 10 D) 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 class Test {
public static void main (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) 20 C) 45 D) 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) 6 C) 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)
continue outer;
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) n D) n + 1

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

import java.util.Scanner;
public class Test {
public static void main(String[ ] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
int i;
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");
else
System.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++!!!

Java题库——Chapter4 循环的更多相关文章

  1. Java题库——Chapter13抽象类和接口

    )What is the output of running class Test? public class Test { public static void main(String[ ] arg ...

  2. Java题库——Chapter8 对象和类

    1)________ represents an entity(实体) in the real world that can be distinctly identified. 1) _______ ...

  3. JAVA题库01

    说出一些常用的类,包,接口,请各举5个 常用的类:BufferedReader  BufferedWriter  FileReader  FileWirter  String Integer java ...

  4. Java题库——Chapter17 二进制I/0

    Introduction to Java Programming 的最后一章,完结撒花!Chapter 17 Binary I/O Section 17.2 How is I/O Handled in ...

  5. Java题库——Chapter16 JavaFX UI组件和多媒体

    Chapter 16 JavaFX UI Controls and Multimedia Section 16.2 Labeled and Label1. To create a label with ...

  6. Java题库——Chapter14 JavaFX基础

    Chapter 14 JavaFX Basics Section 14.2 JavaFX vs Swing and AWT1. Why is JavaFX preferred?a. JavaFX is ...

  7. Java题库——Chapter12 异常处理和文本IO

    异常处理 1)What is displayed on the console when running the following program? class Test { public stat ...

  8. Java题库——Chapter11 继承和多态

    1)Analyze the following code: public class Test { public static void main(String[ ] args) { B b = ne ...

  9. Java题库——Chapter10 面向对象思考

    1)You can declare two variables with the same name in ________. 1) _______ A)a method one as a forma ...

随机推荐

  1. ELK 理论小知识

    ELK 是现阶段众多企业单位都在使用的一种日志分析系统,它能够方便的为我们收集你想要的日志并且展示出来 ELK是Elasticsearch.Logstash.Kibana的简称,这三者都是开源软件,通 ...

  2. Java入门(一)——类、抽象类和接口

    Java是一门面向对象语言,可以看出"对象"在Java有着举足轻重的位置.那么,"对象"从何而来呢?那必须是丈母娘造出来的,下面我们就先来说说这个丈母娘--类. ...

  3. 浅析堆栈段,BBS段,数据段,代码段

    文章目录 1. 进程,线程 2. 堆栈段 3. BBS段 4. 代码段 5. 数据段 6. 例子 7. 总结 1. 进程,线程 所谓进程是指在系统中能独立运行并作为资源分配的基本单位,程序段,数据段和 ...

  4. VS2019 开发Django(十一)------表单

    导航:VS2019开发Django系列 今天是中华人民共和国成立70周年的日子,普天同庆,看阅兵看得满腔热血,热泪盈眶,祖国都这么优秀了,我要更加努力才行啊! 这个Django系列的文章,没有很深入的 ...

  5. 全平台轻量开源verilog仿真工具iverilog+GTKWave使用教程

    前言 如果你只是想检查Verilog文件的语法是否有错误,然后进行一些基本的时序仿真,那么Icarus Verilog 就是一个不错的选择.相比于各大FPGA厂商的IDE几个G的大小,Icarus V ...

  6. 【GZOI 2019】特技飞行

    Problem Description 公元 \(9012\) 年,Z 市的航空基地计划举行一场特技飞行表演.表演的场地可以看作一个二维平面直角坐标系,其中横坐标代表着水平位置,纵坐标代表着飞行高度. ...

  7. 暴力破解( Hydra | Medusa)

    暴力破解 By : Mirror王宇阳 笔者告知 : 暴力破解的结果是运气和速度的结晶,开始暴力破解前烧一炷香也是必要的! 引用张炳帅的一句话:"你的运气和管理员的安全意识成正比" ...

  8. 关于EXIT和BADI增强的查找

    EXIT出口的查找: 方法一: 第一步:通过SE30,输入TCODE(例如ME21N),执行EXCUTE,前台创建一张采购订单.点击TIMES页签,查找EXIT开头的SAP程序.  第二步:这些fun ...

  9. Client error attempting to change layout margins of a private view

    从 iOS 11 开始,UINavigationBar 使用了自动布局,左右两边的按钮到屏幕之间会有 16 或 20 的边距. 为了避免点击到间距的空白处没有响应,通常做法是:定义一个 UINavig ...

  10. Linux MySQL的root无法登录数据库ERROR 1045 (28000)

    Linux环境下,脚本自动安装完数据库,命令行用mysql -uroot -ppasswaord 登录却报了这么个错: ERROR 1045 (28000): Access denied for us ...