2024 年 6 月青少年软件编程 Python 等级考试试卷六级真题(含答案)
分数:100 题数:38
一、单选题(共 25 题,共 50 分)。
1. 运行下面代码的正确结果是()。
with open("example.txt", "a") as file:
file.write("I see you.")
其中 example.txt 文件内容如下:
This is an example.
A. This is an example.
B. I see you.
C. This is an example.I see you.
D. I see you.This is an example.
标准答案:C。
2. 在 Python 中,以下哪个函数可以用于创建一个新的文件()。
A. write( )
B. create( )
C. new( )
D. open( )
标准答案:D。
3. 运行下面代码的正确结果是()。
filename = "example.txt"
line_count = 0
with open(filename, "r") as file:
for line in file:
line_count += 1
print(f"The file 'example' has {line_count} lines.")
其中 example.txt 文件内容如下:
My Favorite Animal
Once upon a time, I had a pet dog named Max.
Max was the most obedient dog I knew.
We played fetch in the park, went on long walks in the woods, and
even took naps together on lazy afternoons.
A. 4
B. 3
C. 2
D. 1
标准答案:A。
4. 运行下面代码的正确结果是()。
with open("myfile.txt", "w") as out_file:
out_file.write("This is my first Python program.")
with open("myfile.txt", "r") as in_file:
myfile = in_file.read()
print(myfile)
其中 myfile.txt 文件内容如下:
Hello World!
A. Hello World!
B. This is my first Python program.
C. Hello World!
This is my first Python program.
D. Hello World!This is my first Python program.
标准答案:B。
5. 编写程序绘制如下图所示的直线,程序空白处应填()。
import matplotlib.pyplot as p
import numpy as np
x= np.array([0,1,2,____,4,5])
p.plot(x,'o:r')
p.show()
A. 1
B. 2
C. 3
D. 4
标准答案:C。
6. 已知程序 1 绘制的图形如下图所示,要绘制相同的图形,请补全程序 2 空白
()。
程序 1:
import matplotlib.pyplot as p
import numpy as np
x= np.array([0,1,0,1,0,1,0])
p.plot(x,'o:r')
p.show()
程序 2:
import matplotlib.pyplot as p
import numpy as np
x= np.array([3,4,3,____,3,4,3])
p.plot(x,'o:r')
p.show()
A. 1
B. 2
C. 3
D. 4
标准答案:D。
7. 在命令行窗口分别运行以下代码,输出结果是()。
>>>import numpy as np
>>>np.full(6,'6')
A. array(['6', '6', '6', '6', '6', '6']
B. array([6, 6, 6, 6, 6, 6]
C. 6, 6, 6, 6, 6, 6
D. '6', '6', '6', '6', '6', '6'
标准答案:A。
8. 运行以下关于二维数组读取的程序,输出结果是()。
a=[[1,2,3],[4,5,6],[7,8,9]]
print(a[1][2])
A. 2
B. 4
C. 5
D. 6
标准答案:D。
9. 运行以下代码,绘制出来的第六个柱形图颜色是()。
import matplotlib.pyplot as p
import numpy as np
x=np.array(['a','b','c','d','e','f'])
h=np.array([1,4,5,6,4,3])
c=np.array(['red','blue','green'])
p.bar(x=x,height=h,color=c)
p.show()
A. red
B. blue
C. green
D. black
标准答案:C。
10. 关于 JSON 格式数据转为 Python 数据格式,运行以下程序,输出结果是()。
import json
a='{"name": "张三", "age": 30, "city": "北京"}'
b=json.loads(a)
c=list(b.keys())
d=list(b.values())
print(d[2])
A. age
B. city
C. 北京
D. 30
标准答案:C。
11. 下列哪个选项不能在 SQLite 数据库中运行()。
A. 10
B. '10'
C. [10,11]
D. None
标准答案:C。
12. CREAT TABLE Users (id,name,password,role)
关于上述语句,说法错误的是()。
A. id 作为唯一标识,约束条件为 PRIMARY 和 NOT NULL
B. name 是可以重复的
C. password 的约束条件为 NOT NULL
D. role 为一个不定长的字符串
标准答案:B。
13. 关于以下代码,说法错误的是()。
import sqlite3
conn = sqlite3.connect('./mydb.sqlite')
cur = conn.cursor()
sql = '''INSERT INTO Users (name,password,role) VALUES (?,?,?)'''
cur.execute(sql,('admin','123456','管理员'))
cur.execute(sql,('admin','123456','管理员'))
cur.execute(sql,('user','123456','普通用户'))
conn.commit()
A. conn = sqlite3.connect('./mydb.sqlite'),如果 mydb.sqlite 不存在会
自动创建。
B. cur = conn.cursor(),的作用是获取一个数据的游标。
C. sql = '''INSERT INTO Users (name,password,role) VALUES
(?,?,?)'''中?的作用是占位符。
D. 运行结果会添加两个 admin 的管理员账号。
标准答案:D。
14. 执行下面程序后,选项中值为 1 的是()。
n=3
m=2
dp=[[0 for i in range(n)]for j in range(m)]
dp.append([0,0,n-m])
dp.insert(-1,[n for i in range(n)])
print(dp)
A. dp[m][n]
B. dp[n][m]
C. dp[len(dp)-1][0]
D. dp[m][0]
标准答案:B。
15. 执行下面程序后,列表 a 的值可能是()。
import random
a=[0]*6
for i in range(1,6):
tmp=random.randint(5,24)
if tmp%2==0 or i%2==1:
a[i]=a[i-1]+tmp
print(a)
A. [0,9,29,50,0,20]
B. [1,11,44,62,86,109]