Practice Questions · AP CSP-Feeder · US / ON / BC / AB Styles练习题集 · AP CSP 衔接 · 美 / 安 / 卑 / 阿省风格
Questions mix multiple-choice and short-answer items. For MCQs, circle the letter and show your trace in the work space. For short-answer items, write the exact output (include quotes only if the question shows them). Code is pseudocode or Python; trace each step to verify before answering.本部分包含选择题与短答题。选择题请圈出字母,并在答题空白处写出追踪过程。短答题写出精确输出(仅在题目显示引号时才加引号)。代码为伪代码或 Python;作答前逐步追踪。
Which Boolean expression evaluates to True when x = 7?当 x = 7 时,哪个布尔表达式的值为 True?
x == 6x > 5 and x < 10x != 7x > 10 or x < 3What 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")
ABCFTrace 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
age = 10? Explain in one sentence.若 age = 10,输出是什么?用一句话解释。 [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 | |||
| 2 | |||
| 3 |
OUTPUT n statement.写出最后 OUTPUT n 语句输出的值。 [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)
i takes during execution.列出执行过程中 i 的所有取值。 [2]range(1, 10). Identify the error and correct it.学生现在想打印 1 到 10(含)的所有整数,写了 range(1, 10)。指出错误并更正。 [2]for loop is preferred over a while loop. Give one example scenario.用一句话解释什么情况下 for 循环比 while 循环更合适。举一个应用场景。 [2]Show every step of your trace. Write pseudocode or Python clearly, with correct indentation. For justify questions, two sentences of reasoning earn full marks. Calculator not applicable; trace by hand.写出每一步追踪过程。伪代码或 Python 须清晰书写,缩进正确。论证题两句推理即可满分。无需计算器;手工追踪。
Study the two programs below.研究以下两个程序。
Program A (uses break):程序 A(使用 break):
numbers = [3, 7, 12, 5, 18]
for n in numbers:
if n > 10:
print("Found:", n)
break
Program B (uses continue):程序 B(使用 continue):
scores = [5, -3, 8, -1, 4]
total = 0
for s in scores:
if s < 0:
continue
total += s
print(total)
total after each iteration. State the final output.逐步追踪程序 B,写出每次迭代后 total 的值。写出最终输出。 [3]break and continue in one sentence each.分别用一句话解释 break 与 continue 的区别。 [2]A program uses nested loops to find all pairs of integers (i, j) where i is in the range 1 to 3 (inclusive) and j is in the range 1 to 3 (inclusive) such that 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
IF block) is checked during the entire execution.写出整个执行过程中内层循环体(IF 块)被检查的总次数。 [2](i, j) that satisfy the condition and are output. Show your working.列出满足条件并被输出的所有 (i, j) 对,并展示过程。 [3]i ranges from 1 to 5. State the new outer loop line.修改外层循环,使程序仅检查 i 从 1 到 5 的情形。写出新的外层循环语句。 [1]i and j) are required in nested loops.论证为什么嵌套循环需要两个独立的计数器变量(i 和 j)。 [2]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
total and val after each iteration, and whether the loop condition is still True.补全该程序的追踪表。填写每次迭代后 total 和 val 的值,以及循环条件是否仍为 True。 [4]
| Iter | total after迭代后 total | val after迭代后 val | total <= 20?total <= 20? |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 |
total exceeds 50 instead of 20. State only the line that changes.修改程序使循环在 total 超过 50(而非 20)时停止。只写出改变的那一行。 [1]while loop is more appropriate than a for loop for this task.解释为什么 while 循环比 for 循环更适合此任务。 [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()
print(row * col, end=" ") executes.写出 print(row * col, end=" ") 共执行了多少次。 [1]Read each scenario carefully before writing code or traces. Where pseudocode is requested, use the standard block format (IF/THEN/END IF, WHILE/END WHILE, FOR/END FOR). Where Python is requested, show indentation correctly. Conclude each question with a one-sentence answer.动笔前仔细阅读每个场景。伪代码使用标准块格式(IF/THEN/END IF、WHILE/END WHILE、FOR/END FOR)。Python 须正确缩进。每题以一句完整的结论句作答。
A program counts how many even numbers exist in a given 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)
data. Record whether x % 2 == 0 is True and the running values of count and total.对 data 的每个元素追踪程序,记录 x % 2 == 0 是否为 True,以及 count 和 total 的累计值。 [4]continue is NOT used in this program even though we skip odd numbers. Describe an alternative version that uses continue to achieve the same result, in pseudocode.解释为什么该程序跳过奇数却没有使用 continue。用伪代码描述一个使用 continue 达到相同结果的替代版本。 [2]A student is designing a program to print the even numbers from 10 down to 2 (in descending order). They decide to use a while loop and an if statement inside it.一名学生正在设计一个程序,按降序打印从 10 到 2 的偶数。他们决定使用 while 循环,并在其中嵌套一个 if 语句。
SET i TO 10
WHILE i >= 2:
IF i % 2 == 0 THEN
OUTPUT i
END IF
SET i TO i - 1
END WHILE
OUTPUT statement run?循环体总共执行多少次?其中 OUTPUT 语句执行了多少次? [2]for loop with range so that the if statement inside the loop body is no longer needed. Write the Python code.用带 range 的 for 循环重写该程序,使循环体内不再需要 if 语句。写出 Python 代码。 [3]for version over the while version for this specific task.针对此具体任务,说明 for 版本相对 while 版本的一个优点。 [1]A program is meant to find and print the first value in a list that is greater than a given threshold, then stop searching.一个程序旨在找到并打印列表中第一个大于给定阈值的值,然后停止搜索。
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, the value of data[i], and whether the condition data[i] > threshold is True.逐步追踪循环。每次迭代写出 i 的值、data[i] 的值,以及条件 data[i] > threshold 是否为 True。 [3]break statement. Describe how the program's behaviour changes. State all values that would now be printed.删去 break 语句。描述程序行为如何改变。写出现在会打印的所有值。 [2]"Not found". Write the additional pseudocode or Python needed.扩展程序:循环结束后,若没有找到超过阈值的值,打印 "Not found"。写出所需的额外伪代码或 Python。 [2]3A-AP-15 · 3.6 · 3.7ICS3U A2.2 · A2.3 · CSE1120Full Syllabus Map in Study Guide: ../Study Guides/Unit_3_Control_Flow.html. CS has no AB standalone diploma exam; AB framing uses CSE1120 outcomes.完整大纲对照见学习指南:../Study Guides/Unit_3_Control_Flow.html。CS 无独立 AB 毕业考;AB 题使用 CSE1120 结果框架。