A P  C O M P U T E R  S C I E N C E  A
Unit 2 · Selection and Iteration第 2 单元 · 选择与循环

Selection and Iteration选择与循环

AP-Style MC Practice QuestionsAP 风格选择题练习

MEDIUM HARD AP MC

Unit 2: Selection and Iteration2 单元:选择与循环CSA



Name:姓名:Date:日期:

Draft v2 — 15 AP-style MCQs at exam level (3 MED + 12 HARD); code-trace heavy with realistic distractors. HARDs upgraded: flag-controlled while exits, OR + guarded decrements, log₂ counter, if-else-if vs independent ifs, modular pair counting, conditional shrink. Assumes top-score target. No calculator. Java Quick Reference assumed available.v2 草稿 —— 15 道 AP 风格选择题(MC),考试难度(3 道 MED + 12 道 HARD);偏重代码追踪(code trace),干扰项贴近真题。HARD 题目升级方向:标志变量控制 while 退出、OR 配合带条件的自减、log₂ 计数循环、if-else-if 与独立 if 的差异、对模计数、条件性缩减。面向冲击 5 分的考生。不允许使用计算器。假定考生可使用 Java Quick Reference。

MULTIPLE CHOICE15 questions · ~34 min at exam pace15 道题 · 考试节奏约 34 分钟

Multiple Choice选择题(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"("考虑以下代码段")默认代码可以正常编译并运行(不抛出异常),除非题目另有说明。

Q1MEDIUM AP MC 2.5 Short-Circuit && [1]

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。)

Q2MEDIUM AP MC 2.8 for Loop Count [1]

Consider the following code segment.考虑以下代码段。

int count = 0;
for (int i = 5; i <= 20; i += 3) {
  count++;
}
System.out.println(count);

What is printed?输出是什么?

Q3MEDIUM AP MC 2.6 De Morgan's Law [1]

Which of the following expressions is logically equivalent to !(a < b || c >= d)?下列哪个表达式与 !(a < b || c >= d) 在逻辑上等价?(提示:使用德摩根定律 De Morgan's Law。)

Q4HARD AP MC 2.4 Nested if Trace [1]

Consider 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?输出是什么?

Q5HARD AP MC 2.9 Digit Sum [1]

Consider 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"循环。)

Q6HARD AP MC 2.10 String Traverse — Case Trap [1]

Consider 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。)

Q7HARD AP MC 2.7 while — Flag-controlled exit + counter [1]

Consider 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 与计数器一起更新。)

Q8HARD AP MC 2.11 Nested Loop Count [1]

Consider 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 的三角形计数。)

Q9HARD AP MC 2.9 Reverse Digits [1]

Consider 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"循环。)

Q10HARD AP MC 2.7 while — OR + guarded decrements [1]

Consider 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"如何防止越界。)

Q11HARD AP MC 2.7 while Threshold [1]

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"。)

Q12HARD AP MC 2.8 for — Integer-division step (log₂ counter) [1]

Consider 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₂ 计数器。)

Q13HARD AP MC 2.4 if-else-if chain vs independent ifs [1]

Consider 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 的区别。)

Q14HARD AP MC 2.11 Nested loop — modular pair counting [1]

Consider 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?输出是什么?(注意 ji 开始,所以是"对模计数 modular pair counting",每对只计一次。)

Q15HARD AP MC 2.7 while — Conditional shrink (halve/decrement) [1]

Consider 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"。)