内容正文:
青少年软件编程(Python)等级考试试卷(六级)
分数:100 题数:38
一、单选题(共25题,共50分)
1.
运行以下程序,输出的结果是?( )
class A():
def __init__(self,x):
self.x=x+1
def b(self):
return self.x*self.x
t=A(3)
print(t.b())
A.
9
B.
12
C.
7
D.
16
试题编号:202306-zzh-26
试题类型:单选题
标准答案:D
2.
运行以下程序,输出的结果是?( )
import sqlite3
conn = sqlite3.connect('t1.db')
cursor = conn.cursor()
conn.execute("DELETE from user")
cursor.execute('insert into user (id, name) values (\'1\', \'张三\')')
cursor.execute('insert into user (id, name) values (\'2\', \'李四\')')
cursor.execute('insert into user (id, name) values (\'3\', \'王二\')')
cursor.execute('insert into user (id, name) values (\'4\', \'刘五\')')
conn.commit()
cursor.execute('select id,name from user')
values = cursor.fetchone()
values = cursor.fetchone()
print(values)
cursor.close()
conn.close()
A.
('4', '刘五')
B.
('1', '张三')
C.
('2', '李四')
D.
('3', '王二')
试题编号:202306-zzh-30
试题类型:单选题
标准答案:C
3.
以下SQLite语句可以修改记录的是?( )
A.
cursor.execute('insert into user (id, name) values (\'1\', \'张三\')')
B.
cursor.execute('update user set name = "吴吴" WHERE ID = 4')
C.
cursor.execute('select id,name from user')
D.
conn.execute("DELETE from user")
试题编号:202306-zzh-31
试题类型:单选题
标准答案:B
4.
SQLite函数中,以下语句的作用是?( )
values = cursor.fetchmany(2)
print(values)
A.
输出前两条记录
B.
输出第2条记录
C.
输出后两条记录
D.
输出中间两条记录
试题编号:202306-zzh-32
试题类型:单选题
标准答案:A
5.
关于SQLite,说法错误的是?( )
A.
commit()功能是提交当前的所有事务。如果没有提交,程序自上次提交后的所有操作是不可见的
B.
execute()功能是执行SQL语句
C.
fetchall()功能是获取查询结果中所有的记录,返回类型为列表
D.
close()功能是关闭数据库连接,将自动调用commit()以保存所有更改
试题编号:202306-zzh-33
试题类型:单选题
标准答案:D
6.
有一个叫做Animal的类,请问下面哪个选项是正确的创建子类Cat的语法?( )
A.
class Cat(Animal):
B.
class Cat extends Animal:
C.
class Cat inherits Animal:
D.
class Cat is Animal:
试题编号:20230614-ltj-023
试题类型:单选题
标准答案:A
7.
下面的代码定义了一个Circle类,用于表示圆形的信息。请问执行下面的代码后,会输出什么?( )
class Circle():
def __init__(self, radius):
self.pi=3.14
self.radius = radius #半径
def area(self): #面积
return self.pi * self.radius ** 2
def perimeter(self): #周长
return 2 * self.pi * self.radius
c = Circle(4)
print(c.area())
print(c.perimeter())
A.
25.12
50.24
B.
没有输出
C.
50.24
25.12
D.
会报错
试题编号:20230614-ltj-024
试题类型:单选题
标准答案:C
8.
下面哪个代码可以创建一个名为cat的实例,属于Animal类,有color和sound两个属性,分别赋值为"black"和"meow"?( )
A.
cat = Animal()
B.
cat = Animal()
cat.color = "black"
cat.sound = "meow"
C.
cat.color = "black"
cat.sound = "meow"
cat = Animal()
D.
cat = new Animal()
cat.color = "black"
cat.sound = "meow"
试题编号:20230614-ltj-025
试题类型:单选题
标准答案:B
9.
以只读的方式打开文本文件‘a.txt’的代码是?( )
A.
f=open('a.txt','r')
B.
f=open('a.txt','w')
C.
f=open('a.txt','a')
D.
f=open('a.txt','r+')
试题编号:20230616-tjt-001
试题类型:单选题
标准答案:A
10.
有如下Python代码:
f=open('RGB.txt','r')
a=f.readlines()
代码中变量a的数据类型是?( )
A.
字符串
B.
数组
C.
元组
D.
列表
试题编号:20230616-tjt-002
试题类型:单选题
标准答案:D
11.
文本文件'a.txt'为空文件,执行以下Python后,'a.txt'文件中的内容是?( )
n=1
f=open('a.txt','a')
while n<=6:
f.write(str(n))
n+=1
f.close()
A.
123456
B.
6
C.
空
D.
1
试题编号:20230616-tjt-003
试题类型:单选题
标准答案:A
12.
文件a.txt中的内容如图所示:
执行如下Python代码,输出的结果是?( )
s=0
with open('a.txt') as f:
a=f.readlines()
for i in a:
if len(i)>3:
s+=1
print(s)
A.
1
B.
2
C.
3
D.
4
试题编号:20230616-tjt-004
试题类型:单选题
标准答案:B
13.
下面代码的输出结果是?( )
import numpy as np
x = np.array([1, 2, 3, 4, 5])
print(x[2:4])
A.
[3 4]
B.
[2 3]
C.
[2 3 4]
D.
[2 4]
试题编号:20230626-cln-014
试题类型:单选题
标准答案:A
14.
下面代码的输出结果是?( )
import numpy as np
arr = np.array([[1, 2],
[3, 4]])
print(arr.sum())
A.
3
B.
4
C.
6
D.
10
试题编号:20230626-cln-015
试题类型:单选题
标准答案:D
15.
下面代码的输出结果是?( )
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.subplot(2, 1, 1)
plt.plot(x, y1)
plt.xlabel('x轴')
plt.ylabel('y1轴')
plt.subplot(2, 1, 2)
plt.scatter(x, y2, color='r')
plt.xlabel('x轴')
plt.ylabel('y2轴')
plt.tight_layout()
plt.show()
A.
显示一个子图,包含一个包含折线图的区域和一个包含散点图的区域
B.
显示一个子图,包含一个包含折线图和散点图的混合图形
C.
显示两个子图,分别包含折线图和散点图
D.
不显示任何内容
试题编号:20230626-cln-016
试题类型:单选题
标准答案:C
16.
下面代码的输出,最合理的选项结果是?( )
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.title('简单折线图')
plt.show()
A.
显示一个简单的折线图
B.
显示一个已经标注了标题、x轴和y轴标签的简单折线图
C.
显示一个已经标注了 x 轴和 y 轴标签的简单折线图
D.
不显示任何内容
试题编号:20230626-cln-017
试题类型:单选题
标准答案:B
17.
以下Python代码为在tk上绘制一个图形,请问绘制的图形是?( )
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=300)
canvas.create_rectangle(100, 100, 200, 200, outline="red")
canvas.pack()
root.mainloop()
A.
100*200的矩形
B.
300*300的矩形
C.
100*100的矩形
D.
200*100的矩形
试题编号:20230703-jx-020
试题类型:单选题
标准答案:C
18.
有如下Python代码,如图状态下,点击提交按钮,文本框内显示的内容为?( )
import tkinter as tk
def show_selected_option():
selection = variable.get()
p={1:"篮球",2:"排球", 3:"足球"}
label.config(text=f"最喜欢的运动是 {p[selection]}")
root = tk.Tk()
options = [("篮球", 1), ("排球", 2), ("足球", 3)]
variable = tk.IntVar()
for text, value in options:
tk.Radiobutton(root, text=text, variable=variable, value=value).pack()
button = tk.Button(root, text="提 交", command=show_selected_option)
label = tk.Label(root, text="最喜欢的运动是什么?")
button.pack()
label.pack()
root.mainloop()
A.
最喜欢的运动是排球
B.
最喜欢的运动是篮球
C.
最喜欢的运动是足球
D.
最喜欢的运动是2
试题编号:20230703-jx-022
试题类型:单选题
标准答案:A
19.
在tkinter中添加一个按钮的代码是?( )
A.
button = tk.Label(root, text="Hello")
B.
button = tk.Entry(root)
C.
button = tk.Button(root, text="Click")
D.
button = tk.Checkbutton(root, text="Check me")
试题编号:20230703-jx-023
试题类型:单选题
标准答案:C
20.
在tkinter中设置控件widget的背景颜色改为红色的方法是?( )
A.
`widget.bg_color = "red"`
B.
`widget.background = "red"`
C.
`widget.set_bg_color("red")`
D.
`widget.config(bg="red")`
试题编号:20230703-jx-027
试题类型:单选题
标准答案:D
21.
下列程序的运行结果是 [20 16 12 8 4],请填空?( )
import numpy as np
x1 = np.arange( , , )
print(x1)
A.
20,0,4
B.
20,0,-4
C.
0,20,4
D.
0,20,-4
试题编号:20230708-hww-007
试题类型:单选题
标准答案:B
22.
下列程序的运行结果为:2,请填空?( )
import numpy as np
a = np.arange(0,12).reshape(3,4)
print( )
A.
a.shape()
B.
a.shape
C.
a.ndim()
D.
a.ndim
试题编号:20230708-hww-008
试题类型:单选题
标准答案:D
23.
在一个Python表示的二维数组a=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]的第二列位置插入一列新的数据后,能够实现访问该数组中数据8的语句是?( )
A.
a[1][3]
B.
a[2][3]
C.
a[1][4]
D.
a[2][4]
试题编号:20230708-hww-009
试题类型:单选题
标准答案:C
24.
以下程序实现:把'xiaoming'的个人信息填到family的csv文件中,再读取出来。空格处应填?( )
import json
import csv
fam = {'name':'xiaoming','age':18,'gender':'nan'}
with open('family.csv','w') as f:
json. (fam,f)
with open('family.csv','r') as f1:
read1 = json. (f1)
print(read1)
A.
reader,writer
B.
writer,reader
C.
dump,load
D.
load,dump
试题编号:20230708-hww-010
试题类型:单选题
标准答案:C
25.
在Python中使用JSON库进行JSON数据的处理,以下哪个选项描述正确的是?( )
A.
使用json.dumps()函数可以将Python对象转换为JSON字符串
B.
使用json.write()函数可以将Python对象写入JSON文件
C.
使用json.decode()函数可以将JSON字符串解码为Python对象
D.
使用json.parse()函数可以将JSON字符串解析为Python对象
试题编号:20230708-hww-011
试题类型:单选题
标准答案:A
二、判断题(共10题,共20分)
26.
在SQLite操作中,语句conn = sqlite3.connect('test1.db')功能是创建一个新数据库test1.db。如果test1.db已经存在,程序将报错。( )
正确
错误
试题编号:202306-zzh-29
试题类型:判断题
标准答案:错误
27.
当创建一个子类时,它会自动获得父类的所有属性和方法。( )
正确
错误
试题编号:20230614-ltj-026
试题类型:判断题
标准答案:正确
28.
关于类与对象的描述,定义方法__init__() 时,self 必不可少,还必须位于其他形参的后面。( )
正确
错误
试题编号:20230614-ltj-027
试题类型:判断题
标准答案:错误
29.
有如下代码:
with open('RGB.jpg','rb') as f:
a=f.read()
这段代码可以将图像文件RGB.jpg的二进制数据存储在变量a中。( )
正确
错误
试题编号:20230616-tjt-005
试题类型:判断题
标准答案:正确
30.
有如下代码
f=open('123.txt','w')
f.write('hello')
f.close()
执行代码后,文件123.txt中的原有内容将会被覆盖。( )
正确
错误
试题编号:20230616-tjt-006
试题类型:判断题
标准答案:正确
31.
import numpy as np
dt = np.dtype('i8')
print(dt)
上面代码的输出结果是int64。( )
正确
错误
试题编号:20230626-cln-018
试题类型:判断题
标准答案:正确
32.
下列代码中plt.bar(x, y) 函数用于散点图。( )
import matplotlib.pyplot as plt
import numpy as np
x = np.array(['A', 'B', 'C', 'D'])
y = np.array([3, 7, 2, 5])
plt.bar(x, y)
plt.show()
正确
错误
试题编号:20230626-cln-019
试题类型:判断题
标准答案:错误
33.
运行如下代码,点击按钮Greet后label框内显示“Hello, World!”字样。( )
import tkinter as tk
def greet():
label.config(text="Hello, World!")
root = tk.Tk()
label = tk.Label(root, text="")
button = tk.Button(root, text="Greet", command=greet)
label.pack()
button.pack()
root.mainloop()
正确
错误
试题编号:20230703-jx-018
试题类型:判断题
标准答案:正确
34.
Python中的JSON库提供了将自定义对象直接转换为JSON格式的功能。( )
正确
错误
试题编号:20230708-hww-012
试题类型:判断题
标准答案:错误
35.
创建一个二维数据的NumPy数组:
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
访问数组中第二行第三列的元素的表示方式是value=data[2, 3]。( )
正确
错误
试题编号:20230708-hww-013
试题类型:判断题
标准答案:错误
三、编程题(共3题,共30分)
36.
编写一个类`Circle`,包含两个属性`radius`和`color`,以及四个方法`get_area()`、`get_circumference()`、`get_diameter()`和`print_info()`,分别用于计算圆面积、圆周长、圆直径,并打印出圆的半径和颜色。
代码如下,请补全代码。
class Circle:
def __init__(self, radius, color):
①
self.color = color
def get_area(self): #圆面积
return ②
def get_circumference(self): #圆周长
return ③
def get_diameter(self):
return 2 * self.radius
def print_info(self):
print("Radius:", self.radius)
print("Color:", self.color)
circle = Circle(5, "red")
④ #输出圆的半径和颜色
print("Area:", circle.get_area())
print("Circumference:", circle.get_circumference())
print("Diameter:", circle.get_diameter())
试题编号:202312-P6-36
试题类型:编程题
标准答案:
参考程序:
class Circle:
def __init__(self, radius, color):
self.radius = radius
self.color = color
def get_area(self): #圆面积
return 3.14 * self.radius ** 2
def get_circumference(self): #圆周长
return 2 * 3.14 * self.radius
def get_diameter(self):
return 2 * self.radius
def print_info(self):
print("Radius:", self.radius)
print("Color:", self.color)
circle = Circle(5, "red")
circle.print_info() #输出圆的半径和颜色
print("Area:", circle.get_area())
print("Circumference:", circle.get_circumference())
print("Diameter:", circle.get_diameter())
试题难度:一般
试题解析:
评分标准:
(1)self.radius = radius或等效答案;(2分)
(2)3.14 * self.radius ** 2或等效答案;(3分)
(3)2 * 3.14 * self.radius或等效答案;(2分)
(4)circle.print_info()或等效答案。(3分)
37.
学生表操作题
建立学生表,将学号设置为主键,实现对数据的添加和查找。(无需运行通过,写入代码即可)
import sqlite3
con = sqlite3. ① ('./student.db')
cur = ②
sql ='''
③ IF NOT EXISTS Stu (
id INTEGER ④ AUTOINCREMENT,
name TEXT,
age INTEGER,
clas TEXT)
'''
cur.execute(sql)
con.commit()
sql = '''
⑤ (name,age,clas) VALUES(?,?,?)
'''
cur.execute(sql,('张三',16,'二三班'))
con.commit()
试题编号:202312-P6-37
试题类型:编程题
标准答案:
参考程序:
import sqlite3
con = sqlite3. connect ('./student.db')
cur = con.cursor()
sql ='''
CREATE TABLE IF NOT EXISTS Stu (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
age INTEGER,
clas TEXT)
'''
cur.execute(sql)
con.commit()
sql = '''
INSERT INTO Stu (name,age,clas) VALUES(?,?,?)
'''
cur.execute(sql,('张三',16,'二三班'))
con.commit()
试题难度:一般
试题解析:
评分标准:
(1)使用connect 连接数据库,如果没有,就会创建数据库;(2分)
(2)con.cursor(),获取游标对象,操作数据库;(2分)
(3)CREATE TABLE,在数据库中创建;(2分)
(4)PRIMARY KEY,创建表中的关键字;(2分)
(5)INSERT INTO Stu,插入到学生表中。(2分)
38.
统计单词问题
统计英文文本中出现的不同单词个数:读取只包含英文和标点的文件'/data/abc.txt',文件中单词和单词之间用1个空格或标点符号隔开,文末以标点符号结尾,在区分单词大小写的情况下,输出该文本中所出现的不同单词个数。
实现上述功能的Python程序如下,请在划线处填入合适的代码。
f=open('/data/ ① ','r')
text=f.read()
lst=[]
s=""
def judge( ② ):
if st in lst:
return False
else:
return True
for i in range(len(text)):
c= ③
if"a"<=c<="z" or"A"<=c<="Z":
s=s+c
else:
if judge(s):
lst.append(s)
s=""
print("出现的不同单词个数为:",len(lst))
试题编号:202312-P6-38
试题类型:编程题
标准答案:
参考程序:
f=open('/data/abc.txt','r')
text=f.read()
lst=[]
s=""
def judge(st):
if st in lst:
return False
else:
return True
for i in range(len(text)):
c=text[i]
if"a"<=c<="z" or"A"<=c<="Z":
s=s+c
else:
if judge(s):
lst.append(s)
#append()用于在列表末位添加元素
s=""
print("出现的不同单词个数为:",len(lst))
学科网(北京)股份有限公司
$$