← All Units← 返回单元列表 ← Course Hub← 课程主页
A P C O M P U T E R S C I E N C E P R I N C I P L E S
Unit 3 · Solutions第 3 单元 · 解析

Algorithms and Programming — Solutions算法与编程 —— 解析

Companion to the AP-Style MC Practice SetAP 风格选择题练习的解析配套

MEDIUM HARD AP MC

Unit 3: Algorithms and Programming第 3 单元:算法与编程CSP



MULTIPLE CHOICEWorked Answers详细解析

Multiple Choice — Worked Answers选择题(Multiple Choice)—— 详细解析

Each item restates the prompt and choices, marks the correct letter, and gives a brief justification. Trap distractors are called out where useful.每道题重述题干与选项、标出正确答案字母,并给出简明解析;对容易混淆的干扰项(trap distractor)单独提示。

Q1MEDIUMAP MC3.1 Variables and Assignments

Consider the following code segment.请考虑以下代码段。

a ← 17
b ← a
 a ← a MOD 5
b ← b / 5
DISPLAY(a)
DISPLAY(b)

What is displayed?显示的结果是什么?

Answer:答案: (D)
  • Assigning a to b copies 17, so the later change to a does not change b.
  • 17 MOD 5 is 2, and 17 / 5 is 3.4. Each DISPLAY adds a space.
Option A incorrectly uses the new value of a for b. Option B reverses the displayed values. Option C treats MOD as integer division.
  • 将 a 赋给 b 时复制了 17,因此之后更改 a 不会改变 b。
  • 17 MOD 5 等于 2,17 / 5 等于 3.4。每次 DISPLAY 后都有一个空格。
选项 A 错误地用 a 的新值计算 b。选项 B 颠倒了显示顺序。选项 C 将 MOD 误认为整数除法。
Q2HARDAP MC3.2 Data Abstraction

A program stores related readings in a list and then makes a copy.程序将相关读数存储在一个列表(list)中,然后创建其副本。

readings ← [8, 3, 6]
backup ← readings
REMOVE(readings, 2)
INSERT(backup, 2, 9)
DISPLAY(readings[2])
DISPLAY(backup[3])

What is displayed?显示的结果是什么?

Answer:答案: (D)
  • backup is a copy, not an alias of readings.
  • After removal, readings is [8, 6]. After insertion, backup is [8, 9, 3, 6].
  • The values at indices 2 and 3 are 6 and 3.
Option A overlooks both shifts. Option B overlooks the shift after insertion. Option C overlooks the shift after removal.
  • backup 是 readings 的副本,而不是指向同一列表的别名(alias)。
  • 删除后 readings 为 [8, 6];插入后 backup 为 [8, 9, 3, 6]。
  • 索引(index)2 和索引 3 处的值分别为 6 和 3。
选项 A 忽略了两次移动。选项 B 忽略了插入后的元素移动。选项 C 忽略了删除后的元素移动。
Q3MEDIUMAP MC3.4 Strings

A label maker takes the first three characters of the string ORCHID, then the last two characters, and concatenates them in that order.标签制作程序先取字符串(string)ORCHID 的前三个字符,再取最后两个字符,并按此顺序拼接。

What string does the label maker produce?标签制作程序生成什么字符串?

Answer:答案: (D)
  • The first substring is ORC and the second is ID.
  • Concatenating them in order gives ORCID.
Option A takes the first five characters instead. Option B selects a different first substring. Option C reverses the two substrings.
  • 第一个子串(substring)是 ORC,第二个是 ID。
  • 按顺序拼接得到 ORCID。
选项 A 取了前五个字符。选项 B 取错了第一个子串。选项 C 颠倒了两个子串的顺序。
Q4HARDAP MC3.7 Nested Conditionals

A program assigns a category based on a score and absences.程序根据分数和缺勤次数确定类别。

score ← 75
absences ← 2
IF(score ≥ 70)
{
  IF(absences < 2)
  {
    DISPLAY(1)
  }
  ELSE
  {
    DISPLAY(2)
  }
}
ELSE
{
  DISPLAY(3)
}
DISPLAY(score MOD 10)

What is displayed?显示的结果是什么?

Answer:答案: (A)
  • The outer condition is true because 75 is at least 70.
  • The inner condition is false because 2 is not less than 2, so the program displays 2.
  • 75 MOD 10 is 5, which is displayed next.
Option B treats the boundary as included in the inner condition. Option C uses division instead of MOD. Option D executes the outer ELSE even though its condition is true.
  • 由于 75 不小于 70,外层条件为真。
  • 由于 2 不小于 2,内层条件为假,因此程序显示 2。
  • 75 MOD 10 等于 5,随后显示 5。
选项 B 错误地将边界值纳入内层条件。选项 C 用除法代替了 MOD。选项 D 在外层条件为真时执行了外层 ELSE。
Q5HARDAP MC3.8 Iteration

Consider the following code segment.请考虑以下代码段。

x ← 4
REPEAT UNTIL(x ≥ 4)
{
  x ← x + 100
}
REPEAT UNTIL(x ≥ 11)
{
  x ← x + 3
DISPLAY(x)
}

What is displayed?显示的结果是什么?

Answer:答案: (C)
  • The first loop executes zero times because its condition is initially true.
  • The second loop increments and displays x: 7, then 10, then 13.
  • The next condition check stops the loop.
Option A displays before incrementing. Option B stops before displaying the value that first meets the condition. Option D shows only the final value, as if DISPLAY ran once after the loop.
  • 第一个循环的条件一开始就为真,因此循环体执行零次。
  • 第二个循环先增加 x 再显示,依次得到 7、10、13。
  • 下一次检查条件时循环停止。
选项 A 在增加之前显示。选项 B 没有显示首次满足停止条件的值。选项 D 只显示了最终值,好像 DISPLAY 在循环结束后才执行一次。
Q6HARDAP MC3.9 Developing Algorithms

A robot is on a 5 by 5 grid. Coordinates increase eastward and northward. It starts at (2, 2), facing east. The only black squares are (4, 2) and (3, 4); all other squares are open.机器人(robot)位于一个 5 行 5 列的网格上。坐标向东和向北增大。机器人从 (2, 2) 出发,朝东。只有 (4, 2) 和 (3, 4) 是黑色方格,其余方格均可通行。

REPEAT 3 TIMES
{
  IF(CAN_MOVE(forward))
  {
    MOVE_FORWARD()
  }
  ELSE
  {
    ROTATE_LEFT()
    MOVE_FORWARD()
  }
}

Where is the robot, and which direction is it facing, after the code finishes?代码执行结束后,机器人位于何处、朝向哪个方向?

Answer:答案: (B)
  • The first pass moves east to (3, 2).
  • The square ahead is black, so the second pass turns north and moves to (3, 3).
  • The square ahead is again black, so the third pass turns west and moves to (2, 3).
Option A omits the third pass. Option C omits both turns. Option D treats a left turn as a different direction and ignores the black squares.
  • 第一轮向东移动到 (3, 2)。
  • 前方是黑色方格,因此第二轮左转朝北并移动到 (3, 3)。
  • 前方再次出现黑色方格,因此第三轮左转朝西并移动到 (2, 3)。
选项 A 漏掉了第三轮。选项 C 漏掉了两次转向。选项 D 错误理解左转方向并忽略了黑色方格。
Q7HARDAP MC3.5 Boolean Expressions

A service admits users whose integer age is from 18 through 65, inclusive.某服务允许整数年龄在 18 至 65 岁之间的用户使用,包含两个端点。

Which two expressions are true exactly for the admitted ages? Select two answers. Select two answers.以下哪两个表达式恰好在年龄符合要求时为真?请选择两个答案。 选择两个答案。

Answer:答案: (A), (B)
  • The first expression includes both endpoints and requires both bounds.
  • The second excludes ages below 18 or above 65, leaving exactly the same range.
Option C excludes ages 18 and 65. Option D is true even for ages outside the range because it uses OR.
  • 第一个表达式同时要求满足上下界,并包含两个端点。
  • 第二个表达式排除小于 18 或大于 65 的年龄,剩余范围完全相同。
选项 C 排除了 18 岁和 65 岁。选项 D 使用 OR,即使年龄超出范围也会为真。
Q8HARDAP MC3.10 Lists

The following code removes selected values from a list.以下代码从列表(list)中删除指定的值。

values ← [4, 2, 2, 5, 2]
i ← 1
REPEAT UNTIL(i > LENGTH(values))
{
  IF(values[i] = 2)
  {
    REMOVE(values, i)
  }
  ELSE
  {
    i ← i + 1
  }
}

What is the value of values when the loop ends?循环结束时,values 的值是什么?

Answer:答案: (C)
  • The 4 is retained and i advances to 2.
  • Both adjacent 2s are removed at index 2 because each removal shifts the next element into that index.
  • The 5 is retained, and the final 2 is removed. The list is [4, 5].
Option A skips an adjacent 2 after removal. Option B leaves the final 2 unexamined. Option D retains the values that the code removes.
  • 保留 4,i 前进到 2。
  • 两个相邻的 2 都在索引(index)2 处被删除,因为每次删除都会将下一元素移到该位置。
  • 保留 5,删除最后一个 2。列表变为 [4, 5]。
选项 A 在删除后跳过了一个相邻的 2。选项 B 没有检查最后一个 2。选项 D 保留了代码要删除的值。
Q9MEDIUMAP MC3.11 Binary Search

A school wants to find an ID in a list. The list is currently in the order in which IDs were issued, not in numerical order.学校要在一个列表(list)中查找某个编号。该列表目前按编号发放顺序排列,而不是按数值顺序排列。

Which change makes binary search applicable to this list?进行哪项更改后,可以对该列表使用二分查找(binary search)?

Answer:答案: (B)
  • Binary search uses the ordering of values to eliminate half of the remaining candidates after a comparison.
  • Sorting by ID supplies the required order.
Option A does not establish order. Option C still leaves the values unsorted. Option D describes a linear search rather than a change that enables binary search.
  • 二分查找利用数值顺序,在比较后排除剩余候选项的一半。
  • 按编号排序可提供所需的顺序。
选项 A 没有建立顺序。选项 C 的数值仍未排序。选项 D 描述的是线性查找(linear search),而不是使二分查找可用的更改。
Q10HARDAP MC3.12 Calling Procedures

The procedure returns the index of the first list element greater than a limit, or -1 if none exists.该过程(procedure)返回列表中第一个大于指定上限的元素的索引;若不存在这样的元素,则返回 -1。

PROCEDURE FirstAbove(items, limit)
{
  i ← 1
REPEAT UNTIL(i > LENGTH(items))
  {
    IF(items[i] > limit)
    {
      RETURN(i)
    }
    i ← i + 1
  }
  RETURN(-1)
}
values ← [2, 5, 9, 7]
result ← FirstAbove(values, 6)
DISPLAY(result)
DISPLAY(LENGTH(values) - result)

What is displayed?显示的结果是什么?

Answer:答案: (C)
  • The first two values do not exceed 6. The third value, 9, does.
  • RETURN immediately exits with index 3. The second displayed value is 4 - 3, or 1.
Option A returns the element rather than its index. Option B continues searching after RETURN. Option D uses zero-based indexing.
  • 前两个值都不大于 6。第三个值 9 大于 6。
  • RETURN 立即退出过程并返回索引 3。第二个显示值为 4 - 3,即 1。
选项 A 返回了元素值而不是索引。选项 B 在 RETURN 后继续查找。选项 D 使用了从 0 开始的索引。
Q11HARDAP MC3.13 Developing Procedures

A team uses a documented library procedure to calculate shipping costs. The procedure accepts a weight and returns a cost. The team wants to update its application and avoid duplicating the calculation.某团队使用一个有文档说明的库(library)过程(procedure)计算运费。该过程接收重量并返回费用。团队希望更新应用程序,同时避免重复编写计算代码。

Which two statements are valid? Select two answers. Select two answers.以下哪两个说法正确?请选择两个答案。 选择两个答案。

Answer:答案: (A), (B)
  • The library documentation describes how to call the procedure and interpret its result.
  • The weight parameter generalizes the calculation across inputs.
Option C defeats reuse because callers can invoke the same procedure. Option D ignores that callers depend on the procedure's documented behavior.
  • 库文档说明如何调用过程以及如何理解其结果。
  • 重量参数(parameter)使计算适用于不同输入。
选项 C 破坏了代码复用,因为调用者可以调用同一个过程。选项 D 忽略了调用者依赖过程的文档化行为。
Q12HARDAP MC3.15 Random Values

A simulation represents an event by drawing an integer uniformly with RANDOM(1, 8). The event occurs when the returned value is at most 3.一个模拟(simulation)使用 RANDOM(1, 8) 等概率抽取整数来表示事件。当返回值不大于 3 时,事件发生。

What is the probability that the event occurs on one draw?一次抽取中该事件发生的概率是多少?

Answer:答案: (A)
  • RANDOM(1, 8) has eight equally likely results, including both endpoints.
  • The favorable results are 1, 2, and 3, so the probability is 3/8.
Option B excludes the favorable endpoint 3. Option C excludes an endpoint from the sample space. Option D counts the outcomes in which the event does not occur.
  • RANDOM(1, 8) 有八个等可能结果,包含两个端点。
  • 符合条件的结果是 1、2 和 3,因此概率为 3/8。
选项 B 排除了符合条件的端点 3。选项 C 从样本空间中排除了一个端点。选项 D 计算的是事件不发生的结果。
Q13HARDAP MC3.16 Simulations

A city simulates evacuation times using road capacity and the number of vehicles. Its model assumes every vehicle starts at the same time, although actual departure times vary.某城市利用道路容量和车辆数量模拟疏散时间。模型假设所有车辆同时出发,但实际出发时间各不相同。

Which conclusion about the simulation is best supported?关于该模拟(simulation),以下哪个结论最有依据?

Answer:答案: (A)
  • A simulation deliberately simplifies a real phenomenon.
  • The simultaneous-departure assumption may change congestion and should be evaluated against observations.
Option B assumes an exact prediction from an incomplete model. Option C incorrectly treats randomness as a guarantee against bias. Option D overlooks the usefulness of purposeful abstraction.
  • 模拟会有目的地简化真实现象。
  • 同时出发的假设可能改变拥堵情况,应使用观测数据评估其影响。
选项 B 错误地认为不完整的模型能精确预测。选项 C 错误地认为随机性保证消除偏差。选项 D 忽略了有目的的抽象(abstraction)的价值。
Q14HARDAP MC3.17 Algorithmic Efficiency

A delivery company must find a low-cost route among many possible routes. An exact algorithm is known to examine an exponentially growing number of candidates as the number of stops increases.一家配送公司需要在众多可能的路线中寻找低成本路线。已知某精确算法检查的候选路线数量会随着停靠点数量增加而呈指数增长。

Which approach is most appropriate when the exact algorithm takes too long on large instances?当精确算法处理大型实例所需时间过长时,哪种方法最合适?

Answer:答案: (A)
  • Exponential growth can make an exact search impractical for large inputs.
  • A heuristic can trade the guarantee of an optimal answer for a useful result in less time.
Option B incorrectly guarantees optimality. Option C confuses an impractical exact search with the inability to find any route. Option D ignores the growth in the number of candidates.
  • 指数增长可能使大型输入的精确搜索变得不切实际。
  • 启发式方法(heuristic)可以放弃最优性保证,以较少时间获得实用结果。
选项 B 错误地保证结果最优。选项 C 将精确搜索不切实际与无法找到任何路线混为一谈。选项 D 忽略了候选路线数量的增长。
Q15MEDIUMAP MC3.18 Undecidable Problems

A researcher studies a decision problem that is known to be undecidable.一名研究者正在研究一个已知不可判定的判定问题(decision problem)。

Which statement must be true?以下哪个说法必然正确?

Answer:答案: (B)
  • Undecidability means no algorithm can provide a correct yes-or-no answer for every possible input.
  • Some individual instances may still be solvable.
Option A incorrectly rules out solutions for individual instances. Option C contradicts the definition of undecidability. Option D mistakes a fundamental limitation for a hardware-speed limitation.
  • 不可判定意味着不存在能对每个可能输入都给出正确“是”或“否”答案的算法。
  • 某些单独的实例仍可能有算法解。
选项 A 错误地排除了单个实例可解的可能性。选项 C 与不可判定的定义矛盾。选项 D 将根本性限制误认为硬件速度限制。