首页 › 题库 › Python编程考级 › 1.1 类与实例

Python编程六级 题库

共 301 道题 ,第 6 / 16 页 · 按题型筛选,点「查看答案」核对解析
章节 1.1 类与实例 清除
101 单选题 适中 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 由下往上布置。

题目详情
102 单选题 适中 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"为追加模式。

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

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

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

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

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

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

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

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

题目详情
105 单选题 适中 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。

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

题目详情
107 单选题 适中 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 统计行数。最后,程序输出文件的行数。

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

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

>>>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')第一个参数指数量。

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

关于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]指第三个数据。

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

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

A
10
B
'10'
C
[10,11]
D
None
答案与解析
正确答案:C

SQLite支持的数据类型包括:NULL、INTEGER、REAL、TEXT、BLOB,分别对应Python的数据类型:None、int、float、str、bytes。

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

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

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

A
id作为唯一标识,约束条件为PRIMARY 和 NOT NULL
B
name是可以重复的password的约束条件为NOT
C
NULL
D
role 为一个不定长的字符串
答案与解析
正确答案:B

name作为用户名是不可以重复的。

题目详情
112 单选题 适中 Python编程考级 ·Python编程六级 · 1.1 类与实例
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

用户名admin不能重复。

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

有如下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的是?( )

A

dp[m][n]

B

dp[n][m]

C

dp[len(dp)-1][0]

D

dp[m][0]

答案与解析
正确答案:B

程序运行结束后, dp的值为[[0, 0, 0], [0, 0, 0], [3, 3, 3],[0, 0, 1]],故选B。

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

有如下python程序段:

import csv
file=open('data1.csv')
file1=csv.reader(file)
next(file1)
for i in file1:
    print(_______)

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

A
i[1]
B
i[2]
C
i[3]
D
file[2]
答案与解析
正确答案:B

索引2是数学成绩,故选B。

题目详情
115 单选题 适中 Python编程考级 ·Python编程六级 · 1.1 类与实例
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表中哪些人的年龄?( )

A
年龄等于30的人
B
年龄大于30的人
C
年龄小于30的人
D
所有人的年龄
答案与解析
正确答案:B

SQL查询中的age > ?与参数(30,)结合,表示查询users表中年龄大于30岁的人。

题目详情
116 单选题 适中 Python编程考级 ·Python编程六级 · 1.1 类与实例
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代码,运行结果是?( )

A
My name is Alice and I am 30 years old.
B
My name is Person and I am 30 years old.
C
My name is Alice and I am 0 years old.
D
My name is 30 and I am Alice years old.
答案与解析
正确答案:A

在Person类的__init__方法中,self.name和self.age分别被赋值为"Alice"和30。introduce方法返回一个字符串,其中包含这些属性的值。因此,当调用p.introduce()时,返回的字符串将是My name is Alice and I am 30 years old.。

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

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

class MyClass():  
    def __init__(self):  
        self.x = 10  
        self.y = 20  
  
    def add(self):  
        return self.x * self.y  
  
obj = MyClass()  
print(obj.add())
A

100

B

30

C

200

D

400

答案与解析
正确答案:C

在MyClass中,我们定义了两个实例变量x和y,并提供了add方法用于返回它们的积。当我们创建MyClass的一个实例并调用add方法时,它返回x和y的乘积,即200。

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

下列代码中,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())
A
Parent's method1Parent's method1
B

Child's method1

Child's method1

C

Child's method1

Parent's method1

D

Parent's method1

Child's method1

答案与解析
正确答案:C

Child类重写了Parent类的method1方法,所以c.method1()调用的是Child类中的method1方法,输出"Child's method1"。Child类的method2方法中使用了super().method1()来调用父类Parent的method1方法,因此c.method2()的输出是"Parent's method1"。

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

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

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

创建的类名称为 Jdage

B

my_stu 为Jdage类的一个对象实例

C

程序代码执行后的结果为“Peter 已成年。”

D

def jd(self)的功能是定义jd 函数

答案与解析
正确答案:D

def jd(self)的功能是定义jd 操作,而不是函数,因此错误的是 D。

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

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

A
button = Button(root, text="开始游戏", command=startGame)
B

button = Button(root, text="开始游戏", onclick=startGame)

C

button = Button(root, text="开始游戏", action=startGame)

D

button = Button(root, text="开始游戏", event=startGame)

答案与解析
正确答案:A

在Tkinter中,command 参数用于绑定一个函数到按钮的点击事件。当按钮被点击时,指定的startGame函数将被执行。其他选项提供的属性并不存在于Tkinter的Button类中。

题目详情