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

Programming Fundamentals编程基础

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 reasoning in the work space. For short-answer items, state the exact value or type (e.g., "a string, '55'"). Code is pseudocode or Python; trace each step to verify before answering.本部分包含选择题与短答题。选择题请圈出字母,并在答题空白处写出推理过程。短答题写出精确值或类型(如"字符串 '55'")。代码为伪代码或 Python;作答前逐步追踪。

Q1 EASY 🇺🇸 US AP CSP-style MCQAP CSP 风格选择题 §1 Variables and Assignment变量与赋值 · AP CSP AAP-1.B [3 marks][3 分]

After the following three lines execute, what is the value of y?以下三行执行后,y 的值是什么?

x = 4
y = x + 3
x = 10
  1. (A) 10
  2. (B) 13
  3. (C) 7
  4. (D) 4
Q2 EASY 🇺🇸 US AP CSP-style MCQAP CSP 风格选择题 §2 Data Types数据类型 · AP CSP 2.1 / ON ICS3U A1.1 [3 marks][3 分]

Which of the following is the correct data type for the value "3.14" (with quotes) in Python?以下哪项是 Python 中 "3.14"(带引号)的正确数据类型?

  1. (A) int整数 (int)
  2. (B) float浮点数 (float)
  3. (C) str字符串 (str)
  4. (D) bool布尔 (bool)
Q3 MEDIUM 🇨🇦 ON ON Provincial-style安大略省考风格 §3 Input and Output输入与输出 · ICS3U A2.1 / AB CSE1110 2.4.5 [7 marks][7 分]

A student writes the following program. The user types 5 at the prompt.一名学生编写了以下程序。用户在提示符处输入 5

n = input("Enter a number: ")
result = n * 2
print(result)
(a) State the data type of n after line 1 executes. Explain why.写出第 1 行执行后 n 的数据类型。解释原因。 [2]
(b) State the exact value that result holds after line 2 executes. State its data type too.写出第 2 行执行后 result 保存的精确值。同时写出其数据类型。 [2]
(c) The student wants result to be 10 (integer). Write the corrected line 1 that fixes the bug.学生希望 result10(整数)。写出修正后的第 1 行以修复该错误。 [2]
(d) State the Python function used to display output on the screen.写出 Python 中用于在屏幕上显示输出的函数。 [1]
Q4 MEDIUM 🇨🇦 BC 🇨🇦 AB AB/Universal Applied阿省/通用应用题 §4 Operators运算符 · BC CS10 / AB CSE1110 2.4.6 [6 marks][6 分]

Given a = 17 and b = 5, evaluate each expression below.给定 a = 17b = 5,计算以下每个表达式。

(a) State the value of a // b and its data type.写出 a // b 的值及其数据类型。 [2]
(b) State the value of a % b and explain what it represents.写出 a % b 的值,并解释它代表什么。 [2]
(c) Evaluate the expression a > 10 and b == 5 and state whether the result is True or False. Show your reasoning.计算表达式 a > 10 and b == 5,写出结果是 True 还是 False。写出推理过程。 [2]
Q5 MEDIUM 🇨🇦 ON ON Provincial-style安大略省考风格 §5 Type Conversion and Casting类型转换 · ICS3U A1.3 / AB CSE1110 2.4.3 [6 marks][6 分]

For each line of Python below, state the result and its data type. If the line raises an error, name the error.对以下每行 Python,写出结果及其数据类型。若该行引发错误,请说明错误名称。

(a) int(7.9) [2]
(b) str(42) + " points" [2]
(c) int("3.9") [2]
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. State data types whenever asked. Calculator not applicable; trace by hand.写出每一步追踪过程。伪代码或 Python 须清晰书写,缩进正确。论证题两句推理即可满分。有要求时须写明数据类型。无需计算器;手工追踪。

Q6 EASY 🇺🇸 US AP CSP-style MCQAP CSP 风格选择题 §6 Constants and Naming Conventions常量与命名规范 · ON ICS3U A1.1 / AB CSE1110 2.4.4 [5 marks][5 分]

Answer each naming question below.回答以下每道命名问题。

(a) Which variable name follows Python's snake_case convention for a variable storing a student's total score?哪个变量名遵循 Python 的 snake_case 规范,用于存储学生的总分? [1]
  1. (A) TotalScore
  2. (B) total_score
  3. (C) TOTAL_SCORE
  4. (D) ts
(b) A tax-rate value of 0.13 should not change during the program. Write the correctly named Python line that declares it as a constant.税率值 0.13 在程序运行中不应改变。写出将其声明为常量的正确命名 Python 语句。 [1]
(c) Explain in two sentences why using TAX_RATE = 0.13 instead of the literal 0.13 throughout the code is better practice.用两句话解释为什么在代码中使用 TAX_RATE = 0.13 而不是字面值 0.13 是更好的实践。 [2]
(d) State the naming convention for Python class names (not variables, not constants).写出 Python 类名(非变量、非常量)的命名规范。 [1]
Q7 MEDIUM 🇨🇦 ON ON Provincial-style安大略省考风格 §1 + §3 Variables, Assignment, and I/O变量、赋值与输入输出 · ICS3U A1.3 / A2.1 [8 marks][8 分]

A student writes a program to swap two variables and then print the result. Assume the user types 10 for the first prompt and 20 for the second.一名学生编写程序来交换两个变量并打印结果。假设用户第一个提示符输入 10,第二个输入 20

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
temp = a
a = b
b = temp
print(a, b)
(a) Trace the program line by line. For each of lines 3-5, state the value of a, b, and temp after that line executes.逐行追踪程序。对第 3-5 行,写出该行执行后 abtemp 的值。 [3]
(b) State the output of line 6.写出第 6 行的输出。 [1]
(c) Explain why a temp variable is required for the swap. What would happen if you wrote a = b followed directly by b = a without using temp?解释为什么交换时需要 temp 变量。若直接写 a = b 然后 b = a 而不使用 temp,会发生什么? [2]
(d) Rewrite the swap as a single Python line using tuple unpacking. State the line.用元组解包将交换改写为一行 Python。写出该行。 [2]
Q8 HARD 🇨🇦 BC 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §4 + §5 Operators and Type Conversion — Debugging运算符与类型转换——调试 · BC CS10 / AB CSE1110 2.4.3 / 2.4.6 [8 marks][8 分]

Each buggy program below contains one or more errors. Identify the error(s) and write the corrected code.以下每个有问题的程序包含一个或多个错误。识别错误,并写出更正后的代码。

Program A:程序 A:

score = input("Enter score: ")
bonus = score + 10
print("Final score:", bonus)

Program B:程序 B:

price = float(input("Enter price: "))
message = "Total is " + price
print(message)
(a) Identify the error in Program A. Name the Python exception it raises and explain why.识别程序 A 中的错误。说明它引发的 Python 异常名称及原因。 [2]
(b) Write the corrected Program A. The corrected version should output Final score: 95 when the user enters 85.写出更正后的程序 A。当用户输入 85 时,更正版应输出 Final score: 95 [2]
(c) Identify the error in Program B. Name the exception and explain the fix needed.识别程序 B 中的错误。说明异常名称,并解释所需修复。 [2]
(d) Write the corrected Program B line that builds the message string. You may use either str() concatenation or an f-string.写出更正后的程序 B 中构建消息字符串的那一行。可以使用 str() 拼接或 f 字符串。 [2]
Q9 HARD Honors荣誉级 🇺🇸 US AP CSP-feeder FRQAP CSP 衔接简答题 §7 First Program — IPO design and edge cases编写第一个程序——IPO 设计与边界情况 · AB CSE1110 2.4.1 / ON ICS3U A2.1 [9 marks][9 分]

A student must write a program that reads a weight in kilograms and a height in metres from the user, computes the Body Mass Index (BMI = weight / height squared), and prints the BMI rounded to one decimal place.一名学生须编写一个程序:从用户读取体重(千克)和身高(米),计算体重指数(BMI = 体重 / 身高平方),并打印保留一位小数的 BMI。

(a) State the IPO (Input, Process, Output) for this program. List each input, the process formula, and the output.写出该程序的 IPO(输入、处理、输出)。列出每个输入、处理公式和输出。 [2]
(b) Write the complete Python program using the five-step pipeline. Use float(input(...)) for reading inputs and an f-string with :.1f for the output.用五步流程编写完整 Python 程序。使用 float(input(...)) 读取输入,用带 :.1f 的 f 字符串输出。 [3]
(c) Trace your program with weight = 70 and height = 1.75. Show the calculation and state the printed output.weight = 70height = 1.75 追踪你的程序。写出计算过程,并写出打印的输出。 [2]
(d) Identify two edge-case inputs that could cause your program to behave unexpectedly. For each, name the Python error (if any) that would occur.识别两种可能导致程序意外行为的边界情况输入。对每种情况,说明会发生的 Python 错误名称(如果有)。 [2]
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 (INPUT, SET, OUTPUT). Where Python is requested, show indentation correctly. Conclude each question with a one-sentence answer.动笔前仔细阅读每个场景。伪代码使用标准格式(INPUT、SET、OUTPUT)。Python 须正确缩进。每题以一句完整的结论句作答。

Q10 MEDIUM 🇺🇸 US 🇨🇦 ON AP CSP-feeder FRQAP CSP 衔接简答题 §2 + §3 + §4 Data types, I/O, and operators — applied数据类型、输入输出与运算符——应用 · ICS3U A1.1 / A2.1 / AB CSE1110 2.4.6 [8 marks][8 分]

A program reads the number of minutes a student studied and converts it to hours and leftover minutes. For example, 200 minutes = 3 hours and 20 minutes.一个程序读取学生学习的分钟数,并将其转换为小时数和剩余分钟数。例如,200 分钟 = 3 小时 20 分钟。

minutes = int(input("Enter total minutes studied: "))
hours = minutes // 60
leftover = minutes % 60
print(hours, "hours and", leftover, "minutes")
(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]
(a) Trace the program for the input 200. State the values of hours and leftover after lines 2 and 3 execute. Show your arithmetic.用输入 200 追踪程序。写出第 2、3 行执行后 hoursleftover 的值。写出算术过程。 [3]
(b) State the exact output of the program when the input is 200.写出输入为 200 时程序的精确输出。 [1]
(c) Why is the int() conversion on line 1 necessary? What error would occur without it when the user types 200?为什么第 1 行的 int() 转换是必要的?若没有它,用户输入 200 时会发生什么错误? [2]
(d) Write the pseudocode for this program using the standard block format (INPUT, SET, OUTPUT).使用标准块格式(INPUT、SET、OUTPUT)为该程序编写伪代码。 [2]
Q11 MEDIUM 🇨🇦 ON 🇨🇦 BC ON Provincial-style安大略省考风格 §5 + §6 + §7 Type conversion, naming, and IPO design类型转换、命名与 IPO 设计 · ICS3U A1.3 / A2.1 / AB CSE1110 2.4.1 [8 marks][8 分]

A student is writing a program to compute an HST-inclusive price. Ontario HST rate is 13%. The program should read a pre-tax price, compute the tax, and display the final price with two decimal places. The student's first draft is shown below.一名学生正在编写一个程序,计算含 HST 的价格。安大略省 HST 税率为 13%。程序应读取含税前价格,计算税额,并以两位小数显示最终价格。学生的初稿如下。

# Draft 1
p = input("Enter price: ")
t = p * 0.13
f = p + t
print("Final:", f)
(a) Identify two errors in Draft 1 and explain each in one sentence.识别初稿 1 中的两处错误,每处用一句话解释。 [2]
(b) Rewrite the program using descriptive names and a named constant for the tax rate. Use an f-string with :.2f for the output line. The corrected version should handle decimal inputs correctly.用描述性名称和税率具名常量重写程序。输出行使用带 :.2f 的 f 字符串。更正版应正确处理小数输入。 [4]
(c) Trace your corrected program with an input of 100.00. State the values of each variable and the printed output.用输入 100.00 追踪你的更正程序。写出每个变量的值以及打印的输出。 [2]
Q12 HARD 🇺🇸 US 🇨🇦 ON 🇨🇦 BC AP CSP-feeder FRQAP CSP 衔接简答题 All sections · Full program design and debugging全节综合 · 完整程序设计与调试 · AP CSP AAP-1 / AB CSE1110 2.4.1-2.4.7 [9 marks][9 分]

A student is writing a program that asks the user for their first name and birth year, then prints a personalised message with their age. Today's year is 2026. The student's buggy version is shown below.一名学生正在编写一个程序,询问用户的名字和出生年份,然后打印带有年龄的个性化消息。今年是 2026 年。学生的有错误版本如下。

CURRENT_YEAR = 2026
name = input("Enter your first name: ")
birth_year = input("Enter your birth year: ")
age = CURRENT_YEAR - birth_year
message = "Hello " + name + "! You are " + age + " years old."
print(message)
(a) Identify the two errors in this program. For each error, name the Python exception it would raise and explain why.识别该程序中的两处错误。对每处错误,说明它会引发的 Python 异常名称及原因。 [4]
(b) Write the corrected complete program. The naming convention for the constant should be preserved.写出更正后的完整程序。应保留常量的命名规范。 [3]
(c) Trace your corrected program for name "Li" and birth year 2010. State the exact printed output.用名字 "Li"、出生年份 2010 追踪你的更正程序。写出精确的打印输出。 [2]

🇺🇸 US CSTA / AP CSP美国 CSTA / AP CSPAAP-1.A · AAP-1.B · AP CSP 2.1
🇨🇦 Ontario安大略ICS3U A1.1 · A1.3 · A1.4 · A2.1
🇨🇦 British Columbia不列颠哥伦比亚BC CS10/CP11: variables, types, I/O, operators, IPO designBC CS10/CP11:变量、类型、输入输出、运算符、IPO 设计
🇨🇦 Alberta阿尔伯塔CSE1110: outcomes 2.4.1-2.4.7CSE1110:结果 2.4.1-2.4.7

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