读代码·推结果·选答案——基础知识的进阶挑战
本套题目在基础知识点上增加了一些常见陷阱和综合逻辑,包括循环控制、列表/字典操作方法、逻辑短路、异常处理、引用传递等。
第81题
count = 0for i in range(1, 6): if i == 3: break count += 1print(count)
第82题
lst = [1, 2, 3, 4, 5]lst.remove(3)print(len(lst))
A. 2
B. 3
C. 4
D. 5
第83题
s = "apple,banana,orange"print(s.split(",")[1])
A. "apple"
B. "banana"
C. "orange"
D. ["apple", "banana", "orange"]
第84题
d = {"a": 1, "b": 2}print(d.get("c", 3))
A. 1
B. 2
C. 3
D. None
第85题
def mul(x, y=2): return x * yprint(mul(3))print(mul(3, 4))
A. 6 和 12
B. 6 和 6
C. 12 和 6
D. 12 和 12
第86题
a = 10b = 5result = a > 5 and b < 3print(result)
A. True
B. False
C. 5
D. 报错
第87题
for i in range(5): if i % 2 == 0: continue print(i, end=" ")
A. 0 2 4
B. 1 3
C. 0 1 2 3 4
D. 1 3 5
第88题
lst = [1, 2, 3]lst.pop(0)print(lst)
A. [2, 3]
B. [1, 2]
C. [1, 2, 3]
D. 报错
第89题
words = ["Hello", "World"]print(" ".join(words))
A. "HelloWorld"
B. "Hello World"
C. ["Hello", "World"]
D. "Hello,World"
第90题
d = {"a": 1, "b": 2}d.update({"c": 3})print(len(d))
A. 2
B. 3
C. 4
D. 报错
第91题
def add_one(lst): lst.append(1)a = [0]add_one(a)print(a)
A. [0]
B. [0, 1]
C. [1]
D. 报错
第92题
try: x = 1 / 0except ZeroDivisionError: print("错误")else: print("正确")
A. "错误"
B. "正确"
C. "错误" 和 "正确"
D. 报错
第93题
a = [1, 2, 3]b = a.copy()b[0] = 9print(a[0])
A. 1
B. 9
C. 2
D. 报错
第94题
for i in range(2): for j in range(2): if i == j: continue print(i, j)
A. 0 1 和 1 0
B. 0 0 和 1 1
C. 0 1
D. 1 0
第95题
def get_min_max(nums): return min(nums), max(nums)a, b = get_min_max([3, 1, 4])print(a, b)
A. 1 3
B. 1 4
C. 3 4
D. 3 1
第96题
squares = [x*x for x in range(5) if x%2==1]print(len(squares))
A. 2
B. 3
C. 4
D. 5
第97题
s = "abcdef"print(s.find("cde"))
A. 2
B. 3
C. 4
D. -1
第98题
s = {1, 2, 3}s.add(2)print(len(s))
A. 2
B. 3
C. 4
D. 报错
第99题
func = lambdax: x*2print(func(3) + func(4))
A. 7
B. 14
C. 12
D. 21
第100题
score = 75if score >= 90: grade = "A"elif score >= 80: grade = "B"elif score >= 70: grade = "C"else: grade = "D"print(grade)
A. A
B. B
C. C
D. D
答案汇总
81.A 82.C 83.B 84.C 85.A 86.B 87.B 88.A 89.B 90.B 91.B 92.A 93.A 94.A 95.B 96.A 97.A 98.B 99.B 100.C
解析
81题:range(1,6)生成1~5,当i==3时执行break退出循环,此时count只加了两次(i=1,2),输出2,选A。
82题:lst.remove(3)删除第一个值为3的元素,剩余[1,2,4,5],长度为4,选C。
83题:split(",")分割得到列表["apple","banana","orange"],索引1为"banana",选B。
84题:字典get("c", 3),键"c"不存在,返回默认值3,选C。
85题:第一次缺省参数y=2,3*2=6;第二次传入4,3*4=12,选A。
86题:a>5为True,b<3为False,逻辑与and短路(第一个为True,继续判断第二个,整体False),输出False,选B。
87题:continue跳过偶数(0,2,4),输出奇数1和3,选B。
88题:pop(0)删除索引0的元素,原列表变为[2,3],选A。
89题:" ".join(words)用空格连接列表元素,得到"Hello World",选B。
90题:字典update添加键值对,长度由2变为3,选B。
91题:列表作为可变对象传入函数,append会修改原列表,a变为[0,1],选B。
92题:发生ZeroDivisionError,执行except块,输出"错误",不执行else,选A。
93题:a.copy()创建浅拷贝,修改拷贝不影响原列表,a[0]仍为1,选A。
94题:外层i=0,内层j=0(跳过),j=1输出0 1;外层i=1,内层j=0输出1 0,j=1跳过,选A(注意输出为两行)。
95题:min([3,1,4])=1,max=4,输出1 4,选B。
96题:列表推导式取range(5)中奇数1,3,平方后得到[1,9],长度为2,选A。
97题:find返回子串起始索引,"cde"从索引2开始,选A。
98题:集合添加重复元素不改变集合,长度仍为3,选B。
99题:lambda(匿名)函数x*2,3*2=6,4*2=8,和为14,选B。
100题:score=75,满足score>=70,输出C,选C。