选择题 共25道

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25


判断题 共10道

26 27 28 29 30 31 32 33 34 35


编程题 共3道

36 37 38

757 202406Python六级真题-练习
选择题 共25道
01

运行下面代码的正确结果是?( )

with open("example.txt", "a") as file:

file.write("I see you.")

其中example.txt文件内容如下:

This is an example.

2分
登录后查看选项
02

在 Python 中,以下哪个函数可以用于创建一个新的文件?( )

2分
登录后查看选项
03

运行下面代码的正确结果是?( )

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.

2分
登录后查看选项
04

运行下面代码的正确结果是?( )

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!

2分
登录后查看选项
05

编写程序绘制如下图所示的直线,程序空白处应填?( )

import matplotlib.pyplot as p

import numpy as np

x= np.array([0,1,2,____,4,5])

p.plot(x,'o:r')

p.show()

2分
登录后查看选项
06

已知程序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()

图片.png

2分
登录后查看选项
07

在命令行窗口分别运行以下代码,输出结果是?( )

>>>import numpy as np

>>>np.full(6,'6')

2分
登录后查看选项
08

运行以下关于二维数组读取的程序,输出结果是?( )

a=[[1,2,3],[4,5,6],[7,8,9]]

print(a[1][2])

2分
登录后查看选项
09

运行以下代码,绘制出来的第六个柱形图颜色是?( )

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()

2分
登录后查看选项
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])

2分
登录后查看选项
11

下列哪个选项不能在SQLite数据库中运行?( )

2分
登录后查看选项
12

CREAT TABLE Users (id,name,password,role)

关于上述语句,说法错误的是?( )

2分
登录后查看选项
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()

关于以上代码,说法错误的是?( )

2分
登录后查看选项
14

有如下Python程序段:

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)

执行程序后,下列选项中值为1的是?( )

2分
登录后查看选项
15

有如下python程序段:

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的值可能是?( )

2分
登录后查看选项
16

import sqlite3

conn = sqlite3.connect('mydatabase.db')

c = conn.cursor()

c.execute("SELECT * FROM users WHERE age > ?", (30,))

results = c.fetchall()

for row in results:

print(row)

conn.close()

上述代码会查询users表中哪些人的年龄?( )

2分
登录后查看选项
17

class Person():

def __init__(self, name, age):

self.name = name

self.age = age  

def introduce(self):  

return f"My name is {self.name} and I am {self.age} years old."

p = Person("Alice", 30)

print(p.introduce())

以上Python代码,运行结果是?( )

2分
登录后查看选项
18

下列代码的输出结果是?( )

class MyClass():  

def __init__(self):

self.x = 10

self.y = 20

def add(self):

return self.x * self.y

obj = MyClass()

print(obj.add())

2分
登录后查看选项
19

下列代码中,c.method1()和c.method2()的输出结果分别是?( )

class Parent():

def method1(self):

return "Parent's method1"  

class Child(Parent):  

def method1(self):  

return "Child's method1"

def method2(self):  

return super().method1()  

c = Child()

print(c.method1())

print(c.method2())

2分
登录后查看选项
20

下列有关该代码的说法中,不正确的是?( )

class Jdage():  

def __init__(self,name,age):

self.name = name

self.age = age  

def jd(self):  

if self. age<18:  

print(self.name+"还未成年。")  

else:  

print(self.name+"已成年")

my_stu =Jdage("Peter",26)

my_stu.jd()

2分
登录后查看选项
21

你正在为一个小型游戏设计界面,需要一个按钮,玩家点击后会显示一个消息表示游戏开始。如何绑定一个函数到按钮点击事件,以便在点击时执行?( )

2分
登录后查看选项
22

你想创建一个简单的程序,显示一个窗口,用于收集用户的反馈。下列哪个选项是正确的方式来创建一个窗口并运行它?( )

2分
登录后查看选项
23

你正在开发一个图书管理系统,需要在界面上显示“书名”这个词。如何添加一个标签控件到你的窗口中显示文本“书名”?( )

2分
登录后查看选项
24

在一个注册界面中,你需要将一个按钮放置在窗口的底部中央。下列哪个布局管理器最适合实现这个需求?(  )

2分
登录后查看选项
25

有如下python程序段:

file=open('data1.csv')

file1=csv.reader(file)

next(file1)

for i in file1:  

print(_______)

'data1.csv'文件的内容如下图,若要打印每个同学的数学成绩,划线处的代码是?( )


2分
登录后查看选项
判断题 共10道
26

在Python中,可以使用 with 语句来自动关闭一个文件。( )

2分
登录后查看选项
27

下面代码的输出结果是:Hello World! ( )

file = open("exam.txt")


print(file)

file.close()

2分
登录后查看选项
28

sqlite3.connect('路径/文件名'),如果文件不存在,connect函数会自动创建这个数据库文件。( )

2分
登录后查看选项
29

json.dumps() 用于将 Python 对象编码成 JSON 字符串。( )

2分
登录后查看选项
30

json.loads()用于将json字符串恢复成Python对象。( )

2分
登录后查看选项
31

在Python的matplotlib库中,plt.scatter()函数可用来绘制散点图。( )

2分
登录后查看选项
32

下列Python代码中,self参数的作用表示MyClass类的一个实例。 ( )

class MyClass():  

def my_method(self, other_arg):  

print(self, other_arg)

obj = MyClass()

obj.my_method("Hello")

2分
登录后查看选项
33

下列代码的输出结果是5。( )

class A():  

def __init__(self):

self.value = 10

class B(A):

def __init__(self):  

super().__init__()

self.value += 5

b = B()

print(b.value)

2分
登录后查看选项
34

阅读以下代码,请问图表中会显示2条曲线。( )

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 10, 100)

y1 = np.sin(x)

y2 = np.cos(x)

plt.plot(x, y1, label='sin(x)')

plt.plot(x, y2, label='cos(x)')

plt.legend()

plt.show()

2分
登录后查看选项
35

你正在为一个小型的图书管理系统设计界面,其中包括一个“添加图书”按钮,用户点击后可以将新书信息添加到系统中。点击Button控件可以触发一个函数或方法。( )

2分
登录后查看选项
编程题 共3道
36

运动时长统计

汪龙对不同年龄段的人群进行运动时长的调研,数据存储在文件“sport_240622.csv”中,数据内容如下图显示:

用Python编写一段代码帮他完成本次调用的运动时长的统计。具体要求如下:

(1)读取文件sport_240622.csv中的姓名、年龄、运动时长;

(2)输出年龄在40岁以上(含40岁)人群的平均运动时长(保留2位小数);

请根据要求,补全代码。

import csv

with open("/data/sport_240622.csv") as f:

rows = list( ① )

s=0

c=0

pj=0

for row in rows[1:]:

if( ② ):

s=s+ ③

pj=s/c

print( ⑤ )

10分
登录后作答
37

创建学生库

使用Python的sqlite3库完成以下问题。

(1)创建一个名为students的数据库;

(2)在这个数据库中,创建一个名为students_table的表,包含以下字段:id(主键),name(学生的名

字),age(学生的年龄),grade(学生的年级);

(3)向students_table中插入至少5个学生的数据;

(4)查询年龄大于18岁的所有学生,并打印结果;

(5)将名字为"Alice"的学生的年龄增加1岁;

(6)删除名字为"Bob"的学生。

(本题无需运行通过,写入代码即可)

import sqlite3

conn = sqlite3.connect(' ① ')

cursor = conn.cursor()

cursor.execute(''' ② students_table(id INTEGER PRIMARY KEY AUTOINC

students = [ ('Alice', 17, '10th'), ('Bob', 18, '11th'), ('Charlie', 16, '10th'),

cursor.executemany('''INSERT INTO students_table (name, age, grade) VALUES (?, ?,

conn.commit()

cursor.execute('SELECT * FROM students_table ③ ')

print("年龄大于18岁的学生:")

print(cursor. ④ )

cursor.execute('UPDATE students_table SET age = age + 1 WHERE name = "Alice"')

cursor.execute('DELETE FROM students_table WHERE name = "Bob"')

conn.commit()

conn.close()

10分
登录后作答
38

BMI计算器

BMI,身体质量指数,在一定程度反映了人体密度。BMI的计算方法是:体重(kg)除以身高(m)的平方。一般情况下,我国成年人身体质量指数在18.5至23.9内属正常范围,低于18.5表示体重偏瘦,高于23.9表示体重偏胖。

利用类编写程序计算BMI指数,当输入体重和身高后,输出BMI值,并输出体形情况:偏瘦、偏胖、体形标准。程序部分运行情况如下图所示,请补全以下代码。

class Boy():

def __init__(self,tizhong,shengao):

self.tizhong=tizhong

self.shengao=shengao

def bmi(self):

return

a=int(input('输入体重(kg):'))

b= ②

c=Boy(a,b)

if c.bmi()<18.5:  

print("BMI:%d偏瘦。" % c.bmi())

elif ③ :  

print("BMI:%d偏胖。" % c.bmi())

else:  

print("BMI:%d体形标准。" % c.bmi())

10分
登录后作答