Python笔试题
一、选择题(共25题,每题3分)
1.以下哪一项不是Python标准库?
A. csv
B. math
C. net
D. turtle
答案:C
解析:
csv处理csv文件;
turtle画图库。
2.
sum(range(1, 101))
得到的数值是:
A. 5049
B. 5050
C. 5150
D. 5151
答案:B
解析:range(1,101)相当于for(i=1;i<101;i++),取[1,101)左闭右开。
>>> sum(range(1,101))
5050
3.使用标准库json序列化对象时(json.dump),不能序列化的类型是:
A. bool
B. dict
C. set
D. tuple
答案:C
解析:
# 3
import json
f = False
d = dict(name='Bob', age=20, score=88)
set = {1, 2}
tuple = (1, 2)
print(json.dumps(f))
# false
print(json.dumps(d))
# '{"age": 20, "score": 88, "name": "Bob"}
# set不能序列化
# print(json.dumps(set))
print(json.dumps(tuple))
# [1, 2]
4.以下哪一项是可变类型?
A. int
B. list
C. str
D. tuple
答案:B
5.
7^10
的结果是:
A. 13
B. 15
C. 2
D. 以上都不对
答案:A
解析:^异或,7:0111,10:1010,13:1101
>>> 7^10
13
6.以下哪一项是非法的变量名?
A. my_dict_1
B. 1st_list
C. foo
D. _
答案:B
解析:可以包含字母、数字、下划线_,不能以数字开头。
7.以下哪一项不是关键字?
A. assert
B. eval
C. global
D. pass
答案:B
解析:
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', '__peg_parser__', 'and',
'as', 'assert', 'async', 'await', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except',
'finally', 'for', 'from', 'global', 'if', 'import',
'in','is', 'lambda', 'nonlocal', 'not', 'or', 'pass',
'raise', 'return', 'try', 'while', 'with', 'yield']
8.有以下运算符:①括号,②指数(**),③乘号,④除号,⑤加号,优先级顺序正确的是:
A. ①②③④⑤
B. ②①③④⑤
C. ②①④③⑤
D. ①②⑤③④
答案:A
9.Python内置函数id的返回值类型是:
A. float
B. bool
C. dict
D. int
答案:D
解析:
# 9
a = 10
print(type(id(a)))
print(sum(range(1, 101)))
# <class 'int'>
10.
"os-easy2020"[3:5]
得到的结果是:
A. easy2
B. -easy
C. ea
D. -e
答案:C
解析:取索引为3和4,字符串切片取索引为[n,m-1]。
11.Python源文件只有以下一行内容,运行时会报什么异常?
list = apple
A. SyntaxError
B. NameError
C. ValueError
D. TypeError
答案:B
12.以下哪一项会导致SyntaxError?
A.'"Once upon a time…", she said.'
B."He said, 'Yes!'"
C.'3\'
D."'That's okay'"
答案:C
13.
print(0.1 + 0.2 == 0.3)
的结果是:
A. True
B. False
C. 报错
D. 视硬件或操作系统而定
答案:B
# 13
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
# 0.30000000000000004
# False
14.以下哪一项是False?
A.1 == True
B.2 == True
C.range(0) == []
D.x = 2 * 2; y = 10 – 6; id(x) == id(y)
答案:C
# 14
print(range(0))
# range(0, 0)
print(range(5))
# range(0, 5)
15.以下哪一项是错误的?
A.float('inf')
B.float('nan')
C.float('56'+'78')
D.float('12+34')
答案:D
# 15
print(float('inf'))
print(float('nan'))
print(float('11' + '22'))
# print(float('11+22'))
16.以下表达式的运算结果是:
2 ** 2 ** 3 ** 1
A. 12
B. 64
C. 128
D. 256
答案:D
>>> 2**2**3**1
256
17.以下代码的运行结果是:
hex(255), int('FF', 16), 0xFF
A. [0xFF, 255, 16, 255]
B. (‘0xff’, 155, 16, 255)
C. 报错
D. (‘0xff’, 255, 255)
答案:D
# 17
print(hex(255), int('FF', 16), 0xFF)
# 0xff 255 255
18.以下代码的运行结果是:
d = dict(p='wuhan', q='2020')'{p}{q}'.format(**d)
A. Error
B.wuhan2020
C. wuhan 2020
D. {‘wuhan’, ‘2020’}
答案:B
# 18
d = dict(p='wuhan', q='2020')
print('{p}{q}'.format(**d))
# wuhan2020
19.以下代码的运行结果是:
A. 0
B. 没有输出
C. 报错
D. 以上皆不是
for i in range(0):
print(i)
答案:B
20.给出代码
[ord(ch) for ch in 'abc']
,其运算结果是:
A. [97, 98, 99]
B. [‘97’, ‘98’, ‘99’]
C. [‘a’, ‘b’, ‘c’]
D. 报错
答案:A
21.What is the output of
'search'.find('S')
?
A. s
B. -1
C. ‘’
D. None of the above
答案:B
22.What is a variable defined outside a function?
A. a static variable
B. a global variable
C. a local variable
D. an automatic variable
答案:B
23.What will be the output of the following code?
A. ValueError
B. Invalid
C. 6
D. ValueError(‘Invalid’)
def get_month(m):
if m < 1 or m > 12:
raise ValueError('Invalid')
print(m)
get_month(6)
答案:C
24.Which of the following is not a valid attribute of a file object fp?
A. fp.name
B. fp.closed
C. fp.mode
D. fp.size
答案:D
25.What will be the output of the following code?
A. []
B. [‘3’]
C. Error
D. [‘trees’]
import re
a = re.compile('0-9')
a.findall('3 treees')
答案:A
二、填空题(共5题,每题5分)
26.给出下列代码片段,请补充完整最后一行列表推导(list comprehension)代码,使得运算结果是[‘e’, ‘o’]:
w = 'hello'
v = ('a', 'e', 'i', 'o', 'u')
z = []
print(z)
答案:z = [x for x in w if x in v]
27.把
'{1:<5}'.format('a', 'b')
的运行结果填入下面划线处,要求:
1)直接填写结果字符串的内容,无需引号,
2)每格填写一个字符,
3)如果需要填写空格字符,就在格子里写0x20,
4)如果有多余的格子,请在格子里写N/A。
– , – , – , – ,0x20, – ,N/A
答案:b,0x20,0x20,0x20,0x20,N/A,N/A
28.在划线处补充完整以下代码的运行结果: 结果为
def multi():
return [lambda x: i * x for i in range(4)]
print([m(3) for m in multi()])
答案:[9, 9, 9, 9]
29.在划线处补充完整以下代码的运行结果: 运行结果为:
a = [ ________ ]
b = [ _________ ]
c = [ ________ ]
d = [ ________ ]
import copy
a = [1, 2, 3, 4, ['a', 'b']]
b = a
c = copy.copy(a)
d = copy.deepcopy(a)
a.append(5)
a[4].append('c')
print('a =', a)
print('b =', b)
print('c =', c)
print('d =', d)
答案:
a = [1, 2, 3, 4, [‘a’, ‘b’, ‘c’], 5]
b = [1, 2, 3, 4, [‘a’, ‘b’, ‘c’], 5]
c = [1, 2, 3, 4, [‘a’, ‘b’, ‘c’]]
d = [1, 2, 3, 4, [‘a’, ‘b’]]
30.What will be the output of the following code?
The output will __
def my_decorator(func):
def inner(*args, **kwargs):
inner.i += 1
return func(*args, **kwargs)
inner.i = 0
return inner
@my_decorator
def test_func():
pass
test_func()
test_func()
test_func()
print(test_func.i)
答案:3