AP-Style MC Practice QuestionsAP 风格选择题练习
Unit 2: Selection and Iteration第 2 单元:选择与循环CSA
Multiple Choice)Choose the best of the four options. Assume all referenced classes and methods are imported / available unless otherwise stated. "Consider the following code segment" implies the code compiles and runs without exception unless the question states otherwise.从四个选项中选择最合适的一个。除非另有说明,假定所有引用的类(class)与方法(method)均已导入且可用。"Consider the following code segment"("考虑以下代码段")默认代码可以正常编译并运行(不抛出异常),除非题目另有说明。
Consider the following code segment.考虑以下代码段。
int x = 0;
if (x != 0 && 10 / x > 0) {
System.out.println("yes");
} else {
System.out.println("no");
}
What is the result of executing the code segment?运行此代码段的结果是什么?(注意 && 的短路求值 short-circuit evaluation。)
yes is printed.被打印。no is printed.被打印。ArithmeticException is thrown (division by zero).抛出 ArithmeticException(除以零)。compile-time error)。Consider the following code segment.考虑以下代码段。
int count = 0;
for (int i = 5; i <= 20; i += 3) {
count++;
}
System.out.println(count);
What is printed?输出是什么?
56715Which of the following expressions is logically equivalent to !(a < b || c >= d)?下列哪个表达式与 !(a < b || c >= d) 在逻辑上等价?(提示:使用德摩根定律 De Morgan's Law。)
a > b && c < da >= b && c < da >= b || c < da > b || c < dConsider the following code segment.考虑以下代码段。
int x = 15;
if (x > 10) {
if (x % 2 == 0) {
System.out.print("A");
} else {
System.out.print("B");
}
} else if (x > 5) {
System.out.print("C");
}
System.out.println("D");
What is printed?输出是什么?
ADBDBCDDConsider the following code segment.考虑以下代码段。
int n = 234;
int s = 0;
while (n > 0) {
s += n % 10;
n /= 10;
}
System.out.println(s);
What is printed?输出是什么?(提示:典型的"数字求和 digit sum"循环。)
69234432Consider the following code segment.考虑以下代码段。
String s = "ProgrAmming";
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
System.out.println(count);
What is printed?输出是什么?(注意:'a' 与 'A' 是不同字符,区分大小写 case-sensitive。)
1234Consider the following code segment.考虑以下代码段。
int n = 100;
int count = 0;
boolean stop = false;
while (n > 0 && !stop) {
count++;
if (count > 5) stop = true;
n -= 10;
}
System.out.println(count + " " + n);
What is printed?输出是什么?(提示:标志控制退出 flag-controlled exit 与计数器一起更新。)
5 505 606 4010 0Consider the following code segment.考虑以下代码段。
int count = 0;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
count++;
}
}
System.out.println(count);
What is printed?输出是什么?(嵌套循环 nested loop 的三角形计数。)
4101624Consider the following code segment.考虑以下代码段。
int n = 1234;
int rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
System.out.println(rev);
What is printed?输出是什么?(典型的"数字反转 reverse digits"循环。)
123443214324320Consider the following code segment.考虑以下代码段。
int a = 5;
int b = 3;
while (a > 0 || b > 0) {
if (a > 0) a--;
if (b > 0) b--;
}
System.out.println(a + " " + b);
What is printed?输出是什么?(注意条件 OR 与"带条件自减 guarded decrement"如何防止越界。)
0 00 -22 0infinite loop)。Consider the following code segment.考虑以下代码段。
int n = 0;
int p = 1;
while (p < 1000) {
p *= 2;
n++;
}
System.out.println(n);
What is printed?输出是什么?(找出使 2^n ≥ 1000 的最小 n,即典型的"阈值循环 threshold loop"。)
91010001024Consider the following code segment.考虑以下代码段。
int n = 100;
int count = 0;
for (int i = n; i > 1; i = i / 2) {
count++;
}
System.out.println(count);
What is printed?输出是什么?(每次 i 整除以 2,相当于一个 log₂ 计数器。)
56750Consider the following code segment.考虑以下代码段。
int x = 7;
String result = "";
if (x > 0) result += "P";
if (x % 2 == 0) result += "E";
else if (x > 5) result += "B";
if (x < 10 && x > 3) result += "M";
System.out.println(result);
What is printed?输出是什么?(注意 if-else-if 链与多个独立 if 的区别。)
PBMPEBMPEBPMConsider the following code segment.考虑以下代码段。
int total = 0;
for (int i = 1; i <= 6; i++) {
for (int j = i; j <= 6; j++) {
if ((i + j) % 3 == 0) total++;
}
}
System.out.println(total);
What is printed?输出是什么?(注意 j 从 i 开始,所以是"对模计数 modular pair counting",每对只计一次。)
67812Consider the following code segment.考虑以下代码段。
int n = 13;
int count = 0;
while (n > 1) {
if (n % 2 == 0) n /= 2;
else n--;
count++;
}
System.out.println(count);
What is printed?输出是什么?(偶则减半、奇则减一的"条件性缩减 conditional shrink"。)
45612