首页 › 题库 › Python编程考级 › Python编程六级

Python编程六级 题库

共 604 道题 ,第 9 / 31 页 · 按题型筛选,点「查看答案」核对解析
161 单选题 适中 Python编程考级 ·Python编程六级 · 3.1 概念与特性

下面代码的输出结果正确的是?()

import json
data={
    "name":"Alice",
    "age":25,
    "city":"New York"
}
text=json.dumps(data)
print(text)
A
["name":"Alice","age":25,"city":"New York"]
B
{'name':"Alice",'age':25,'city':"New York"}
C
{"name":"Alice","age":25,"city":"New York"}
D
{'name':'Alice','age':25,'city':'New York'}
答案与解析
正确答案:C

这段代码将 Python 对象用 json.dumps()编码为 JSON 字符串并输出。

题目详情
162 单选题 适中 Python编程考级 ·Python编程六级 · 1.1 类与实例

有关 JSON(JavaScript Object Notation)的概念,正确的是?()

A
是一种数据交换格式
B
是一种编程语言
C
是一种数据库
D
是一种算法
答案与解析
正确答案:A

JSON 是一种基于文本的轻量级数据交换格式,具有易读、易写的特点。它常用于将数据从一个应用程序或平台传输到另一个应用程序或平台。由于其简洁性和普遍性,JSON 已经成为了互联网上数据交换的常用格式。

题目详情
163 单选题 适中 Python编程考级 ·Python编程六级 · 1.1 类与实例

使用 tkinter 设置一个按钮,将按钮放置在窗口最下方,则划线处的代码为?()

from tkinter import*
root=Tk()
root.geometry('300x200')
root.title('my window')
btn1=Button(root,text='按钮 1',bg='red') __________
root.mainloop()
A
btn1.pack(side=TOP)
B
btn1.pack()
C

btn1.pack(side=BOTTOM)

D

btn1.pack(side='')

答案与解析
正确答案:C

side=BOTTOM 由下往上布置。

题目详情
164 单选题 适中 Python编程考级 ·Python编程六级 · 4.2 基本操作与实现

以下代码实现将鼠标移到按钮上时按钮变红,鼠标移开时按钮变蓝,划线处的代码是?()

from tkinter import*
root=Tk()
root.title()
root.geometry('450x350')
btn1=Button(root,text='1')
btn1.place(x=200,y=50,width=40,height=40)
def changebg(event):
    #鼠标移到按钮上按钮变红
    event.widget['bg']='red'
def changebg1(event):
    #鼠标离开按钮上按钮变蓝
    event.widget['bg']='blue'
____________
btn1.bind('',changebg1)
root.mainloop()
A
btn1.bind()
B
btn1.bind('<Enter>',changebg)
C
btn1.bind('<Enter>',changebg1)
D
btn1.bind('<Button-1>',changebg1)
答案与解析
正确答案:B

btn1.bind('<Enter>',changebg)可以实现移动到按钮上按钮变红。

题目详情
165 单选题 适中 Python编程考级 ·Python编程六级 · 4.2 基本操作与实现

以下代码实现点击“点我”按钮,弹出信息“give flower”,划线处的代码是?()

import tkinter as tk
import tkinter.messagebox
from tkinter import*
root=Tk()
bt=Button(root)
bt['text']='点我'
bt.pack()
def dianji(event):
    tk.messagebox.showinfo('message','give flower')
bt.bind('<Botton-1>',_______)
root.mainloop()
A
root
B
dianji
C
def
D
give flower
答案与解析
正确答案:B

bt.bind('',dianji)能使按钮事件和自定义函数绑定,实现效果。

题目详情
166 单选题 适中 Python编程考级 ·Python编程六级 · 2.2 基本操作

使用 tkinter 模块,下列代码能创建一个输入框的是?()

A
from tkinter import*root=Tk()root.title("test")e1=Entry(root)e1.pack()root.mainloop()
B
from tkinter import*root=Tk()root.title("test")e1=Button(root)e1.pack()root.mainloop()
C
from tkinter import*root=Tk()root.title("test")e1=Label(root,text='123')e1.pack()root.mainloop()
D
from tkinter import*root=Tk()root.title("test")e1=Checkbutton(root,text='123')e1.pack()mainloop()
答案与解析
正确答案:A

e1=Entry(root)可以创建文本框,所以选 A。

题目详情
167 单选题 适中 Python编程考级 ·Python编程六级 · 2.2 基本操作

执行以下代码,数据表中共有几条数据?()

import sqlite3
conn=sqlite3.connect('student_info.db')
cursor=conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS Student
(id INTEGER PRIMARY KEY,name TEXT,age INTEGER)''')
cursor.execute("INSERT INTO Student(id,name,age)VALUES(1,'Alice',20)")
cursor.execute("INSERT INTO Student(id,name,age)VALUES(2,'Bob',22)")
cursor.execute("INSERT INTO Student(id,name,age)VALUES(3,'Charlie',21)")
cursor.execute("SELECT*FROM Student")
students=cursor.fetchall()
for student in students:
    print(f"ID:{student[0]},Name:{student[1]},Age:{student[2]}")
cursor.execute("UPDATE Student SET age=23 WHERE id=2")
cursor.execute("DELETE FROM Student WHERE id=3")
conn.commit()
conn.close()
A
2
B
3
C
4
D
5
答案与解析
正确答案:A

创建了数据表并插入了 3 条记录,然后修改了其中 id=2 的记录,然后删除了id=3 的记录,最后只有 2 条记录。

题目详情
168 单选题 适中 Python编程考级 ·Python编程六级 · 2.2 基本操作

如下代码创建一个数据库表,表内有几个字段?()

import sqlite3
connection=sqlite3.connect('test.db')
cursor=connection.cursor()
cursor.execute('''CREATE TABLE employees
    (id INTEGER PRIMARY KEY,name TEXT,age INTEGER,salary REAL)''')
connection.close()
A
3
B
4
C
5
D
6
答案与解析
正确答案:B

答案为 B,共设置了四个字段,分别为 id,name,age,salary。

题目详情
169 单选题 适中 Python编程考级 ·Python编程六级 · 2.2 基本操作

以下哪个代码片段用于创建一个 SQLite 数据库 mydatabase 连接?()

A
'connection=sqlite3.connection("mydatabase.db")'
B
'connection=sqlite3.connect("mydatabase.db")'
C
'connection=sqlite3.open("mydatabase.db")'
D
'connection=sqlite3.connect("mydatabase.txt")'
答案与解析
正确答案:B

在 sqlite3 模块中,使用`connect()`函数创建一个 SQLite 数据库连接。数据库的后缀名是.db。

题目详情
170 单选题 适中 Python编程考级 ·Python编程六级 · 2.2 基本操作

运行以下代码输出的数据为?()

import sqlite3
connection=sqlite3.connect("students.db")
cursor=connection.cursor()
cursor.execute("CREATE TABLE students(name TEXT,age INTEGER)")
cursor.execute("INSERT INTO students VALUES('John',19)")
cursor.execute("INSERT INTO students VALUES('John',18)")
cursor.execute("DELETE FROM students WHERE name='John'")
cursor.execute("INSERT INTO students VALUES('John',17)")
cursor.execute("UPDATE students SET age=22 WHERE name='John'")
cursor.execute("SELECT*FROM students")
results=cursor.fetchall()
for row in results:
    print(row)
connection.close()
A
('John',22)
B
('John',18)
C
('John',19)
D
('John',17)
答案与解析
正确答案:A

创建了数据库和表之后,插入了二条姓名都为'John'数据,然后删除'John'相关数据时二条数据都被删除,重新插入'John'的数据并修改为 22,故最后查找出的结果为 22。

题目详情
171 单选题 适中 Python编程考级 ·Python编程六级 · 1.1 类与实例

以只读方式打开 d:\myfile.txt 文件的代码是?()

A
f=open("d:\\myfile.txt")
B
f=open("d:\\myfile.txt","rt+")
C
f=open("d:\\myfile.txt","r+")
D
f=open("d:\\myfile.txt","a")
答案与解析
正确答案:A

只读方式:r,不写时,默认为r;打开文本文档默认为t,可以省略,"rt+"与"r+"模式一样,为可读可写模式;"a"为追加模式。

题目详情
172 单选题 适中 Python编程考级 ·Python编程六级 · 1.1 类与实例

在进行文件读写时,以下为非二进制文件的是?()

A
timu.docx
B
timu.mp4
C
timu.txt
D
timu.jpg
答案与解析
正确答案:C

文本文件的格式为:txt;二进制文件的格式为:图像、音频、视频、office 文档、可执行性文档等。

题目详情
173 单选题 适中 Python编程考级 ·Python编程六级 · 3.1 概念与特性

程序填空:程序的输出结果如下图所示,程序空白处应该是?()

with open("./text.txt","r",encoding='utf-8')as f:
    a=f.read()
    print(a)

A
10
B
11
C
9
D
12
答案与解析
正确答案:B

换行符也占 1 位字符数。

题目详情
174 单选题 适中 Python编程考级 ·Python编程六级 · 1.1 类与实例

下面的程序输出结果是"like",请填空?()

A
2,0
B
2,1
C
2,2
D
0,2
答案与解析
正确答案:A

seek(偏移值[,起点]),起点可以省略,0 表示从文件头开始,1 表示从当前位置开始,2 表示从文件尾开始,默认为 0。

题目详情
175 单选题 适中 Python编程考级 ·Python编程六级 · 1.1 类与实例

运行以下程序,输出的结果是?()

class F():
    def__init__(self,a):
        self.x=a+1
    def b(self):
        return self.x*self.x
f=F(3)
print(f.b())
A
4
B
8
C
16
D
20
答案与解析
正确答案:C

self.a=a+1,f=F(3)=3+1=4,b*b=16。

题目详情
176 单选题 适中 Python编程考级 ·Python编程六级 · 1.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用于在文件的结尾追加内容。也就是说,新的内容将会被写入到已有内容之后。

题目详情
177 单选题 适中 Python编程考级 ·Python编程六级 · 2.2 基本操作

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

A
write( )
B
create( )
C
new( )
D
open( )
答案与解析
正确答案:D

在Python中,要创建一个新的文件,你可以使用内置的 `open()` 函数,并且模式(mode)应该为 "w"(表示写入模式),这样就会创建(如果文件不存在的话)或覆盖(如果文件已存在)一个文件。

因此,选项 `open()` 是正确的。选项 `create()` 和 选项 `new()` 并不是Python内置的函数。而选项 `write()` 是一个用于写入文件的函数,但它本身并不创建文件。

题目详情
178 单选题 适中 Python编程考级 ·Python编程六级 · 1.1 类与实例
运行下面代码的正确结果是?
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 统计行数。最后,程序输出文件的行数。

题目详情
179 单选题 适中 Python编程考级 ·Python编程六级 · 2.2 基本操作
运行下面代码的正确结果是?
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' 模式打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。

题目详情
180 单选题 适中 Python编程考级 ·Python编程六级 · 2.1 概念与特性

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

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

根据折线图判断点的坐标。

题目详情