Practice Questions · AP CSP-Feeder · US / ON / BC / AB Styles练习题集 · AP CSP 衔接 · 美 / 安 / 卑 / 阿省风格
Questions mix multiple-choice and short-answer items. For MCQs, circle the letter. For short-answer items, write functions in pseudocode or Python with correct indentation. Trace code by hand: write each variable's value at each step before answering.本部分包含选择题与短答题。选择题请圈出字母。短答题用伪代码或 Python 书写函数,缩进须正确。手工追踪代码:作答前写出每一步每个变量的值。
A student copies the same 15-line calculation into four different places in their program. Later, they discover a bug in that calculation. What is the main disadvantage of this approach compared to using a function?一名学生将同一段 15 行计算复制到程序的四个不同地方。后来他们在该计算中发现了一个错误。与使用函数相比,这种做法的主要缺点是什么?
What does the following program output?以下程序输出什么?
def say_hello(name):
print("Hello, " + name + "!")
say_hello("Sam")
say_hello("Alex")
Hello, name! printed twice打印两次Hello, Sam! then然后 Hello, Alex!Hello, Alex! then然后 Hello, Sam!Consider the following function and calls.考察以下函数及调用。
FUNCTION power(base, exponent):
SET result TO 1
FOR i FROM 1 TO exponent:
SET result TO result * base
RETURN result
END FUNCTION
OUTPUT power(3, 2)
OUTPUT power(2, 4)
power.指出函数 power 的形参。 [1]power(3, 2), state the argument bound to each parameter and trace the loop to find the return value.对于调用 power(3, 2),写出绑定到每个形参的实参,并追踪循环以求出返回值。 [3]power(2, 4) without full trace; justify in one sentence.无需完整追踪,写出 power(2, 4) 的输出;用一句话说明理由。 [1]A programmer writes a function to classify a score and uses it in a program.程序员编写了一个分类成绩的函数,并在程序中使用它。
FUNCTION grade(score):
IF score >= 90 THEN RETURN "A"
IF score >= 80 THEN RETURN "B"
IF score >= 70 THEN RETURN "C"
IF score >= 60 THEN RETURN "D"
RETURN "F"
END FUNCTION
SET result TO grade(82)
OUTPUT result
OUTPUT grade(55)
grade(82) step by step. Identify which RETURN executes and state the value returned.逐步追踪调用 grade(82)。指出哪条 RETURN 语句执行,写出返回的值。 [3]grade is a value-returning function. Describe one difference between a value-returning function and a procedure (void function).grade 是有返回值的函数。描述有返回值的函数与过程(无返回值函数)之间的一个区别。 [1]Study the following program carefully.仔细研究以下程序。
SET score TO 50 # global variable
FUNCTION update():
SET score TO score + 10
RETURN score
END FUNCTION
OUTPUT score # Line A
OUTPUT update() # Line B (Python: uses global keyword inside)
OUTPUT score # Line C
score (using the global keyword in Python), state the output at Line B and Line C.假设函数使用 global 关键字修改了全局变量 score,写出第 B 行和第 C 行的输出。 [2]score as a parameter and return the new value instead of modifying a global. Give one specific risk of relying on global state.解释为什么将 score 作为形参传入并返回新值比修改全局变量更好。给出依赖全局状态的一个具体风险。 [2]count, can they interfere with each other? Answer in one sentence.如果两个函数各自都有一个名为 count 的局部变量,它们会互相干扰吗?用一句话回答。 [1]Write pseudocode or Python clearly, with correct indentation. For each trace, show every variable value at each step. For written questions, two complete sentences earn full marks. Trace function calls by writing the parameter bindings before entering the function body.伪代码或 Python 须清晰书写,缩进正确。每次追踪须写出每一步每个变量的值。书面解答题用两句完整句子即可满分。追踪函数调用时,先写出形参绑定,再进入函数体。
A programmer decomposes a grade-report program into four functions as shown below. The main program uses only four lines.程序员将成绩报告程序分解为如下四个函数。主程序只有四行。
FUNCTION get_scores():
-- reads 5 scores and returns them as a list
...
END FUNCTION
FUNCTION compute_average(scores):
SET total TO 0
FOR EACH s IN scores: SET total TO total + s
RETURN total / 5
END FUNCTION
FUNCTION letter_grade(average):
IF average >= 90 THEN RETURN "A"
IF average >= 80 THEN RETURN "B"
IF average >= 70 THEN RETURN "C"
IF average >= 60 THEN RETURN "D"
RETURN "F"
END FUNCTION
FUNCTION display_report(avg, grade):
OUTPUT "Average: " + avg
OUTPUT "Grade: " + grade
END FUNCTION
-- Main program
SET scores TO get_scores()
SET avg TO compute_average(scores)
SET grade TO letter_grade(avg)
display_report(avg, grade)
[85, 90, 78, 92, 80], trace the call compute_average(scores) and state the return value.如果成绩为 [85, 90, 78, 92, 80],追踪调用 compute_average(scores) 并写出返回值。 [3]Study the following two functions and the code that calls them.研究以下两个函数及调用它们的代码。
def double(n):
return n * 2
def add_and_double(a, b):
total = a + b
return double(total)
x = add_and_double(3, 7)
print(x)
print(add_and_double(5, 5))
add_and_double(3, 7), state the argument bound to each parameter and trace the function to determine the return value.对于调用 add_and_double(3, 7),写出绑定到每个形参的实参,并追踪函数以确定返回值。 [3]add_and_double calls double internally. Explain in one sentence what this demonstrates about function reuse.add_and_double 在内部调用 double。用一句话解释这说明了函数复用的什么特点。 [1]double to multiply by 3 instead of 2, state the new output of both print statements.如果学生将 double 修改为乘以 3 而非 2,写出两条 print 语句的新输出。 [2]Study the following program carefully. Note the local variable named result inside each function.仔细研究以下程序。注意每个函数内部名为 result 的局部变量。
def square(n):
result = n * n
return result
def cube(n):
result = n * n * n
return result
a = square(4)
b = cube(3)
print(a)
print(b)
print(a + b)
square(4): state the value bound to n, compute result, and state the return value.追踪调用 square(4):写出绑定到 n 的值,计算 result,写出返回值。 [2]square and cube have a local variable called result. Explain in one sentence why these two variables do not interfere with each other.square 和 cube 都有名为 result 的局部变量。用一句话解释这两个变量为什么互不干扰。 [1]square so that it uses a global variable result instead of a local one (add the global declaration). Then explain one reason why this version is worse than the original.重写 square,使其使用全局变量 result 而非局部变量(添加 global 声明)。然后解释该版本比原版差的一个原因。 [2]The two programs below compute the same result but use different designs. Program A uses a shared global variable; Program B passes data only through parameters and return values.以下两个程序计算相同的结果,但使用了不同的设计。程序 A 使用共享的全局变量;程序 B 仅通过形参和返回值传递数据。
Program A (global state):程序 A(全局状态):
total = 0
def add_item(price):
global total
total = total + price
add_item(15)
add_item(30)
print(total)
Program B (parameter-based):程序 B(基于参数):
def add_item(running_total, price):
return running_total + price
t = 0
t = add_item(t, 15)
t = add_item(t, 30)
print(t)
add_item(10) a second time in a different part of the program? Explain why the result depends on hidden state.在程序 A 中,如果在程序的另一处再次调用 add_item(10),会发生什么?解释为什么结果依赖于隐藏状态。 [2]Read each scenario carefully before writing code. For pseudocode, use standard block format (FUNCTION/END FUNCTION, IF/THEN/END IF). For Python, show correct indentation. Each question asks you to design, trace, or extend functions; always state the return value or output explicitly.动笔前仔细阅读每个场景。伪代码使用标准块格式(FUNCTION/END FUNCTION、IF/THEN/END IF)。Python 须正确缩进。每题要求设计、追踪或扩展函数;务必明确写出返回值或输出。
A student needs a program that converts a temperature in Celsius to Fahrenheit using the formula F = C x 9/5 + 32. The same conversion is needed in three different places.一名学生需要一个程序,使用公式 F = C x 9/5 + 32 将摄氏温度转换为华氏温度。同一转换在三个不同地方都需要用到。
to_fahrenheit(celsius) in pseudocode. It must accept one parameter and return the converted value.用伪代码编写函数 to_fahrenheit(celsius)。它必须接受一个形参并返回转换后的值。 [3]to_fahrenheit(0) and to_fahrenheit(100). State the two return values.手工追踪 to_fahrenheit(0) 和 to_fahrenheit(100) 以验证你的函数。写出两个返回值。 [2]A student must write a program that computes statistics (minimum, maximum, average) for a list of quiz scores, then displays a summary. They choose a modular design with one function per task.一名学生需要编写一个程序,计算一组测验成绩的统计数据(最小值、最大值、平均值),然后显示摘要。他们选择每个任务一个函数的模块化设计。
scores = [72, 88, 65, 91, 78]
def find_min(data):
return min(data) # uses built-in min()
def find_max(data):
return max(data) # uses built-in max()
def find_average(data):
return sum(data) / len(data) # uses built-in sum() and len()
def display_summary(data):
print("Min:", find_min(data))
print("Max:", find_max(data))
print("Avg:", find_average(data))
display_summary(scores)
display_summary(scores). Show your calculations for the average.写出 display_summary(scores) 打印的三个值。展示平均值的计算过程。 [4]min(), max(), sum(), and len() are used in this program. Explain why using them instead of writing custom loops illustrates the principle described in CSTA 3B-AP-16.该程序使用了内置函数 min()、max()、sum() 和 len()。解释为什么使用它们(而非编写自定义循环)体现了 CSTA 3B-AP-16 所描述的原则。 [2]find_range(data) that returns the difference between the maximum and minimum. Write this function in Python. It should call find_max and find_min instead of calling max() and min() directly, to demonstrate function reuse.学生想添加一个函数 find_range(data),返回最大值与最小值之差。用 Python 编写此函数。它应调用 find_max 和 find_min(而非直接调用 max() 和 min()),以展示函数复用。 [2]find_range(scores) for the given list.对于给定列表,写出 find_range(scores) 的返回值。 [1]A student is building a tip calculator. They use top-down design: the main program reads three inputs (bill amount, tip percentage, number of people) and calls three helper functions to do the work.一名学生正在构建小费计算器。他们采用自顶向下设计:主程序读取三个输入(账单金额、小费百分比、人数),并调用三个辅助函数完成工作。
FUNCTION compute_tip(bill, percent):
RETURN bill * percent / 100
END FUNCTION
FUNCTION compute_total(bill, tip):
RETURN bill + tip
END FUNCTION
FUNCTION per_person(total, people):
RETURN total / people
END FUNCTION
-- Main program
SET bill TO 120
SET percent TO 15
SET people TO 4
SET tip TO compute_tip(bill, percent)
SET total TO compute_total(bill, tip)
SET share TO per_person(total, people)
OUTPUT "Each person pays: " + share
apply_discount(total, discount_pct) that reduces the total by a given percentage before splitting. Write the function in pseudocode and state the new share if discount_pct = 10 is applied to the total computed in part (a).学生想添加一个函数 apply_discount(total, discount_pct),在拆分之前按给定百分比减少总额。用伪代码编写该函数,并写出对 (a) 中计算的总额应用 discount_pct = 10 后的新人均分摊额。 [3]3A-AP-17 · 3A-AP-18 · AAP-3.BICS3U A3.1 · A3.2 · B2.3Full Syllabus Map in Study Guide: ../Study Guides/Unit_4_Functions_and_Modular_Design.html. CS has no AB standalone diploma exam; AB framing uses CSE2110 outcomes.完整大纲对照见学习指南:../Study Guides/Unit_4_Functions_and_Modular_Design.html。CS 无独立 AB 毕业考;AB 题使用 CSE2110 结果框架。