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

Object-Oriented Programming (Intro)面向对象编程(入门)

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

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


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

Section A · Short ResponseA 部分 · 短答题

Questions mix multiple-choice and short-answer items. For MCQs, circle the letter and justify in one sentence in the work space. For short-answer items, write precisely using OOP vocabulary (class, instance, attribute, method, constructor, self). Code is Python; trace each line to verify before answering.本部分包含选择题与短答题。选择题请圈出字母,并在答题空白处用一句话说明理由。短答题用 OOP 词汇(类、实例、属性、方法、构造函数、self)精确作答。代码为 Python;作答前逐行追踪。

Q1 EASY 🇺🇸 US AP CSA-style MCQAP CSA 风格选择题 §1 Class vs Object类与对象 · CSTA 3B-AP-14 [3 marks][3 分]

A programmer writes class Dog: and later writes fido = Dog("Fido", 3). Which statement correctly identifies the relationship between Dog and fido?程序员写了 class Dog:,随后写了 fido = Dog("Fido", 3)。下列哪项正确描述了 Dogfido 的关系?

  1. (A) Dog is an instance of fidoDogfido 的一个实例
  2. (B) fido is an instance of the Dog class (blueprint)fidoDog 类(蓝图)的一个实例
  3. (C) Dog and fido are two separate classesDogfido 是两个独立的类
  4. (D) fido is a method defined inside Dogfido 是在 Dog 内部定义的方法
Q2 EASY 🇺🇸 US AP CSA-style MCQAP CSA 风格选择题 §2 Class Syntax / self类语法 / self · CSTA 3B-AP-20 [3 marks][3 分]

What does self refer to inside an instance method?self 在实例方法内部指代什么?

  1. (A) The class definition itself类定义本身
  2. (B) A copy of the class stored in memory存储在内存中的类的副本
  3. (C) The specific object on which the method was called调用该方法的具体对象
  4. (D) The first argument supplied by the caller when calling the method调用方法时由调用者提供的第一个实参
Q3 EASY 🇨🇦 ON ON Provincial-style安大略省考风格 §3 Attributes (Instance Variables)属性(实例变量) · ICS4C B2.1 [4 marks][4 分]

Consider the following class.考察以下类。

class Cat:
    def __init__(self, name, age):
        self.name = name
        self.age  = age

a = Cat("Whiskers", 4)
b = Cat("Luna", 2)
a.age = 5
(a) State the value of a.name after the code runs.写出代码运行后 a.name 的值。 [1]
(b) State the value of b.age after the code runs.写出代码运行后 b.age 的值。 [1]
(c) Explain in one sentence why changing a.age did not affect b.age.用一句话解释为什么修改 a.age 没有影响 b.age [2]
Q4 MEDIUM 🇨🇦 AB AB/Universal Applied阿省/通用应用题 §4 Methods: mutator and accessor方法:修改器与访问器 · CSE3120 1.1.3 [7 marks][7 分]

Consider the following class.考察以下类。

class Counter:
    def __init__(self, start=0):
        self.count = start

    def increment(self, amount=1):
        self.count += amount

    def reset(self):
        self.count = 0

    def get_count(self):
        return self.count

c = Counter(10)
c.increment()
c.increment(3)
print(c.get_count())
c.reset()
print(c.get_count())
(a) Trace the program step by step. Show the value of c.count after each line that modifies it.逐步追踪程序。写出每行修改 c.count 后的值。 [3]
(b) State the two values printed by the program in order.按顺序写出程序打印的两个值。 [2]
(c) Classify each method: state whether increment, reset, and get_count are mutators or accessors. Justify in one sentence for each.对每个方法分类:说明 incrementresetget_count 分别是修改器还是访问器,各用一句话说明理由。 [2]
Q5 MEDIUM 🇨🇦 BC BC Provincial-style卑诗省考风格 §5 Constructor (__init__)构造函数(__init__) · ICS4U C1.1 [8 marks][8 分]

A student writes the following class to model a rectangle.一名学生编写以下类来建模矩形。

class Rectangle:
    def __init__(self, width, height):
        self.width  = width
        self.height = height

    def area(self):
        return self.width * self.height

    def is_square(self):
        return self.width == self.height

r1 = Rectangle(6, 4)
r2 = Rectangle(5, 5)
(a) List the two instance attributes set by __init__ and their values for r1.列出 __init__r1 设置的两个实例属性及其值。 [2]
(b) State the return value of r1.area() and r2.area().写出 r1.area()r2.area() 的返回值。 [2]
(c) State the return value of r1.is_square() and r2.is_square(). Explain why each value is correct.写出 r1.is_square()r2.is_square() 的返回值,并解释各值正确的原因。 [2]
(d) Explain in one sentence what happens internally when Python executes r1 = Rectangle(6, 4). Mention __init__ in your answer.用一句话解释 Python 执行 r1 = Rectangle(6, 4) 时内部发生了什么。答案中须提及 __init__ [2]
PART II  ·  EXTENDED RESPONSE第二部分  ·  简答题AP CSA-feeder FRQ + Honors · 31 marksAP CSA 衔接简答题 + 荣誉级 · 共 31 分

Section B · Extended ResponseB 部分 · 简答题

Show all your reasoning. Write Python code with correct indentation and self on every method. For explain/justify questions, two sentences of reasoning earn full marks. Trace object state by drawing an attribute table where helpful.展示全部推理过程。Python 代码须正确缩进,每个方法均须有 self。论证题两句推理即可满分。如有帮助,可用属性表格追踪对象状态。

Q6 MEDIUM 🇺🇸 US AP CSA-feeder FRQAP CSA 衔接简答题 §2 + §3 Define class + trace attributes定义类 + 追踪属性 · CSTA 3B-AP-20 [8 marks][8 分]

Read the following class definition and the four lines of client code that follow it.阅读以下类定义及其后的四行客户代码。

class Student:
    def __init__(self, name, grade):
        self.name  = name
        self.grade = grade

    def is_passing(self):
        return self.grade >= 50

    def update_grade(self, new_grade):
        self.grade = new_grade

s1 = Student("Alice", 78)
s2 = Student("Bob",   42)
s1.update_grade(85)
s2.update_grade(55)
(a) State the value of s1.grade and s2.grade after all four client lines execute.写出四行客户代码全部执行后 s1.grades2.grade 的值。 [2]
(b) State the return value of s1.is_passing() and s2.is_passing() at that point. Explain each.此时 s1.is_passing()s2.is_passing() 的返回值各是什么?各自解释。 [2]
(c) The programmer wants to add a method letter_grade(self) that returns "A" if grade ≥ 80, "B" if grade ≥ 70, "C" if grade ≥ 60, "D" if grade ≥ 50, and "F" otherwise. Write the complete method.程序员想添加方法 letter_grade(self):成绩 ≥ 80 返回 "A",≥ 70 返回 "B",≥ 60 返回 "C",≥ 50 返回 "D",否则返回 "F"。写出完整方法。 [3]
(d) Explain in one sentence why update_grade is a mutator and is_passing is an accessor.用一句话解释为什么 update_grade 是修改器而 is_passing 是访问器。 [1]
Q7 MEDIUM 🇨🇦 ON ON Provincial-style安大略省考风格 §4 + §5 Methods + constructor design方法 + 构造函数设计 · ICS4C B2.1 [8 marks][8 分]

A program models a simple bank account. The class below is partially written.一个程序建模简单银行账户。以下类部分已写好。

class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner   = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
        else:
            print("Insufficient funds")

    def get_balance(self):
        return self.balance

acc = BankAccount("Carol", 500)
acc.deposit(200)
acc.withdraw(100)
acc.withdraw(700)
print(acc.get_balance())
(a) Trace the four method calls on acc. Show the value of acc.balance after each call, and state what is printed during the calls.追踪对 acc 的四次方法调用。写出每次调用后 acc.balance 的值,并写出调用期间打印的内容。 [4]
(b) State what print(acc.get_balance()) outputs.写出 print(acc.get_balance()) 输出什么。 [1]
(c) The default parameter in __init__(self, owner, balance=0) means balance is optional. Write the single line of code that creates a new account for "David" with no starting balance.__init__(self, owner, balance=0) 中的默认参数使 balance 可选。写出为"David"创建无初始余额账户的一行代码。 [1]
(d) Explain in one sentence how the withdraw method demonstrates encapsulation in protecting the balance from becoming negative.用一句话解释 withdraw 方法如何通过封装保护余额不变为负值。 [2]
Q8 HARD 🇨🇦 BC 🇺🇸 US AP CSA-feeder FRQAP CSA 衔接简答题 §5 + §6 Constructor + list of objects构造函数 + 对象列表 · CSE3120 2.3.3 [8 marks][8 分]

A roster of students is stored as a list of objects. Study the code below.学生名单以对象列表的形式存储。研究以下代码。

class Student:
    def __init__(self, name, grade):
        self.name  = name
        self.grade = grade

    def is_passing(self):
        return self.grade >= 50

roster = [
    Student("Alice",  88),
    Student("Bob",    45),
    Student("Carol",  72),
    Student("David",  50),
]

total = sum(s.grade for s in roster)
avg   = total / len(roster)

failing = [s.name for s in roster if not s.is_passing()]
(a) State the value of total and avg after those lines execute. Show your arithmetic.写出这些行执行后 totalavg 的值,展示计算过程。 [3]
(b) State the contents of the failing list.写出 failing 列表的内容。 [2]
(c) Write one line of Python code (using max and a key argument) to find the Student object with the highest grade. Store it in a variable named top.写出一行 Python 代码(使用 maxkey 参数),找到成绩最高的 Student 对象,存入变量 top [2]
(d) State why iterating over a list of objects and calling s.is_passing() is preferable to storing grades in a plain list of integers for this kind of roster analysis.说明对于此类名单分析,遍历对象列表并调用 s.is_passing() 为何优于将成绩存储在普通整数列表中。 [1]
Q9 HARD Honors荣誉级 🇺🇸 US AP CSA-feeder FRQAP CSA 衔接简答题 §7 Encapsulation + Inheritance封装 + 继承 · CSE3120 1.1.2 / ICS4U C1.2 [7 marks][7 分]

Study the following two-class hierarchy.研究以下两个类的层次结构。

class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner    = owner
        self._balance = balance

    def deposit(self, amount):
        if amount > 0:
            self._balance += amount

    def get_balance(self):
        return self._balance

class SavingsAccount(BankAccount):
    def __init__(self, owner, balance, rate):
        super().__init__(owner, balance)
        self.rate = rate

    def add_interest(self):
        self._balance *= (1 + self.rate)

savings = SavingsAccount("Emma", 1000, 0.05)
savings.deposit(200)
savings.add_interest()
(a) Trace the three method calls. State the value of savings._balance after each call. Show your arithmetic for add_interest.追踪三次方法调用。写出每次调用后 savings._balance 的值,并展示 add_interest 的计算过程。 [3]
(b) Explain what super().__init__(owner, balance) does inside SavingsAccount.__init__.解释 super().__init__(owner, balance)SavingsAccount.__init__ 内部的作用。 [2]
(c) Explain why _balance uses a leading underscore and how that relates to encapsulation in Python.解释 _balance 使用下划线前缀的原因,以及这与 Python 封装的关系。 [2]
PART III  ·  MODELING / APPLIED第三部分  ·  建模与应用Universal / multi-region applied · 25 marks通用/多地区应用题 · 共 25 分

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

Read each scenario carefully before designing or tracing code. Where code is requested, write Python with correct indentation and self on every method. Conclude each question with a one-sentence summary.动笔前仔细阅读每个场景。需要代码时,写 Python 并保证正确缩进,每个方法均须有 self。每题以一句完整的结论句作答。

Q10 MEDIUM 🇺🇸 US 🇨🇦 ON AP CSA-feeder FRQAP CSA 衔接简答题 §6 Object list + iteration对象列表 + 迭代 · ICS4U C1.1 [8 marks][8 分]

A teacher tracks quiz scores using Student objects. Consider the following partial program.一位教师用 Student 对象追踪测验分数。考察以下部分程序。

class Student:
    def __init__(self, name, score):
        self.name  = name
        self.score = score

    def passed(self):
        return self.score >= 60

students = [
    Student("Amy",   75),
    Student("Ben",   55),
    Student("Cleo",  90),
    Student("Dan",   60),
    Student("Eve",   48),
]
(a) Write a Python loop that prints the name of every student who did NOT pass. State which names would be printed for this roster.写出打印所有未通过学生姓名的 Python 循环,并写出该名单中哪些名字会被打印。 [3]
(b) Write Python code to compute and print the class average score. Show the arithmetic for this specific list.写出计算并打印班级平均分的 Python 代码,并展示该具体名单的计算过程。 [3]
(c) Explain in one sentence why using a list of Student objects (rather than two parallel lists, one for names and one for scores) makes the code easier to maintain when adding new student data.用一句话解释为什么使用 Student 对象列表(而非两个平行列表,一个存姓名一个存分数)在添加新学生数据时更易维护。 [2]
Q11 MEDIUM 🇨🇦 ON 🇨🇦 BC ON Provincial-style安大略省考风格 §3 + §4 + §5 Design a class from scratch从零设计一个类 · ICS4C B2.1 / CSE3120 3.3 [9 marks][9 分]

Design a Product class to model an item in a store inventory. The class must satisfy all of the following requirements:设计一个 Product 类来建模商店库存中的商品。该类必须满足以下所有要求:

(a) Write the complete Product class in Python.用 Python 写出完整的 Product 类。 [5]
(b) Create a Product object for a widget priced at 2.50 with 10 in stock. Then call sell(15) on it and trace what happens. State what is printed and the final value of quantity.创建一个单价 2.50、库存 10 件的 Product 对象(名为 "Widget")。对其调用 sell(15),追踪发生了什么。写出打印内容和 quantity 的最终值。 [2]
(c) State which of the three methods are mutators and which is an accessor.说明三个方法中哪些是修改器,哪个是访问器。 [2]
Q12 HARD 🇺🇸 US 🇨🇦 ON 🇨🇦 BC AP CSA-feeder FRQAP CSA 衔接简答题 §1 + §7 OOP vs procedural + inheritance designOOP vs 过程式 + 继承设计 · CSTA 3B-AP-21 / ICS4U C1.2 [8 marks][8 分]

A programmer is modelling a vehicle fleet. They currently store data in separate variables:一名程序员正在建模车队。他们目前将数据存储在独立变量中:

# Procedural approach
car1_make  = "Toyota"
car1_year  = 2020
car1_km    = 15000
car2_make  = "Honda"
car2_year  = 2019
car2_km    = 22000
(a) State two problems with the procedural approach above when the fleet grows to 100 vehicles.说明当车队扩大到 100 辆时,上述过程式方式存在的两个问题。 [2]
(b) Design a Vehicle class with attributes make, year, and km. Add a method add_km(self, distance) that increases km by distance. Write the complete class.设计一个 Vehicle 类,包含属性 makeyearkm。添加方法 add_km(self, distance),将 km 增加 distance。写出完整类。 [3]
(c) A ElectricVehicle class should extend Vehicle and add a battery_kwh attribute and a charge(self, kwh) method that adds to battery_kwh. Write the complete ElectricVehicle class, including the call to super().__init__.ElectricVehicle 类应继承 Vehicle,并添加 battery_kwh 属性和 charge(self, kwh) 方法(将 kwh 加到 battery_kwh)。写出完整的 ElectricVehicle 类,包括对 super().__init__ 的调用。 [3]

🇺🇸 US CSTA / AP CSA美国 CSTA / AP CSA3B-AP-14 · 3B-AP-20 · 3B-AP-21
🇨🇦 Ontario安大略ICS4U C1.1 · C1.2 · ICS4C B2.1
🇨🇦 British Columbia不列颠哥伦比亚CP 12: classes, objects, attributes, methodsCP 12:类、对象、属性、方法
🇨🇦 Alberta阿尔伯塔CSE3120: outcomes 1.1, 1.1.2, 1.1.3, 1.1.5, 2.3.3CSE3120:结果 1.1、1.1.2、1.1.3、1.1.5、2.3.3

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