Files
qz2026--WangKunying-/written.md
T
2026-10-07 23:18:24 +08:00

24 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
一、选择题
1. B 2. B 3. B 4. B 5. B 6. B 7. B 8. B 9. B 10. B
每题思路
1. `[1, 2]` —— 可变默认参数:`def f(x, lst=[])` 里的 `[]` 只在函数定义时创建一次,之后所有调用共用同一个列表。`f(1)` 后是 `[1]`,`f(2)` 再 append 2 变成 `[1, 2]`。
2. `[1, 2, 3, 4] [1, 2, 3]` —— `b = a` 是别名(指向同一列表),`c = a.copy()` 是复制出的新列表。`a.append(4)` 改的是 a/b 共用的列表,所以 b 变、c 不变。
3. 输出 A 和 C —— 出错走 `except` 打印 A;`else` 只在没出错时执行(这里出错,跳过);`finally` 一定执行打印 C。
4. `7 5` —— `" hello "` 是 1 空格 + 5 字母 + 1 空格 = 7 个字符;`strip()` 去掉首尾空格后 `"hello"` 长 5。
5. 只输出 end —— for 循环被 `break` 打断时,`else` 不执行,所以 `done` 不打印,只执行循环外的 `end`。
6. `Rex: woof` —— Dog 继承了 Animal 的 `__init__`,又重写了 `speak()`,所以 `d.speak()` 调用 Dog 版本,打印 `Rex: woof`。
7. `{'b': 2}` —— 字典推导式 `{k: v for k, v in d.items() if v > 1}` 只保留 `v > 1` 的项,只有 `"b": 2` 满足。
8. `<class 'str'>` —— `json.dumps()` 把字典序列化成 JSON 字符串,所以结果是 str。
9. 只输出 6 —— `f(5)` 单独一行返回了 6 但没打印,无输出;`print(f(5))` 才打印 6,所以只有一行 6。
10. get() 返回 None,[] 抛 KeyError —— `user.get("city")` 键不存在返回 None(不报错),`user["city"]` 键不存在抛 KeyError。