← Course Hub← 课程主页 ← All Units← 返回单元列表
H I G H  S C H O O L  C O M P U T E R  S C I E N C E
Practice练习题

Control Flow控制流程

Practice Questions · AP CSP-Feeder · US / ON / BC / AB Styles练习题集 · AP CSP 衔接 · 美 / 安 / 卑 / 阿省风格

EASY MEDIUM HARD 🇺🇸 US 🇨🇦 ON 🇨🇦 BC 🇨🇦 AB AP CSP-style MCQAP CSP 风格选择题 AP CSP-feeder FRQAP CSP 衔接简答题 ON Provincial-style安大略省考风格 BC Provincial-style卑诗省考风格 AB/Universal Applied阿省/通用应用题 Honors荣誉级


Name:姓名:Date:日期:
PART I  ·  SHORT RESPONSE第一部分  ·  短答题AP CSP-style MCQ + ON/BC short answer · 25 marksAP CSP 风格选择题 + 安/卑省考短答 · 共 25 分

Section A · Short ResponseA 部分 · 短答题

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;作答前逐步追踪。

Q1 EASY 🇺🇸 US AP CSP-style MCQAP CSP 风格选择题 §1 Boolean Conditions布尔条件 · AP CSP 3.6 [3 marks][3 分]

Which Boolean expression evaluates to True when x = 7?x = 7 时,哪个布尔表达式的值为 True

  1. (A) x == 6
  2. (B) x > 5 and x < 10
  3. (C) x != 7
  4. (D) x > 10 or x < 3
Q2 EASY 🇺🇸 US AP CSP-style MCQAP CSP 风格选择题 §2 if / elif / elseif / elif / else · AP CSP 3.6 [3 marks][3 分]

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")
  1. (A) A
  2. (B) B
  3. (C) C
  4. (D) F
Q3 EASY 🇨🇦 ON ON Provincial-style安大略省考风格 §3 Nested Conditionals嵌套条件 · ICS3U A2.3 [4 marks][4 分]

Trace the nested conditional below with age = 15 and height = 130.age = 15height = 130 追踪以下嵌套条件。

IF age >= 12 THEN
    IF height >= 140 THEN
        OUTPUT "Welcome!"
    ELSE
        OUTPUT "Too short."
    END IF
ELSE
    OUTPUT "Too young."
END IF
(a) State the output.写出输出结果。 [1]
(b) Explain why the inner conditional is reached (does execute) in this case.解释为什么内层条件在本例中被执行到。 [2]
(c) What would the output be if age = 10? Explain in one sentence.age = 10,输出是什么?用一句话解释。 [1]
Q4 MEDIUM 🇨🇦 AB AB/Universal Applied阿省/通用应用题 §4 while Loopswhile 循环 · CSE1120 outcome 1 [7 marks][7 分]

Consider the pseudocode below.考察以下伪代码。

SET n TO 0
WHILE n < 5:
    SET n TO n + 2
OUTPUT n
(a) Complete the trace table below.补全以下追踪表。 [3]
Iterationn before body循环体前 nCondition (n < 5)?条件(n < 5)?n after body循环体后 n
1
2
3
(b) State the value output by the final OUTPUT n statement.写出最后 OUTPUT n 语句输出的值。 [1]
(c) How many times does the loop body execute? Justify your answer.循环体执行了几次?请说明理由。 [2]
(d) Modify the condition so the loop executes exactly 2 times. State the new condition.修改条件使循环恰好执行 2 次。写出新条件。 [1]
Q5 MEDIUM 🇨🇦 BC BC Provincial-style卑诗省考风格 §5 for Loopsfor 循环 · CSTA 3A-AP-15 [8 marks][8 分]

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)
(a) List all values that i takes during execution.列出执行过程中 i 的所有取值。 [2]
(b) Write the exact output of the program, one value per line.写出程序的精确输出,每行一个值。 [2]
(c) The student now wants to print all integers from 1 to 10 inclusive. They write range(1, 10). Identify the error and correct it.学生现在想打印 1 到 10(含)的所有整数,写了 range(1, 10)。指出错误并更正。 [2]
(d) Explain in one sentence when a for loop is preferred over a while loop. Give one example scenario.用一句话解释什么情况下 for 循环比 while 循环更合适。举一个应用场景。 [2]
PART II  ·  EXTENDED RESPONSE第二部分  ·  简答题AP CSP-feeder FRQ + Honors · 31 marksAP CSP 衔接简答题 + 荣誉级 · 共 31 分

Section B · Extended ResponseB 部分 · 简答题

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 须清晰书写,缩进正确。论证题两句推理即可满分。无需计算器;手工追踪。

Q6 MEDIUM 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §6 break & continuebreak 与 continue · CSE1120 2.8 [8 marks][8 分]

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)
(a) Trace Program A. State the output and identify which element caused the loop to exit.追踪程序 A。写出输出,并指出是哪个元素导致循环退出。 [3]
(b) Trace Program B step by step, showing the value of total after each iteration. State the final output.逐步追踪程序 B,写出每次迭代后 total 的值。写出最终输出。 [3]
(c) Explain the difference between break and continue in one sentence each.分别用一句话解释 breakcontinue 的区别。 [2]
Q7 MEDIUM 🇨🇦 ON ON Provincial-style安大略省考风格 §7 Nested Loops嵌套循环 · ICS3U A2.3 / CSE1120 2.8.2 [8 marks][8 分]

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),其中 ij 均在 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
(a) State the total number of times the inner loop body (the IF block) is checked during the entire execution.写出整个执行过程中内层循环体(IF 块)被检查的总次数。 [2]
(b) List all pairs (i, j) that satisfy the condition and are output. Show your working.列出满足条件并被输出的所有 (i, j) 对,并展示过程。 [3]
(c) Modify the outer loop so that the program only checks pairs where i ranges from 1 to 5. State the new outer loop line.修改外层循环,使程序仅检查 i 从 1 到 5 的情形。写出新的外层循环语句。 [1]
(d) Justify why two separate counter variables (i and j) are required in nested loops.论证为什么嵌套循环需要两个独立的计数器变量(ij)。 [2]
Q8 HARD 🇨🇦 BC 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §2 + §4 if inside whilewhile 内嵌 if · CSTA 3A-AP-15 [8 marks][8 分]

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
(a) Complete the trace table for this program. Fill in the value of total and val after each iteration, and whether the loop condition is still True.补全该程序的追踪表。填写每次迭代后 totalval 的值,以及循环条件是否仍为 True [4]
Itertotal after迭代后 totalval after迭代后 valtotal <= 20?total <= 20?
1
2
3
4
5
6
(b) State the two values output by the program.写出程序输出的两个值。 [2]
(c) Modify the program so the loop stops when total exceeds 50 instead of 20. State only the line that changes.修改程序使循环在 total 超过 50(而非 20)时停止。只写出改变的那一行。 [1]
(d) Explain why a while loop is more appropriate than a for loop for this task.解释为什么 while 循环比 for 循环更适合此任务。 [1]
Q9 HARD Honors荣誉级 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §5 + §7 for + nested + analysisfor + 嵌套 + 分析 · CSTA 3A-AP-15 [7 marks][7 分]

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()
(a) Write the complete output of this program exactly as it appears on screen.写出该程序在屏幕上的完整输出(与实际显示一致)。 [3]
(b) State the total number of times print(row * col, end=" ") executes.写出 print(row * col, end=" ") 共执行了多少次。 [1]
(c) The student wants a 4-row by 5-column grid. Write the two modified loop headers.学生希望打印 4 行 5 列的网格。写出两行修改后的循环头。 [2]
(d) For an m-row by n-column grid using this program structure, express the total number of inner-body executions in terms of m and n.对于使用该程序结构的 m 行 n 列网格,用 m 和 n 表示内层循环体的总执行次数。 [1]
PART III  ·  MODELING / APPLIED第三部分  ·  建模与应用Universal / multi-region applied · 25 marks通用/多地区应用题 · 共 25 分

Section C · Modeling and ApplicationsC 部分 · 建模与应用

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 须正确缩进。每题以一句完整的结论句作答。

Q10 MEDIUM 🇺🇸 US 🇨🇦 ON AP CSP-feeder FRQAP CSP 衔接简答题 §4 + §5 Loop selection + accumulator循环选择 + 累加器 · ICS3U A2.2 [8 marks][8 分]

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)
(a) Trace the program for each element of data. Record whether x % 2 == 0 is True and the running values of count and total.data 的每个元素追踪程序,记录 x % 2 == 0 是否为 True,以及 counttotal 的累计值。 [4]
(b) State the two values output by the program.写出程序输出的两个值。 [2]
(c) Explain why 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]
Q11 MEDIUM 🇨🇦 ON 🇨🇦 BC ON Provincial-style安大略省考风格 §2 + §3 + §5 Design: conditionals + for设计:条件 + for · ICS3U A2.2 [9 marks][9 分]

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
(a) Write the complete output of this program.写出该程序的完整输出。 [3]
(b) How many times does the loop body execute in total? How many of those times does the OUTPUT statement run?循环体总共执行多少次?其中 OUTPUT 语句执行了多少次? [2]
(c) Rewrite this program using a for loop with range so that the if statement inside the loop body is no longer needed. Write the Python code.用带 rangefor 循环重写该程序,使循环体内不再需要 if 语句。写出 Python 代码。 [3]
(d) State one advantage of the for version over the while version for this specific task.针对此具体任务,说明 for 版本相对 while 版本的一个优点。 [1]
Q12 HARD 🇺🇸 US 🇨🇦 ON 🇨🇦 BC AP CSP-feeder FRQAP CSP 衔接简答题 All sections · Debugging + extension全节综合 · 调试与扩展 · CSTA 3A-AP-15 [8 marks][8 分]

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
(a) Trace the loop step by step. For each iteration, state the value of i, the value of data[i], and whether the condition data[i] > threshold is True.逐步追踪循环。每次迭代写出 i 的值、data[i] 的值,以及条件 data[i] > threshold 是否为 True。 [3]
(b) State the output of the program.写出程序的输出。 [1]
(c) Remove the break statement. Describe how the program's behaviour changes. State all values that would now be printed.删去 break 语句。描述程序行为如何改变。写出现在会打印的所有值。 [2]
(d) Extend the program: after the loop ends, if no value was found above the threshold, print "Not found". Write the additional pseudocode or Python needed.扩展程序:循环结束后,若没有找到超过阈值的值,打印 "Not found"。写出所需的额外伪代码或 Python。 [2]

🇺🇸 US CSTA / AP CSP美国 CSTA / AP CSP3A-AP-15 · 3.6 · 3.7
🇨🇦 Ontario安大略ICS3U A2.2 · A2.3 · CSE1120
🇨🇦 British Columbia不列颠哥伦比亚CS 11 / APCSP: selection, iteration, boolean logicCS 11 / APCSP:选择、迭代、布尔逻辑
🇨🇦 Alberta阿尔伯塔CSE1120: outcomes 1, 2.6, 2.8, 2.8.1, 2.8.2CSE1120:结果 1、2.6、2.8、2.8.1、2.8.2

Full 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 结果框架。