← 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练习题

Data Structures数据结构

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 any trace in the work space. For short-answer items, write exact output or state the structure name clearly. Code is pseudocode or Python; trace each step before answering.本部分包含选择题与短答题。选择题请圈出字母,并在答题空白处写出追踪过程。短答题写出精确输出或清晰说明结构名称。代码为伪代码或 Python;作答前逐步追踪。

Q1 EASY 🇺🇸 US AP CSP-style MCQAP CSP 风格选择题 §1 Arrays and Lists数组与列表 · AP CSP AAP-2.K [3 marks][3 分]

Given temps = [15, 22, 18, 30, 27], which statement is correct?给定 temps = [15, 22, 18, 30, 27],以下哪个说法正确?

  1. (A) temps[2] has value 22temps[2] 的值为 22
  2. (B) temps[2] has value 18temps[2] 的值为 18
  3. (C) len(temps) is 4len(temps)4
  4. (D) The last valid index is 5最后一个有效索引为 5
Q2 EASY 🇺🇸 US AP CSP-style MCQAP CSP 风格选择题 §2 Indexing and Traversal索引与遍历 · AP CSP 3.10 [3 marks][3 分]

What does the following program output?以下程序输出什么?

data = [10, 20, 30, 40, 50]
total = 0
for i in range(0, 3):
    total += data[i]
print(total)
  1. (A) 150
  2. (B) 90
  3. (C) 60
  4. (D) 100
Q3 MEDIUM 🇨🇦 ON ON Provincial-style安大略省考风格 §3 List Operations列表操作 · ICS3U A1.6 / AB CSE2120 1.3.2 [5 marks][5 分]

A program builds and modifies a list step by step.一个程序逐步构建并修改列表。

cart = []
cart.append("apples")
cart.append("bread")
cart.insert(1, "milk")
cart.remove("bread")
cart.append("eggs")
(a) State the final contents of cart after all five operations complete.写出全部五次操作完成后 cart 的最终内容。 [2]
(b) Explain what cart.insert(1, "milk") does to the list. State which element is shifted and in which direction.解释 cart.insert(1, "milk") 对列表的操作。说明哪个元素被移动,向哪个方向移动。 [2]
(c) State the difference between remove("bread") and pop(1) in one sentence.用一句话说明 remove("bread")pop(1) 的区别。 [1]
Q4 MEDIUM 🇨🇦 AB AB/Universal Applied阿省/通用应用题 §7 Choosing the Right Data Structure选择合适的数据结构 · CSTA 3B-AP-12 [6 marks][6 分]

For each scenario below, identify the most appropriate data structure (list, 2D array, dictionary, set, or tuple) and give a one-sentence justification.对于以下每个场景,确定最合适的数据结构(列表、二维数组、字典、集合或元组),并用一句话说明理由。

(a) Storing the GPS coordinate pair (latitude, longitude) for a fixed location that should never change.存储固定位置的 GPS 坐标对 (纬度, 经度),该位置不应改变。 [2]
(b) Storing the pixel values of an 8x8 greyscale image where each cell holds an integer 0-255.存储 8x8 灰度图像的像素值,每个单元格存储 0-255 的整数。 [2]
(c) Recording which students have already submitted an assignment, where you frequently check whether a given student has submitted.记录已提交作业的学生,需要频繁检查某位学生是否已提交。 [2]
Q5 MEDIUM 🇨🇦 BC BC Provincial-style卑诗省考风格 §5 Dictionaries and Maps字典与映射 · ICS4U C1.1 [8 marks][8 分]

A program tracks student grades using a dictionary.一个程序使用字典跟踪学生成绩。

grades = {}
grades["Alice"] = 91
grades["Bob"] = 78
grades["Carol"] = 85
grades["Bob"] = 82
for name, score in grades.items():
    if score >= 85:
        print(name)
(a) State the final value of grades["Bob"] after all assignments complete. Explain why.所有赋值完成后,写出 grades["Bob"] 的最终值并解释原因。 [2]
(b) State the complete output of the program. Show your reasoning for each student.写出程序的完整输出,并对每位学生说明判断过程。 [3]
(c) Explain in one sentence why accessing a dictionary value by key is faster than searching a list for the same value.用一句话解释为什么通过键访问字典值比在列表中搜索相同值更快。 [1]
(d) Write one line of Python that adds a new student "Dave" with score 70 to the dictionary.写一行 Python,将新学生 "Dave"(成绩 70)添加到字典中。 [1]
(e) Identify which data structure would be less suitable than a dictionary for this task. Give one reason.说明哪种数据结构不如字典适合此任务,并给出一个理由。 [1]
PART II  ·  EXTENDED RESPONSE第二部分  ·  简答题AP CSP-feeder FRQ + Honors · 30 marksAP CSP 衔接简答题 + 荣誉级 · 共 30 分

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. Code in questions is un-translated pseudocode or Python.写出每一步追踪过程。伪代码或 Python 须清晰书写,缩进正确。论证题两句推理即可满分。题目中的代码为语言无关的伪代码或 Python。

Q6 EASY 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §6 Tuples and Sets元组与集合 · CSTA 3B-AP-12 / BC CP12 [6 marks][6 分]

Study the two programs below.研究以下两个程序。

Program A (tuple):程序 A(元组):

point = (3, 7)
print(point[0])
# point[0] = 5   # This line is commented out

Program B (set):程序 B(集合):

scores = [85, 90, 85, 74, 90, 65]
unique = set(scores)
print(len(unique))
print(85 in unique)
(a) State the output of Program A. Explain why the commented-out line would cause an error if uncommented.写出程序 A 的输出。解释若取消注释该行为何会报错。 [2]
(b) State the complete output of Program B, one value per line. Show which duplicate values are removed when the set is created.写出程序 B 的完整输出,每行一个值。说明创建集合时哪些重复值被删除。 [3]
(c) State one situation where you would prefer a tuple over a list. Give a concrete example.说明一种应优先选择元组而非列表的场景,并给出具体示例。 [1]
Q7 MEDIUM 🇨🇦 ON ON Provincial-style安大略省考风格 §2 + §3 Traversal + List Operations遍历 + 列表操作 · ICS3U A2.3 / AP CSP AAP-2.K [8 marks][8 分]

A program processes a list of test scores to find the maximum and filter out failing scores.一个程序处理一系列测试成绩,找出最高分并过滤掉不及格成绩。

scores = [72, 45, 88, 61, 95, 38, 77]

# Part (a): find maximum
max_val = scores[0]
for i in range(1, len(scores)):
    if scores[i] > max_val:
        max_val = scores[i]

# Part (b): build passing list
passing = []
for s in scores:
    if s >= 60:
        passing.append(s)
(a) Trace the maximum-finding loop step by step. Complete the trace table.逐步追踪求最大值的循环,补全追踪表。 [3]
iscores[i]Update max_val?更新 max_valmax_val
1
2
3
4
5
6
(b) State the final contents of passing after the second loop completes.写出第二个循环完成后 passing 的最终内容。 [2]
(c) Why does the maximum-finding loop start at index 1 rather than 0? Justify in two sentences.为什么求最大值的循环从索引 1 而不是 0 开始?用两句话说明理由。 [2]
(d) State the length of passing and the length of the original scores list.写出 passing 的长度和原始 scores 列表的长度。 [1]
Q8 HARD Honors荣誉级 🇨🇦 BC 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §4 2D Arrays and Nested Lists二维数组与嵌套列表 · ICS4U A3.5 / AB CSE2120 1.2.1 [8 marks][8 分]

A program stores grades for 3 students across 3 tests in a 2D array and computes the sum of all values.一个程序将 3 名学生在 3 次测试中的成绩存储在二维数组中,并计算所有值之和。

grades = [[85, 90, 78],
          [72, 88, 94],
          [91, 67, 83]]

total = 0
for row in grades:
    for score in row:
        total += score
print(total)
print(grades[1][2])
(a) State the value of grades[1][2]. Explain which student and which test it refers to (using 0-based counting).写出 grades[1][2] 的值。解释它对应哪位学生和哪次测试(使用从 0 开始的计数)。 [2]
(b) Trace the nested loop and state the final value of total. Show the running total after each row is processed.追踪嵌套循环并写出 total 的最终值。写出每行处理后的累计总和。 [3]
(c) Write a modified version of the inner loop that computes the sum of only the first two tests (columns 0 and 1) for every student. Write the Python code.编写修改版的内层循环,仅计算每位学生前两次测试(第 0 和第 1 列)的成绩总和。写出 Python 代码。 [2]
(d) State how many times the inner for score in row loop body executes in total during the full traversal.写出完整遍历过程中内层 for score in row 循环体共执行了多少次。 [1]
Q9 HARD Honors荣誉级 🇺🇸 US 🇨🇦 ON AP CSP-feeder FRQAP CSP 衔接简答题 §5 + §6 Dictionary + Set analysis字典 + 集合分析 · CSTA 3B-AP-12 / ICS4U C1.1 [8 marks][8 分]

A program counts word frequencies in a sentence and then finds words that appear in both sentences using sets.一个程序统计句子中的词频,然后用集合找出两个句子中共同出现的单词。

sentence1 = "the cat sat on the mat"
words1 = sentence1.split()
counts = {}
for word in words1:
    if word in counts:
        counts[word] += 1
    else:
        counts[word] = 1

sentence2 = "the dog sat by the tree"
set1 = set(sentence1.split())
set2 = set(sentence2.split())
common = set1 & set2
(a) State the final contents of counts. List all key-value pairs.写出 counts 的最终内容,列出所有键值对。 [3]
(b) State the contents of set1, set2, and common. (Sets are unordered; list elements in any order.)写出 set1set2common 的内容。(集合无序;元素可按任意顺序列出。) [3]
(c) Explain why set1 has fewer elements than len(words1).解释为什么 set1 的元素数量少于 len(words1) [1]
(d) State what the & operator does when applied to two sets.说明 & 运算符对两个集合的操作。 [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. Where Python is requested, show correct indentation. Conclude each question with a one-sentence answer to the scenario.动笔前仔细阅读每个场景。伪代码使用标准块格式。Python 须正确缩进。每题以一句完整结论句作答。

Q10 MEDIUM 🇺🇸 US 🇨🇦 ON AP CSP-feeder FRQAP CSP 衔接简答题 §1 + §2 Array indexing + traversal patterns数组索引 + 遍历模式 · ICS3U A1.5 / CSTA 3A-AP-14 [8 marks][8 分]

A weather station records daily high temperatures for one week: [14, 19, 22, 18, 25, 21, 17]. A program computes the average and finds the first day above a threshold.气象站记录一周每日最高气温:[14, 19, 22, 18, 25, 21, 17]。一个程序计算平均值并找出第一个超过阈值的天。

temps = [14, 19, 22, 18, 25, 21, 17]
threshold = 20

total = 0
for t in temps:
    total += t
average = total / len(temps)

found_index = -1
for i in range(len(temps)):
    if temps[i] > threshold:
        found_index = i
        break
(a) Compute the value of average. Show your calculation.计算 average 的值,写出计算过程。 [2]
(b) Trace the second loop. State the value of found_index after the loop ends and identify which day (by its value) triggered the break.追踪第二个循环。写出循环结束后 found_index 的值,并说明哪一天的气温(具体数值)触发了 break。 [3]
(c) What would found_index be if the threshold were changed to 30? Explain why.若将阈值改为 30found_index 会是什么?解释原因。 [2]
(d) Identify the last valid index of temps and state the value stored there.说明 temps 的最后一个有效索引,并写出该位置存储的值。 [1]
Q11 MEDIUM 🇨🇦 ON 🇨🇦 BC ON Provincial-style安大略省考风格 §3 + §5 List operations + Dictionary design列表操作 + 字典设计 · ICS3U A1.6 / ICS4U C1.1 [9 marks][9 分]

A school library uses a system to track which books are currently checked out. Books are stored in a list; a dictionary maps each student name to a list of books they have borrowed.学校图书馆用一个系统跟踪当前借出的书籍。书籍存储在列表中;字典将每位学生姓名映射到他们借阅的书籍列表。

checked_out = []
checked_out.append("Python Basics")
checked_out.append("Data Science")
checked_out.insert(1, "Algorithms")
checked_out.remove("Data Science")

borrowers = {}
borrowers["Maya"] = ["Python Basics", "Algorithms"]
borrowers["Leon"] = ["Python Basics"]
(a) State the final contents of checked_out after all four list operations.写出全部四次列表操作后 checked_out 的最终内容。 [2]
(b) Write Python code that prints the name of every student who has borrowed "Python Basics". Use the borrowers dictionary.编写 Python 代码,打印所有借阅过 "Python Basics" 的学生姓名。使用 borrowers 字典。 [3]
(c) Write one line of Python that removes "Algorithms" from Maya's borrowed list in the dictionary.写一行 Python,将 "Algorithms" 从字典中 Maya 的借阅列表中删除。 [2]
(d) Explain why a dictionary mapping student names to book lists is more efficient than searching two parallel lists (one of student names, one of book lists) to find a student's books. One sentence.解释为什么将学生姓名映射到书籍列表的字典,比搜索两个并行列表(一个学生名列表,一个书籍列表)查找学生书籍更高效。一句话即可。 [2]
Q12 HARD 🇺🇸 US 🇨🇦 ON 🇨🇦 BC AP CSP-feeder FRQAP CSP 衔接简答题 §6 + §7 Sets + structure choice + analysis集合 + 结构选择 + 分析 · CSTA 3B-AP-12 [8 marks][8 分]

A social media platform finds users that two people have in common and users that only one person follows. It also needs to count how many times each tag appears in a list of posts.一个社交媒体平台找出两人共同关注的用户,以及只有一人关注的用户。它还需要统计帖子列表中每个标签出现的次数。

followsA = {"alice", "bob", "carol", "dave"}
followsB = {"bob", "eve", "carol", "frank"}

tags = ["#python", "#cs", "#python", "#data", "#cs", "#python"]
tag_counts = {}
for tag in tags:
    if tag in tag_counts:
        tag_counts[tag] += 1
    else:
        tag_counts[tag] = 1
(a) Write Python expressions (using set operators) that compute: (i) users followed by both A and B; (ii) users followed only by A (not by B). State the result of each expression.用集合运算符写出 Python 表达式,计算:(i) A 和 B 都关注的用户;(ii) 只有 A 关注(B 未关注)的用户。写出每个表达式的结果。 [3]
(b) Trace the tag_counts loop. State the final contents of tag_counts.追踪 tag_counts 循环。写出 tag_counts 的最终内容。 [3]
(c) Identify the data structure used for followsA and followsB and state one property that makes it ideal for computing users-in-common.说明 followsAfollowsB 使用的数据结构,并说明使其特别适合计算共同用户的一个属性。 [2]

🇺🇸 US CSTA / AP CSP美国 CSTA / AP CSP3A-AP-14 · 3B-AP-12 · AAP-2.K
🇨🇦 Ontario安大略ICS3U A1.5 · A1.6 · A2.3 · ICS4U A3.5
🇨🇦 British Columbia不列颠哥伦比亚CS 11 / AP CSP: lists, data organisation, BC CP12CS 11 / AP CSP:列表、数据组织、BC CP12
🇨🇦 Alberta阿尔伯塔CSE2120: outcomes 1.2.1, 1.3.2 (arrays, insertion, deletion)CSE2120:结果 1.2.1、1.3.2(数组、插入、删除)

Full Syllabus Map in Study Guide: ../Study Guides/Unit_5_Data_Structures.html. CS has no AB standalone diploma exam; AB framing uses CSE2120 outcomes.完整大纲对照见学习指南:../Study Guides/Unit_5_Data_Structures.html。CS 无独立 AB 毕业考;AB 题使用 CSE2120 结果框架。