Companion to the Practice Set · Mark-by-mark walkthroughs · AP CSP-Feeder / US / ON / BC / AB styles练习题配套详解 · 逐分讲解 · AP CSP 衔接 / 美 / 安 / 卑 / 阿省考风格
Which Boolean expression evaluates to True when x = 7?当 x = 7 时,哪个布尔表达式的值为 True?
x > 5 and x < 10答案:(B) - x > 5 and x < 10Evaluate each option with x = 7: [1]将 x = 7 代入每个选项求值:[1]
x == 6 → 7 == 6 → False. Incorrect.x == 6 → 7 == 6 → False。不正确。x > 5 and x < 10 → 7 > 5 is True AND 7 < 10 is True → True and True → True. Correct. [1]x > 5 and x < 10 → 7 > 5 为 True 且 7 < 10 为 True → True and True → True。正确。[1]x != 7 → 7 != 7 → False. Incorrect.x != 7 → 7 != 7 → False。不正确。x > 10 or x < 3 → 7 > 10 is False OR 7 < 3 is False → False or False → False. Incorrect. [1]x > 10 or x < 3 → 7 > 10 为 False 或 7 < 3 为 False → False or False → False。不正确。[1]and operator requires BOTH sub-conditions to be True simultaneously. Here, 7 lies strictly between 5 and 10, satisfying both halves. Option (D) is a common trap: or needs only one side True, but neither side is True for x = 7.and 运算符要求两个子条件同时为 True。这里 7 严格介于 5 和 10 之间,两个子条件均满足。选项 (D) 是常见陷阱:or 只需一边为 True,但 x = 7 时两边均为 False。What does the following program output when score = 75?当 score = 75 时,以下程序输出什么?
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("F")
C答案:(C) - 输出为 CTrace the if/elif chain top to bottom with score = 75: [1]以 score = 75 从上到下追踪 if/elif 链:[1]
score >= 90 → 75 >= 90 → False, skip. score >= 80 → 75 >= 80 → False, skip. [1] score >= 70 → 75 >= 70 → True, execute print("C"). The remaining branches are skipped. [1]score >= 90 → 75 >= 90 → False,跳过。score >= 80 → 75 >= 80 → False,跳过。[1] score >= 70 → 75 >= 70 → True,执行 print("C"),其余分支跳过。[1]
>= 70 but also technically satisfies lower thresholds, which do not matter here because the chain stops at the first match.在 if/elif/else 链中,只有第一个条件为 True 的分支会执行。一旦某个分支触发,Python 会跳过所有后续分支。75 满足 >= 70,但链式结构在首次匹配处停止,后续条件不再检查。Trace the nested conditional below with age = 15 and height = 130.以 age = 15、height = 130 追踪以下嵌套条件。
IF age >= 12 THEN
IF height >= 140 THEN
OUTPUT "Welcome!"
ELSE
OUTPUT "Too short."
END IF
ELSE
OUTPUT "Too young."
END IF
Output: Too short. [1]输出:Too short. [1]
The outer condition age >= 12 is checked first: 15 >= 12 is True, so execution enters the THEN block. [1] Inside that block, the inner condition height >= 140 is reached: 130 >= 140 is False, so the ELSE branch fires and outputs "Too short." [1]首先检查外层条件 age >= 12:15 >= 12 为 True,故进入 THEN 块。[1] 在该块内,内层条件 height >= 140 被检查:130 >= 140 为 False,故执行 ELSE 分支,输出 "Too short."。[1]
Output: Too young. - because 10 >= 12 is False so the outer ELSE fires and the inner conditional is never reached. [1]输出:Too young. - 因为 10 >= 12 为 False,外层 ELSE 分支触发,内层条件不会被执行到。[1]
Consider the pseudocode below.考察以下伪代码。
SET n TO 0
WHILE n < 5:
SET n TO n + 2
OUTPUT n
| Iteration迭代 | n before body循环体前 n | Condition (n < 5)?条件(n < 5)? | n after body循环体后 n |
|---|---|---|---|
| 1 | 0 | True | 2 |
| 2 | 2 | True | 4 |
| 3 | 4 | True | 6 |
After iteration 3: n = 6. Check condition: 6 < 5 → False. Loop exits. [3]第 3 次迭代后:n = 6。检查条件:6 < 5 → False。循环退出。[3]
OUTPUT n prints 6. [1]OUTPUT n 打印 6。[1]
The loop body executes 3 times. [1] Justification: n starts at 0 and increments by 2 each time (0 → 2 → 4 → 6). The condition n < 5 is True for n = 0, 2, 4 and False for n = 6, giving exactly 3 iterations. [1]循环体执行 3 次。[1] 理由:n 从 0 开始,每次加 2(0 → 2 → 4 → 6)。条件 n < 5 在 n = 0, 2, 4 时为 True,n = 6 时为 False,恰好 3 次迭代。[1]
Change the condition to n < 4. Then: iteration 1 (n=0, True, n becomes 2), iteration 2 (n=2, True, n becomes 4), check: 4 < 4 → False, exit. Exactly 2 iterations. [1]将条件改为 n < 4。则:第 1 次(n=0,True,n 变为 2),第 2 次(n=2,True,n 变为 4),检查:4 < 4 → False,退出。恰好 2 次。[1]
A student writes the following program to print multiples of 2 from 2 to 6 inclusive.一名学生编写以下程序,打印 2 到 6(含)的 2 的倍数。
for i in range(2, 8, 2):
print(i)
range(2, 8, 2) generates values starting at 2, stepping by 2, stopping before 8: 2, 4, 6. [2]range(2, 8, 2) 从 2 开始,步长 2,在 8 之前停止,生成:2, 4, 6。[2]
2 4 6
[2] (1 mark per correct line; all three required for full marks)[2](每行正确得 1 分;三行全对才能得满分)
Error: range(1, 10) stops before 10, so 10 is not included; the output would be 1 through 9 only. [1] Correction: use range(1, 11) so the stop value is 11, which causes Python to include 10. [1]错误:range(1, 10) 在 10 之前停止,因此 10 不被包含,输出仅为 1 到 9。[1] 纠正:改用 range(1, 11),停止值为 11,Python 因此会包含 10。[1]
A for loop is preferred when the number of iterations is known in advance (or when iterating over a defined sequence). [1] Example scenario: printing each student's name from a class list of 30 students; the count is fixed so for name in students: is cleaner than maintaining a counter in a while loop. [1]当迭代次数事先已知(或遍历已定义的序列)时,for 循环更合适。[1] 应用场景举例:打印 30 名学生名单中的每个名字,次数固定,for name in students: 比在 while 循环中维护计数器更简洁。[1]
range(start, stop, step): start is inclusive, stop is exclusive. The most common mistake is forgetting that the stop value is excluded, causing an off-by-one error. Always set stop = desired_last_value + 1 for inclusive counting.range(start, stop, step):start 含,stop 不含。最常见的错误是忘记 stop 值不被包含,导致差一错误。对于含端点计数,始终将 stop 设为所需最大值加一。Study the two programs below.研究以下两个程序。
numbers = [3, 7, 12, 5, 18]
for n in numbers:
if n > 10:
print("Found:", n)
break
scores = [5, -3, 8, -1, 4]
total = 0
for s in scores:
if s < 0:
continue
total += s
print(total)
| n | n > 10?n > 10? | Action操作 |
|---|---|---|
| 3 | False | continue loop继续循环 |
| 7 | False | continue loop继续循环 |
| 12 | True | print "Found: 12", break打印 "Found: 12",退出 |
Output: Found: 12. [1] The element that caused the loop to exit is 12 (first value > 10). [1] Elements 5 and 18 are never reached. [1]输出:Found: 12。[1] 导致循环退出的元素是 12(第一个大于 10 的值)。[1] 元素 5 和 18 从未被访问到。[1]
| s | s < 0?s < 0? | Action操作 | total after迭代后 total |
|---|---|---|---|
| 5 | False | total += 5total += 5 | 5 |
| -3 | True | continue (skip)continue(跳过) | 5 |
| 8 | False | total += 8total += 8 | 13 |
| -1 | True | continue (skip)continue(跳过) | 13 |
| 4 | False | total += 4total += 4 | 17 |
Final output: 17. [3] (1 mark per correct row group; deduct 1 for each incorrect total value)最终输出:17。[3](每组正确行得 1 分;total 值每错一处扣 1 分)
break immediately exits the entire loop, skipping all remaining iterations. [1] continue skips only the remainder of the current iteration and jumps to the next one, keeping the loop running. [1]break 立即退出整个循环,跳过所有剩余迭代。[1] continue 仅跳过当前迭代的剩余部分,直接进入下一次迭代,循环继续运行。[1]
break when you have found what you need and further searching is wasteful. Use continue when you want to filter out certain items but still process everything else. Program A searches for the first large number; Program B accumulates only non-negative values.当你找到所需结果、无需继续搜索时,使用 break。当你想过滤掉某些项目、但仍需处理其余项目时,使用 continue。程序 A 查找第一个较大的数;程序 B 只累加非负数。A program uses nested loops to find all pairs (i, j) where i and j are in [1, 3] and i + j == 4.一个程序用嵌套循环找到满足 i + j == 4 的所有整数对 (i, j),其中 i 和 j 均在 [1, 3] 内。
FOR i FROM 1 TO 3:
FOR j FROM 1 TO 3:
IF i + j == 4 THEN
OUTPUT i, j
END IF
END FOR
END FOR
The outer loop runs for i = 1, 2, 3 (3 values). [1] For each value of i, the inner loop runs for j = 1, 2, 3 (3 values). Total checks = 3 x 3 = 9. [1]外层循环对 i = 1, 2, 3 运行(共 3 次)。[1] 每个 i 值对应内层循环对 j = 1, 2, 3 运行(共 3 次)。总检查次数 = 3 x 3 = 9。[1]
| i | j | i + j | == 4?== 4? | Output?是否输出? |
|---|---|---|---|---|
| 1 | 1 | 2 | No | - |
| 1 | 2 | 3 | No | - |
| 1 | 3 | 4 | Yes | (1, 3) |
| 2 | 1 | 3 | No | - |
| 2 | 2 | 4 | Yes | (2, 2) |
| 2 | 3 | 5 | No | - |
| 3 | 1 | 4 | Yes | (3, 1) |
| 3 | 2 | 5 | No | - |
| 3 | 3 | 6 | No | - |
Pairs output: (1, 3), (2, 2), (3, 1). [3] (1 mark per correct pair)输出的对:(1, 3)、(2, 2)、(3, 1)。[3](每对正确得 1 分)
FOR i FROM 1 TO 5 [1]FOR i FROM 1 TO 5 [1]
Each counter tracks a different dimension of the iteration independently. [1] If both loops shared the same variable, advancing the inner loop would overwrite the outer loop's progress, making it impossible to enumerate all combinations of rows and columns correctly. [1]每个计数器独立跟踪迭代的一个维度。[1] 若两个循环共用同一个变量,内层循环的推进会覆盖外层循环的进度,导致无法正确枚举行列的所有组合。[1]
A program accumulates a running total by repeatedly adding integers starting from 1. The loop stops as soon as the total exceeds 20.一个程序通过反复累加从 1 开始的整数来积累累计总和,当总和超过 20 时循环停止。
SET total TO 0
SET val TO 1
WHILE total <= 20:
SET total TO total + val
SET val TO val + 1
OUTPUT total
OUTPUT val
| Iter迭代 | total after迭代后 total | val after迭代后 val | total <= 20?total <= 20? |
|---|---|---|---|
| 1 | 1 | 2 | True |
| 2 | 3 | 3 | True |
| 3 | 6 | 4 | True |
| 4 | 10 | 5 | True |
| 5 | 15 | 6 | True |
| 6 | 21 | 7 | False |
[4] (1 mark per two correct rows; all six rows must be filled)[4](每两行正确得 1 分;六行须全部填写)
OUTPUT total prints 21. [1] OUTPUT val prints 7. [1]OUTPUT total 打印 21。[1] OUTPUT val 打印 7。[1]
Change WHILE total <= 20 to WHILE total <= 50. [1]将 WHILE total <= 20 改为 WHILE total <= 50。[1]
A while loop is more appropriate because the number of iterations is not known in advance; the loop continues until a data-dependent condition (total exceeding the threshold) is met, which cannot be expressed cleanly with a fixed range. [1]while 循环更合适,因为迭代次数事先不可知;循环持续运行直到满足与数据相关的条件(total 超过阈值),这无法用固定的 range 简洁表达。[1]
A program prints a 2-row by 3-column grid of products.一个程序打印 2 行 3 列的乘积网格。
for row in range(1, 3):
for col in range(1, 4):
print(row * col, end=" ")
print()
row = 1: col = 1 prints 1 , col = 2 prints 2 , col = 3 prints 3 , then print() moves to next line. [1]row = 1:col = 1 打印 1 ,col = 2 打印 2 ,col = 3 打印 3 ,print() 换行。[1]
row = 2: col = 1 prints 2 , col = 2 prints 4 , col = 3 prints 6 , then print() moves to next line. [1]row = 2:col = 1 打印 2 ,col = 2 打印 4 ,col = 3 打印 6 ,print() 换行。[1]
1 2 3 2 4 6
(Each number is followed by a space; print() adds a newline after each row.) [1](每个数字后跟一个空格;print() 在每行结束后换行。)[1]
print(row * col, end=" ") executes 2 (rows) x 3 (cols) = 6 times. [1]print(row * col, end=" ") 执行 2(行)x 3(列)= 6 次。[1]
for row in range(1, 5):
for col in range(1, 6):
[1] for the outer loop, [1] for the inner loop. (range stop = desired_last + 1)外层循环 [1],内层循环 [1]。(range 的 stop = 所需最大值 + 1)
Total inner-body executions = m x n. [1]内层循环体总执行次数 = m x n。[1]
end=" " argument suppresses the default newline and replaces it with a space, so all values in a row print on one line. The bare print() at the outer loop level then emits the newline that separates rows. This technique is the standard pattern for printing 2D grids in Python.end=" " 参数抑制了默认换行符,替换为空格,使同一行的所有值打印在一行上。外层循环级别的 print() 负责在行之间输出换行符。这是 Python 打印二维网格的标准技巧。A program counts even numbers in a list and computes their sum.一个程序计算列表中偶数的个数,并计算它们的总和。
data = [3, 8, 5, 12, 7, 4, 11, 6]
count = 0
total = 0
for x in data:
if x % 2 == 0:
count += 1
total += x
print(count)
print(total)
| x | x % 2 == 0? | countcount | totaltotal |
|---|---|---|---|
| 3 | False | 0 | 0 |
| 8 | True | 1 | 8 |
| 5 | False | 1 | 8 |
| 12 | True | 2 | 20 |
| 7 | False | 2 | 20 |
| 4 | True | 3 | 24 |
| 11 | False | 3 | 24 |
| 6 | True | 4 | 30 |
[4] (1 mark per two correct rows; penalise cumulative total errors)[4](每两行正确得 1 分;累计 total 错误扣分)
print(count) outputs 4. [1] print(total) outputs 30. [1]print(count) 输出 4。[1] print(total) 输出 30。[1]
The program uses an if block to only process even numbers; continue is not needed because the updates are already guarded inside the if body and odd numbers are simply skipped by not executing that block. [1]程序使用 if 块来只处理偶数;不需要 continue,因为更新操作已在 if 体内受到保护,奇数只需不执行该块即可跳过。[1]
Alternative version using continue: [1]使用 continue 的替代版本:[1]
FOR x IN data:
IF x % 2 != 0 THEN
CONTINUE
END IF
count += 1
total += x
A student designs a program to print even numbers from 10 down to 2 using a while loop with an if inside.一名学生设计一个程序,使用 while 循环加内嵌 if 按降序打印 10 到 2 的偶数。
SET i TO 10
WHILE i >= 2:
IF i % 2 == 0 THEN
OUTPUT i
END IF
SET i TO i - 1
END WHILE
Trace: i starts at 10. Each iteration checks if i is even and outputs it, then decrements. Even values encountered: 10, 8, 6, 4, 2. When i = 1: 1 >= 2 is False, loop exits. [3] (1 mark per correct output line up to 3)追踪:i 从 10 开始,每次迭代检查 i 是否为偶数并输出,然后递减。遇到的偶数:10、8、6、4、2。当 i = 1 时:1 >= 2 为 False,循环退出。[3](每行正确输出得 1 分,最多 3 分)
10 8 6 4 2
The loop body executes 9 times (i = 10, 9, 8, 7, 6, 5, 4, 3, 2). [1] The OUTPUT statement executes 5 times (for even values: 10, 8, 6, 4, 2). [1]循环体执行 9 次(i = 10, 9, 8, 7, 6, 5, 4, 3, 2)。[1] OUTPUT 语句执行 5 次(对应偶数:10、8、6、4、2)。[1]
for i in range(10, 1, -2):
print(i)
[3]: 1 mark for range with correct start (10), 1 mark for correct stop (1, so 2 is included), 1 mark for correct step (-2). Accept any valid equivalent.[3]:start 正确(10)得 1 分,stop 正确(1,使 2 被包含)得 1 分,step 正确(-2)得 1 分。接受任何等效的正确写法。
The for version is shorter and eliminates both the explicit counter decrement and the if statement, reducing the risk of errors such as forgetting to update the counter. [1]for 版本更简洁,省去了显式计数器递减和 if 语句,降低了忘记更新计数器等错误的风险。[1]
range(start, stop, step) supports negative steps for countdown loops. range(10, 1, -2) produces 10, 8, 6, 4, 2 directly, eliminating the need for a separate parity check. When the step encodes the selection logic, the if statement inside the loop becomes redundant.range(start, stop, step) 支持负步长用于倒计时循环。range(10, 1, -2) 直接生成 10, 8, 6, 4, 2,无需单独的奇偶检查。当步长本身已编码了选择逻辑时,循环内的 if 语句就变得多余了。A program finds and prints the first value in a list greater than a threshold, then stops.一个程序查找并打印列表中第一个大于给定阈值的值,然后停止。
data = [4, 9, 2, 15, 7]
threshold = 10
for i in range(len(data)):
if data[i] > threshold:
print("Found at index", i, ":", data[i])
break
| i | data[i] | data[i] > 10?data[i] > 10? | Action操作 |
|---|---|---|---|
| 0 | 4 | False | continue loop继续循环 |
| 1 | 9 | False | continue loop继续循环 |
| 2 | 2 | False | continue loop继续循环 |
| 3 | 15 | True | print output, break打印输出,退出 |
[3] (1 mark per column correct across all rows; i=4 never reached due to break)[3](各列全行正确得 1 分;由于 break,i=4 从未被访问)
Found at index 3 : 15 [1]Found at index 3 : 15 [1]
Without break, the loop continues checking all remaining elements after finding 15 at index 3. [1] The loop then checks index 4: data[4] = 7, 7 > 10 is False, so nothing additional is printed. The only value printed is still Found at index 3 : 15 because 7 does not exceed the threshold. The program no longer stops early and completes the full iteration. [1]删去 break 后,循环在找到索引 3 处的 15 之后继续检查剩余元素。[1] 接着检查索引 4:data[4] = 7,7 > 10 为 False,不打印额外内容。唯一打印的仍是 Found at index 3 : 15,因为 7 不超过阈值。程序不再提前停止,完成全部迭代。[1]
found = False
for i in range(len(data)):
if data[i] > threshold:
print("Found at index", i, ":", data[i])
found = True
break
if not found:
print("Not found")
[1] for the found flag initialised before the loop, [1] for the correct post-loop check. (Accept equivalent pseudocode.)[1] 用于在循环前初始化 found 标志,[1] 用于循环后的正确检查。(接受等效的伪代码。)
for/else construct where the else block runs only when the loop completes without hitting a break, achieving the same result more concisely."带标志搜索"模式是线性搜索算法的基础。在循环前设置布尔标志并在循环后检查是检测循环是否找到所需内容的标准方式。Python 还支持优雅的 for/else 结构,else 块仅在循环未触发 break 的情况下执行,以更简洁的方式实现相同效果。