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 and show your trace in the work space. For short-answer items, write the exact output. Code is Python; trace each step to verify before answering.本部分包含选择题与短答题。选择题请圈出字母,并在答题空白处写出追踪过程。短答题写出精确输出。代码为 Python;作答前逐步追踪。
Given s = "Python", which expression evaluates to "t"?给定 s = "Python",哪个表达式的值为 "t"?
s[1]s[2]s[-1]s[3]What does "computer"[3:6] evaluate to?"computer"[3:6] 的值是什么?
"com""put""pute""mpu"Consider the following code.考察以下代码。
word = "hello"
word.upper()
print(word)
print(word).写出 print(word) 的输出。 [1]word does not change after calling word.upper().用一句话解释为什么调用 word.upper() 后 word 不发生改变。 [2]word stores the uppercased version after the call.重写第 2 行,使 word 在调用后保存大写版本。 [1]A student writes three programs to produce a grade report line.一名学生编写三个程序,用不同方式产生成绩报告行。
# Program A
name = "Alex"
score = 88
result_a = "Student: " + name + ", Score: " + score
print(result_a)
# Program B
result_b = "Student: " + name + ", Score: " + str(score)
print(result_b)
# Program C
result_c = f"Student: {name}, Score: {score}"
print(result_c)
f prefix and the {} braces do in an f-string.写出程序 C 的精确输出。解释 f-string 中 f 前缀和 {} 花括号的作用。 [3]Use the following variable for all parts.以下所有小题均使用此变量。
sentence = "the quick brown fox"
"fox" in sentence. State the type of value returned.写出 "fox" in sentence 的值。说明返回值的类型。 [2]sentence.find("quick") and of sentence.find("cat"). Explain the meaning of each result.写出 sentence.find("quick") 和 sentence.find("cat") 的值。解释每个结果的含义。 [3]sentence.count("o"). Explain which characters are counted.写出 sentence.count("o") 的值。解释哪些字符被计数。 [2]in rather than find() for a search? Give one reason.什么情况下搜索应使用 in 而不是 find()?给出一个理由。 [1]Show every step of your trace. Write Python clearly, with correct indentation. For explain/justify questions, two sentences of reasoning earn full marks. No calculator needed; trace by hand.写出每一步追踪过程。Python 须清晰书写,缩进正确。论证题两句推理即可满分。无需计算器;手工追踪。
Study the two programs below. Both count vowels in the word "education".研究以下两个程序,均用于计算单词 "education" 中的元音字母数量。
Program A (for-each style):程序 A(for-each 风格):
word = "education"
vowels = "aeiou"
count = 0
for ch in word:
if ch in vowels:
count += 1
print(count)
Program B (for-range style):程序 B(for-range 风格):
word = "education"
vowels = "aeiou"
count = 0
for i in range(len(word)):
if word[i] in vowels:
count += 1
print(count)
"education" and whether it is a vowel. State the final output.追踪程序 A。列出 "education" 中的每个字符,并注明是否为元音。写出最终输出。 [3]len(word) returns and what values i takes during Program B.写出 len(word) 的返回值,以及程序 B 执行过程中 i 的所有取值。 [2]A student normalises and analyses a messy CSV field.一名学生规范化并分析一个杂乱的 CSV 字段。
row = " Alice , Biology , 92 "
fields = row.split(",")
clean = [f.strip().lower() for f in fields]
print(clean)
print(clean[0].count("l"))
print("bio" in clean[1])
fields immediately after line 2.写出第 2 行之后 fields 的值。 [2]clean after line 3. Explain what strip() and lower() each do to the first element.写出第 3 行之后 clean 的值。说明 strip() 和 lower() 分别对第一个元素做了什么。 [3]A student explores two ways to reverse a string.一名学生在探索反转字符串的两种方法。
Method 1 (slice step):方法 1(切片步长):
s = "racecar"
rev1 = s[::-1]
print(rev1)
Method 2 (character loop):方法 2(字符循环):
s = "racecar"
rev2 = ""
for ch in s:
rev2 = ch + rev2
print(rev2)
[::-1] means (start, stop, step).写出方法 1 的输出。解释切片 [::-1] 的含义(start、stop、step)。 [3]rev2 after each iteration. State the final output.逐步追踪方法 2,写出每次迭代后 rev2 的值。写出最终输出。 [4]True if s is a palindrome.写出一个 Python 单行表达式(不用循环),当 s 是回文时值为 True。 [1]A program processes a sentence using the full text-processing pipeline.一个程序使用完整的文本处理流水线处理一个句子。
sentence = "The cat sat on the mat and the cat"
text = sentence.lower().strip()
words = text.split()
total = len(words)
freq = {}
for w in words:
if w in freq:
freq[w] += 1
else:
freq[w] = 1
top = max(freq, key=freq.get)
print(f"Total words: {total}")
print(f"Most frequent: '{top}' ({freq[top]} times)")
print(f"'cat' appears: {'yes' if 'cat' in words else 'no'}")
text after line 2 and the value of words after line 3.写出第 2 行后 text 的值和第 3 行后 words 的值。 [2]total and write the complete contents of freq after the loop.写出 total 的值,以及循环结束后 freq 的完整内容。 [3]print statements.写出三个 print 语句产生的三行输出。 [2]Read each scenario carefully before writing code or traces. Where Python is requested, show correct indentation. Conclude each question with a one-sentence answer. No EASY questions in Part III.动笔前仔细阅读每个场景。Python 须正确缩进。每题以一句完整的结论句作答。第三部分无易级题目。
A program counts uppercase letters in a string and reports whether the digit "5" is present.一个程序统计字符串中大写字母的数量,并报告数字 "5" 是否存在。
text = "Hello5World"
upper_count = 0
for ch in text:
if ch.isupper():
upper_count += 1
print(upper_count)
print("5" in text)
"Hello5World". Record whether ch.isupper() is True and the running value of upper_count.对 "Hello5World" 的每个字符追踪循环,记录 ch.isupper() 是否为 True 及 upper_count 的累计值。 [4]sum() and a generator expression so the same count is produced without an explicit loop. Write the single line.用 sum() 和生成器表达式将循环体改写为一行,无需显式循环。写出这一行。 [2]A program parses a date in "YYYY-MM-DD" format and produces a human-readable report.一个程序解析 "YYYY-MM-DD" 格式的日期并生成人性化报告。
date = "2024-06-15"
year = date[0:4]
month = date[5:7]
day = date[8:10]
last_char = date[-1]
report = f"Year: {year}, Month: {month}, Day: {day}, Last char: {last_char}"
print(report)
year, month, and day after lines 2, 3, and 4.写出第 2、3、4 行后 year、month、day 的值。 [3]last_char. Explain how negative indexing works to retrieve it.写出 last_char 的值。解释负索引如何取得它。 [2]print(report).写出 print(report) 的精确输出。 [2]date[0] = "3" to change the first digit. This raises a TypeError. Explain why, and state how they should instead produce a corrected date string.一名学生写 date[0] = "3" 来修改第一位,但这会引发 TypeError。解释原因,并说明如何产生修改后的日期字符串。 [2]A student writes a program to check (case-insensitively) whether a target word appears in a sentence and print how many times it occurs. The program contains a bug.一名学生编写了一个程序,不区分大小写地检查目标词是否出现在句子中并打印次数。该程序存在错误。
sentence = "The Quick Brown Fox"
target = "the"
if target in sentence:
print("Found!")
print(sentence.count(target))
else:
print("Not found.")
"the" in sentence.写出程序当前的输出。找出错误:解释为什么第 3 行的检查在 sentence 中找不到 "the"。 [3]sentence and target.修复错误后,对给定的 sentence 和 target,写出正确的输出。 [2]3A-AP-14 · 3.4 · 4.BICS3C A1.2 · ICS3U A1.1Full Syllabus Map in Study Guide: ../Study Guides/Unit_6_Strings_and_Text_Processing.html. CS has no AB standalone diploma exam; AB framing uses CSE1110/CSE1120 outcomes.完整大纲对照见学习指南:../Study Guides/Unit_6_Strings_and_Text_Processing.html。CS 无独立 AB 毕业考;AB 题使用 CSE1110/CSE1120 结果框架。