青少年软件编程(python六级)等级考试试卷-客观题(2024年6月)
=========================================================================
更多内容和历年真题请查看网站:【试卷中心 -----> 电子学会 ----> 机器人技术 ----> 六级】
网站链接
青少年软件编程历年真题模拟题实时更新
=========================================================================
青少年软件编程(python六级)等级考试试卷-客观题(2024年6月)
一、单选题
第 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
解析
这行代码使用了`with`语句来打开"example.txt"文件,a用于在文件的结尾追加内容。也就是说,新的内容将会被写入到已有内容之后。
第 2 题 单选题
在 Python 中,以下哪个函数可以用于创建一个新的文件?( )
A.write( )
B.create( )
C.new( )
D.open( )
答案 D
解析
在Python中,要创建一个新的文件,你可以使用内置的 `open()` 函数,并且模式(mode)应该为 "w"(表示写入模式),这样就会创建(如果文件不存在的话)或覆盖(如果文件已存在)一个文件。
因此,选项 `open()` 是正确的。选项 `create()` 和 选项 `new()` 并不是Python内置的函数。而选项 `write()` 是一个用于写入文件的函数,但它本身并不创建文件。
第 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 AnimalOnce 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
解析
此程序文件名为 “exmaple.txt”。程序使用 with 语句打开文件,在文件的每一行上迭代,并使用计数器 line_count 统计行数。最后,程序输出文件的行数。
第 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
解析
代码的作用是在文件 myfile.txt 中写入"This is my first Python program."覆盖掉原本的内容“Hello World!”。
with 关键字用于创建一个上下文环境,在该上下文环境中,代码块执行完毕后,会自动关闭文件,无需手动调用 close() 方法。使用 with 可以避免一些文件打开和关闭的常见错误,使代码更加简洁和容易维护。
使用 'w' 模式打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。
第 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
解析
绘图的X坐标为索引号,Y值表示高度。
第 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
解析
np.full(6,'6')第一个参数指数量。
第 8 题 单选题
运行以下关于二维数组读取的程序,输出结果是?( )
a=[[1,2,3],[4,5,6],[7,8,9]]
print(a[1][2])
A.2
B.4
C.5
D.6
答案 D
解析
二维数组左上角坐标为[0,0]。
第 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
解析
json格式数据一个逗号为一组数据,d[2]指第三个数据。
=========================================================================
更多内容和历年真题请查看网站:【试卷中心 -----> 电子学会 ----> 机器人技术 ----> 六级】
网站链接
青少年软件编程历年真题模拟题实时更新
=========================================================================