Practice Questions · AP CSA-Feeder · US / ON / BC / AB Styles练习题集 · AP CSA 衔接 · 美 / 安 / 卑 / 阿省风格
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;作答前逐行追踪。
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)。下列哪项正确描述了 Dog 与 fido 的关系?
Dog is an instance of fidoDog 是 fido 的一个实例fido is an instance of the Dog class (blueprint)fido 是 Dog 类(蓝图)的一个实例Dog and fido are two separate classesDog 和 fido 是两个独立的类fido is a method defined inside Dogfido 是在 Dog 内部定义的方法What does self refer to inside an instance method?self 在实例方法内部指代什么?
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.name after the code runs.写出代码运行后 a.name 的值。 [1]b.age after the code runs.写出代码运行后 b.age 的值。 [1]a.age did not affect b.age.用一句话解释为什么修改 a.age 没有影响 b.age。 [2]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())
c.count after each line that modifies it.逐步追踪程序。写出每行修改 c.count 后的值。 [3]increment, reset, and get_count are mutators or accessors. Justify in one sentence for each.对每个方法分类:说明 increment、reset 和 get_count 分别是修改器还是访问器,各用一句话说明理由。 [2]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)
__init__ and their values for r1.列出 __init__ 为 r1 设置的两个实例属性及其值。 [2]r1.area() and r2.area().写出 r1.area() 和 r2.area() 的返回值。 [2]r1.is_square() and r2.is_square(). Explain why each value is correct.写出 r1.is_square() 和 r2.is_square() 的返回值,并解释各值正确的原因。 [2]r1 = Rectangle(6, 4). Mention __init__ in your answer.用一句话解释 Python 执行 r1 = Rectangle(6, 4) 时内部发生了什么。答案中须提及 __init__。 [2]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。论证题两句推理即可满分。如有帮助,可用属性表格追踪对象状态。
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)
s1.grade and s2.grade after all four client lines execute.写出四行客户代码全部执行后 s1.grade 和 s2.grade 的值。 [2]s1.is_passing() and s2.is_passing() at that point. Explain each.此时 s1.is_passing() 和 s2.is_passing() 的返回值各是什么?各自解释。 [2]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]update_grade is a mutator and is_passing is an accessor.用一句话解释为什么 update_grade 是修改器而 is_passing 是访问器。 [1]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())
acc. Show the value of acc.balance after each call, and state what is printed during the calls.追踪对 acc 的四次方法调用。写出每次调用后 acc.balance 的值,并写出调用期间打印的内容。 [4]print(acc.get_balance()) outputs.写出 print(acc.get_balance()) 输出什么。 [1]__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]withdraw method demonstrates encapsulation in protecting the balance from becoming negative.用一句话解释 withdraw 方法如何通过封装保护余额不变为负值。 [2]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()]
total and avg after those lines execute. Show your arithmetic.写出这些行执行后 total 和 avg 的值,展示计算过程。 [3]failing list.写出 failing 列表的内容。 [2]max and a key argument) to find the Student object with the highest grade. Store it in a variable named top.写出一行 Python 代码(使用 max 和 key 参数),找到成绩最高的 Student 对象,存入变量 top。 [2]s.is_passing() is preferable to storing grades in a plain list of integers for this kind of roster analysis.说明对于此类名单分析,遍历对象列表并调用 s.is_passing() 为何优于将成绩存储在普通整数列表中。 [1]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()
savings._balance after each call. Show your arithmetic for add_interest.追踪三次方法调用。写出每次调用后 savings._balance 的值,并展示 add_interest 的计算过程。 [3]super().__init__(owner, balance) does inside SavingsAccount.__init__.解释 super().__init__(owner, balance) 在 SavingsAccount.__init__ 内部的作用。 [2]_balance uses a leading underscore and how that relates to encapsulation in Python.解释 _balance 使用下划线前缀的原因,以及这与 Python 封装的关系。 [2]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。每题以一句完整的结论句作答。
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),
]
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]Design a Product class to model an item in a store inventory. The class must satisfy all of the following requirements:设计一个 Product 类来建模商店库存中的商品。该类必须满足以下所有要求:
name (string), price (float), quantity (int).属性:name(字符串)、price(浮点数)、quantity(整数)。total_value(self): returns price * quantity.方法 total_value(self):返回 price * quantity。restock(self, amount): adds amount to quantity.方法 restock(self, amount):将 amount 加到 quantity。sell(self, amount): subtracts amount from quantity only if enough stock is available; otherwise prints "Not enough stock".方法 sell(self, amount):只有库存充足时才从 quantity 中减去 amount;否则打印 "Not enough stock"。Product class in Python.用 Python 写出完整的 Product 类。 [5]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]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
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 类,包含属性 make、year 和 km。添加方法 add_km(self, distance),将 km 增加 distance。写出完整类。 [3]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]3B-AP-14 · 3B-AP-20 · 3B-AP-21ICS4U C1.1 · C1.2 · ICS4C B2.1Full 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 结果框架。