← 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, Databases and the Web数据、数据库与网络

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/AB short answer · 25 marksAP CSP 风格选择题 + 安/卑/阿省考短答 · 共 25 分

Section A · Short ResponseA 部分 · 短答题

Questions mix multiple-choice and short-answer items. For MCQs, circle the letter and briefly justify your choice in the work space. For short-answer items, write concise answers and show any code traces step by step.本部分包含选择题与短答题。选择题请圈出字母,并在答题空白处简要说明理由。短答题写出简洁答案,并逐步展示代码追踪过程。

Q1 EASY 🇺🇸 US AP CSP-style MCQAP CSP 风格选择题 §1 Data vs Information数据与信息 · CSTA 3A-DA-10 [3 marks][3 分]

A weather sensor records the values 22.4, 65, 1013.2 with no labels. A program then adds the labels "temperature (C)", "humidity (%)", and "pressure (hPa)" and displays: "Average temp today: 22.4 C, expect rain." Which statement best describes the relationship between the raw sensor values and the final display?气象传感器记录了值 22.4, 65, 1013.2,没有标签。程序随后添加标签"温度 (C)"、"湿度 (%)"和"气压 (hPa)"并显示:"今日平均温度:22.4 C,预计有雨。"哪个陈述最准确地描述了原始传感器值与最终显示之间的关系?

  1. (A) The raw values are information because a computer can store them.原始值是信息,因为计算机可以存储它们。
  2. (B) The raw values are data; the labelled, interpreted display is information.原始值是数据;经过标注和解释的显示是信息。
  3. (C) Both the raw values and the final display are data.原始值和最终显示都是数据。
  4. (D) The final display is data because it contains numbers.最终显示是数据,因为它包含数字。
Q2 EASY 🇺🇸 US AP CSP-style MCQAP CSP 风格选择题 §2 File Input/Output文件读写 · CSE2130 1.4.2 [3 marks][3 分]

A student wants to add a new log entry to log.txt without erasing the existing entries. Which Python file-open mode should they use?一名学生想向 log.txt 添加新日志条目,而不删除现有条目。他们应该使用哪种 Python 文件打开模式?

  1. (A) "r"
  2. (B) "w"
  3. (C) "a"
  4. (D) "x"
Q3 EASY 🇨🇦 ON ON Provincial-style安大略省考风格 §3 CSV and Structured DataCSV 与结构化数据 · ICS4U A3.1 [4 marks][4 分]

Consider the CSV file below.考察以下 CSV 文件。

product_id,name,price
101,Notebook,3.50
102,Pen,1.25
103,Ruler,2.00
(a) Identify the header row and state how many records (data rows) this file contains.指出标题行,并说明该文件包含多少条记录(数据行)。 [2]
(b) A program reads the price field and tries to compute price * 1.13 (tax). The program crashes. Identify the most likely cause and state the fix in one line of Python.程序读取 price 字段并尝试计算 price * 1.13(含税价)。程序崩溃。找出最可能的原因,并用一行 Python 写出修复方法。 [2]
Q4 MEDIUM 🇨🇦 AB AB/Universal Applied阿省/通用应用题 §2 File Input/Output文件读写 · CSE2130 1.4.4 / 1.4.5 [7 marks][7 分]

The file scores.txt contains one integer per line:文件 scores.txt 每行包含一个整数:

87
92
74
95
88

A student writes the following program to compute the average and write it to summary.txt.一名学生编写以下程序计算平均值并写入 summary.txt

with open("scores.txt", "r") as f:
    scores = [int(line.strip()) for line in f]

average = sum(scores) / len(scores)

with open("summary.txt", "w") as f:
    f.write(f"Count: {len(scores)}\n")
    f.write(f"Average: {average:.1f}\n")
(a) State the exact contents written to summary.txt after the program runs.写出程序运行后写入 summary.txt 的确切内容。 [2]
(b) Explain what line.strip() does and why it is necessary here.解释 line.strip() 的作用,以及为什么在这里是必要的。 [2]
(c) The student wants to append a line "Max: 95" to the existing summary.txt without overwriting it. Write the two lines of Python needed.学生希望在不覆盖 summary.txt 的情况下追加一行 "Max: 95"。写出所需的两行 Python 代码。 [2]
(d) Why does the with open(...) as f: pattern eliminate the need to call f.close()? Answer in one sentence.为什么 with open(...) as f: 模式不需要调用 f.close()?用一句话回答。 [1]
Q5 MEDIUM 🇨🇦 BC BC Provincial-style卑诗省考风格 §3 CSV and Structured DataCSV 与结构化数据 · CSTA 3A-DA-11 [8 marks][8 分]

A program reads students.csv using csv.DictReader and prints students who scored above 85.程序使用 csv.DictReader 读取 students.csv,并打印成绩高于 85 的学生。

import csv

with open("students.csv", "r") as f:
    reader = csv.DictReader(f)
    for row in reader:
        if int(row["score"]) > 85:
            print(row["name"], row["score"])

The CSV content is:CSV 内容为:

name,grade,score
Alice,11,92
Bob,11,78
Carol,12,95
David,12,82
Eve,11,88
(a) Write the exact output of the program.写出程序的精确输出。 [2]
(b) Explain what csv.DictReader does differently from csv.reader. State one advantage of using DictReader for named columns.解释 csv.DictReadercsv.reader 的不同之处。说明对有名称的列使用 DictReader 的一个优点。 [2]
(c) Why must int(row["score"]) be used instead of just row["score"] in the comparison? What error would occur without the conversion?为什么比较中必须使用 int(row["score"]) 而不是直接用 row["score"]?如果不转换会出现什么错误? [2]
(d) Modify the condition to print only grade-12 students who scored above 85. State the new condition line.修改条件,使程序只打印成绩高于 85 的 12 年级学生。写出新的条件行。 [2]
PART II  ·  EXTENDED RESPONSE第二部分  ·  简答题AP CSP-feeder FRQ + Honors · 31 marksAP CSP 衔接简答题 + 荣誉级 · 共 31 分

Section B · Extended ResponseB 部分 · 简答题

Show all reasoning. Write SQL in full (SELECT...FROM...WHERE...ORDER BY). For design questions, justify your choice. Two sentences of reasoning earn full marks on explain/justify questions.展示所有推理过程。SQL 须完整书写(SELECT...FROM...WHERE...ORDER BY)。设计题须说明理由。论证/解释题两句推理即可满分。

Q6 MEDIUM 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §4 Relational Databases and Tables关系数据库与表 · CSTA 3A-DA-10 [8 marks][8 分]

A school library uses two tables to track books and loans.一所学校图书馆使用两个表来追踪图书和借阅记录。

Table: Books表:Books

book_id | title               | author
--------|---------------------|-------------
1       | Python Primer       | Smith, J.
2       | Data Science 101    | Lee, C.
3       | Web Basics          | Patel, R.

Table: Loans表:Loans

loan_id | book_id | student_name | due_date
--------|---------|--------------|----------
1       | 2       | Alice        | 2026-07-01
2       | 1       | Bob          | 2026-07-10
3       | 2       | Carol        | 2026-07-05
(a) Identify the primary key of the Books table and the primary key of the Loans table. Explain why a column qualifies as a primary key.指出 Books 表的主键和 Loans 表的主键。解释一列为何可以作为主键。 [3]
(b) Identify the foreign key in the Loans table. Which column in which table does it reference? Explain what linking the two tables via this foreign key allows a program to do.指出 Loans 表中的外键。它引用了哪个表的哪一列?解释通过此外键连接两个表允许程序做什么。 [3]
(c) The title "Python Primer" appears in the Books table once. Why is it better to store the title once in Books and reference it by book_id in Loans, rather than repeating the full title in every Loans row? Name this database design principle."Python Primer" 这个书名在 Books 表中只存储一次。为什么将书名存储在 Books 表中一次,并在 Loans 中通过 book_id 引用,比在每条 Loans 记录中重复书名更好?说出这种数据库设计原则的名称。 [2]
Q7 MEDIUM 🇨🇦 ON ON Provincial-style安大略省考风格 §5 SQL BasicsSQL 基础 · ICS4C A2.2 [8 marks][8 分]

Use the Books and Loans tables from Q6 to answer the SQL questions below.使用 Q6 中的 Books 和 Loans 表回答以下 SQL 问题。

(a) Write a SQL query to return all columns from the Books table.写一个 SQL 查询,返回 Books 表的所有列。 [1]
(b) Write a SQL query to return only the title and author from Books where book_id = 2.写一个 SQL 查询,从 Books 中返回 book_id = 2titleauthor [2]
(c) Write a SQL query to return all Loans rows sorted by due_date from earliest to latest. State the keyword for ascending sort order.写一个 SQL 查询,返回所有 Loans 行,按 due_date 从早到晚排序。说出升序排列的关键字。 [2]
(d) A student writes: SELECT title WHERE book_id = 1;. Identify the error and write the corrected query.学生写了:SELECT title WHERE book_id = 1;。找出错误并写出更正后的查询。 [2]
(e) State the mandatory clause order for a SQL SELECT statement that uses all four clauses.说出使用全部四个子句的 SQL SELECT 语句的必须子句顺序。 [1]
Q8 HARD 🇨🇦 BC 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §4 + §5 DB design + SQL analysis数据库设计 + SQL 分析 · CSTA 3A-DA-10 [8 marks][8 分]

A music streaming service stores data in two tables: Students (student_id, name, plan) and Plays (play_id, student_id, song_title, play_count). The plan column in Students can be "free" or "premium".一个音乐流媒体服务将数据存储在两个表中:Students(student_id, name, plan)和 Plays(play_id, student_id, song_title, play_count)。Students 表的 plan 列可以是 "free" 或 "premium"。

(a) Write a SQL query to return the name and plan of all students on the "premium" plan, sorted alphabetically by name.写一个 SQL 查询,返回所有 "premium" 方案学生的 nameplan,按姓名字母顺序排序。 [3]
(b) Write a SQL query to return all songs (song_title) where play_count is greater than 100, sorted by play_count from highest to lowest.写一个 SQL 查询,返回 play_count 大于 100 的所有歌曲(song_title),按 play_count 从高到低排序。 [3]
(c) State one tradeoff between storing all data in a single "flat" table versus using two linked tables as above. Which design is better when student names might change, and why?说出将所有数据存储在单个"平面"表中与使用如上两个关联表之间的一个权衡。当学生姓名可能更改时,哪种设计更好?原因是什么? [2]
Q9 HARD Honors荣誉级 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §5 SQL COUNT + tradeoff analysisSQL COUNT + 权衡分析 · CSTA 3A-DA-10 / 3A-DA-12 [7 marks][7 分]

Using the Scores table from the Study Guide (columns: score_id, student_id, subject, score), answer the following questions.使用学习指南中的 Scores 表(列:score_id、student_id、subject、score),回答以下问题。

score_id | student_id | subject | score
---------|------------|---------|------
1        | 1          | Math    | 92
2        | 1          | CS      | 88
3        | 2          | Math    | 78
4        | 3          | CS      | 95
(a) Write a SQL query to count the number of rows in the Scores table where subject = 'CS'. State the expected result.写一个 SQL 查询,统计 Scores 表中 subject = 'CS' 的行数。说出预期结果。 [2]
(b) Write a SQL query to return the student_id and score for all rows where subject = 'Math' AND score >= 85.写一个 SQL 查询,返回所有 subject = 'Math'score >= 85 的行的 student_idscore [2]
(c) CSTA 3A-DA-10 says to "evaluate tradeoffs in how data is organized." Compare storing these scores as a CSV file vs a relational database. Give one advantage of each approach. Then state which approach you would recommend for a school with 500 students and 10 subjects, and justify in two sentences.CSTA 3A-DA-10 要求"评估数据组织方式的权衡"。比较将这些成绩存储为 CSV 文件与关系数据库。各给出一个优点。然后说明对于拥有 500 名学生和 10 门课程的学校,你会推荐哪种方法,并用两句话说明理由。 [3]
PART III  ·  MODELING / APPLIED第三部分  ·  建模与应用Universal / multi-region applied · 25 marks通用/多地区应用题 · 共 25 分

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

Read each scenario carefully before answering. Write HTML tags exactly (include angle brackets). Write SQL statements in full. Write Python code with correct indentation. Conclude each question with a one-sentence summary where indicated.动笔前仔细阅读每个场景。HTML 标签须精确书写(包括尖括号)。SQL 语句须完整书写。Python 代码须正确缩进。在指示处以一句完整的结论句作答。

Q10 MEDIUM 🇺🇸 US 🇨🇦 ON AP CSP-feeder FRQAP CSP 衔接简答题 §6 HTML and the WebHTML 与网络 · AB CSE1210 [8 marks][8 分]

A student is building a simple web page for a school library catalogue. They produce the following HTML, which contains errors.一名学生正在为学校图书馆目录构建一个简单的网页。他们写出了以下 HTML,其中包含错误。

<!DOCTYPE html>
<html>
<head>
  <title>Library Catalogue<title>
</head>
<body>
  <h1>Library Catalogue</h1>
  <p>Welcome to the library.
  <a href="books.html">Browse Books</p>
  <ul>
    <li>Python Primer</li>
    <li>Data Science 101
  </ul>
</body>
</html>
(a) Identify two HTML errors in the code above. For each error, state the line or element involved and write the correction.找出以上代码中的两个 HTML 错误。对每个错误,说明涉及的行或元素,并写出更正。 [4]
(b) The student wants to add a table showing two books with columns "Title" and "Author". Write the complete HTML for this table (two data rows: "Python Primer / Smith, J." and "Web Basics / Patel, R.").学生想添加一个显示两本书的表格,列名为"Title"和"Author"。为此表格写出完整的 HTML(两行数据:"Python Primer / Smith, J." 和 "Web Basics / Patel, R.")。 [3]
(c) State which HTML section (<head> or <body>) contains the page's visible content, and give one example of something that belongs in the other section.说明哪个 HTML 部分(<head><body>)包含页面的可见内容,并举一个属于另一部分的内容的例子。 [1]
Q11 MEDIUM 🇨🇦 ON 🇨🇦 BC ON Provincial-style安大略省考风格 §7 JSON and APIsJSON 与接口 · CSTA 3A-DA-12 [9 marks][9 分]

A weather API returns the following JSON response. Study the Python program that processes it.天气接口返回以下 JSON 响应。研究处理它的 Python 程序。

{
  "city": "Vancouver",
  "temperature": 18.5,
  "humidity": 72,
  "forecast": {
    "tomorrow": "sunny",
    "temp_high": 22.0
  },
  "alerts": ["fog warning", "wind advisory"]
}
import json

response_text = '{"city": "Vancouver", "temperature": 18.5, "humidity": 72, "forecast": {"tomorrow": "sunny", "temp_high": 22.0}, "alerts": ["fog warning", "wind advisory"]}'

data = json.loads(response_text)
city = data["city"]
tomorrow = data["forecast"]["tomorrow"]
first_alert = data["alerts"][0]
print(f"{city}: tomorrow will be {tomorrow}. Alert: {first_alert}")
(a) State the exact output of the program.写出程序的精确输出。 [2]
(b) Explain step by step how data["forecast"]["tomorrow"] accesses the value "sunny".逐步解释 data["forecast"]["tomorrow"] 如何访问值 "sunny" [2]
(c) State the Python expression to access the second alert in the "alerts" list.写出访问 "alerts" 列表中第二个警告的 Python 表达式。 [1]
(d) Identify two JSON syntax rules that differ from Python dictionary syntax. Give one example of each difference.指出 JSON 语法与 Python 字典语法不同的两条规则。各举一个例子。 [2]
(e) Explain what json.loads() does and what Python type it returns when the input is a JSON object.解释 json.loads() 的作用,以及当输入是 JSON 对象时它返回什么 Python 类型。 [2]
Q12 HARD 🇺🇸 US 🇨🇦 ON 🇨🇦 BC AP CSP-feeder FRQAP CSP 衔接简答题 §6 + §7 HTML + JSON + data pipelineHTML + JSON + 数据管线 · CSTA 3A-DA-12 [8 marks][8 分]

A student builds a data pipeline: an API returns student scores as JSON; the program parses them and generates an HTML table to display on a web page.一名学生构建数据管线:接口以 JSON 格式返回学生成绩;程序解析后生成 HTML 表格在网页上显示。

import json

api_response = '''
[
  {"name": "Alice", "score": 92},
  {"name": "Bob",   "score": 78},
  {"name": "Carol", "score": 95}
]
'''

students = json.loads(api_response)

html = "<table>\n  <tr><th>Name</th><th>Score</th></tr>\n"
for s in students:
    html += f"  <tr><td>{s['name']}</td><td>{s['score']}</td></tr>\n"
html += "</table>"
print(html)
(a) What Python type does json.loads(api_response) return in this case: a dict or a list? Explain how you can tell from the JSON structure.json.loads(api_response) 在这里返回什么 Python 类型:字典还是列表?解释如何从 JSON 结构判断。 [2]
(b) Write the complete HTML table output that the program prints. Include all rows.写出程序打印的完整 HTML 表格输出。包含所有行。 [3]
(c) The student wants to highlight rows where score >= 90 by wrapping the <tr> tag with a CSS class: <tr class="high">. Modify the for loop (pseudocode or Python) to add this class only when the score meets the threshold.学生希望通过给 <tr> 标签添加 CSS 类 <tr class="high"> 来高亮 score >= 90 的行。修改 for 循环(伪代码或 Python),使其仅在成绩达到阈值时添加此类。 [2]
(d) In one sentence, explain how this program demonstrates the data-to-information transformation described in Section 1 of the Study Guide.用一句话解释该程序如何体现学习指南第 1 节中描述的数据到信息的转换。 [1]

🇺🇸 US CSTA / AP CSP美国 CSTA / AP CSP3A-DA-10 · 3A-DA-11 · 3A-DA-12 · DAT-2.C
🇨🇦 Ontario安大略ICS4U A3.1 · ICS4C A2.2 · CSE2130
🇨🇦 British Columbia不列颠哥伦比亚CS 11 / APCSP: data, files, structured data, web conceptsCS 11 / APCSP:数据、文件、结构化数据、网络概念
🇨🇦 Alberta阿尔伯塔CSE2130 outcomes 1.4.2, 1.4.4, 1.4.5; CSE1210/1220 web scriptingCSE2130 结果 1.4.2、1.4.4、1.4.5;CSE1210/1220 网页脚本

Full Syllabus Map in Study Guide: ../Study Guides/Unit_10_Data_Databases_and_the_Web.html. CS has no AB standalone diploma exam; AB framing uses CSE outcomes. SQL questions are ICS4C (college stream); ICS4U students see file I/O and CSV only.完整大纲对照见学习指南:../Study Guides/Unit_10_Data_Databases_and_the_Web.html。CS 无独立 AB 毕业考;AB 题使用 CSE 结果框架。SQL 题属 ICS4C(学院流);ICS4U 学生仅见文件 I/O 和 CSV。