青少年軟件編程(Python)等級考試試卷(六級)
答案解析:https://blog.csdn.net/qq_33897084/article/details/147341458
一、單選題(共25題,共50分)
1. 在tkinter的常用組件中,可以顯示文本和位圖的是?( )
A. Entry
B. Label
C. Text
D. Button
Label標簽組件,可以顯示文本和位圖
Text文本組件,用于顯示多行的文本內容
Button按鈕組件,在程序中顯示按鈕
2. 下列選項中JSON對象格式表示方法正確的是?( )
A. jsonObject = [“name”: “John”, “age”: 30, “city”: “New York”]
B. jsonObject = [‘name’: ‘John’, ‘age’: 30, ‘city’: ‘New York’]
C. jsonObject= {“name”: “John”, “age”: 30, “city”: “New York”}
D. jsonObject = {“name”: “John”; “age”: 30; “city”: “New York”}
3. 使用tkinter模塊創建GUI應用程序時,可以使用什么方法來保持應用程序活動?( )
A. mainloop()
B. run()
C. show()
D. create()
4. 小明編寫Python程序時,使用open函數打開"data1.txt"文件并將相關數據追加到文件中,下列代碼最合理的是?( )
A. open(‘data1.txt’,‘r’')
B. open(‘data1.txt’,‘w’)
C. open(‘data1.txt’,‘a’)
D. open(‘data1.txt’,‘w+’)
5. 運行以下程序,輸出結果是?( )
a=[[‘a’,‘b’,‘c’],[1,2,3],[‘d’,‘e’,‘f’],[4,5,6]]
print(a[0:2])
A. [[‘a’, ‘b’, ‘c’], [1, 2, 3]]
B. [[‘a’,‘b’,‘c’],[1,2,3],[‘d’,‘e’,‘f’]]
C. ‘b’
D. ‘c’
6. 以下Python程序的運行結果為?( )
import numpy as np
print(np.linspace(0, 10, 11))
A. [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
B. [ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]
C. [ 0. 0.9 1.8 2.7 3.6. 4.5 5.4 6.3 7.2 8.1 9.]
D. [0. , 0.9, 1.8, 2.7, 3.6, 4.5, 5.4, 6.3, 7.2, 8.1, 9.]
7. 如果要從一個CSV文件中讀取某一行的數據,然后進行一些分析,如求和、平均值、最大值、最小值等。下面哪個方法可以用來讀取文件中的一行?( )
A. readline()
B. readlines()
C. read()
D. readall()
8. 假設你正在編寫一個圖書管理系統,需要創建一個表示書籍的類,其中包含書名和作者名。如何正確創建一個名為"Python Basics"由"Jane Ming"編寫的Book類的實例?( )
class Book:
def init(self, title, author):
self.title = title
self.author = author
A. Book(“Python Basics”, “Jane Ming”)
B. Book.new(“Python Basics”, “Jane Ming”)
C. new Book(“Python Basics”, “Jane Ming”)
D. Book.create(“Python Basics”, “Jane Ming”)
9. 在開發一個簡單的員工管理系統時,你定義了一個Employee類,其中包含一個表示員工計數的類屬性。每當創建一個Employee實例時,以下哪項會發生?( )
class Employee:
employee_count = 0
def init(self, name):
self.name = name
Employee.employee_count += 1
A. name屬性的值會遞增
B. employee_count屬性的值會遞增
C. employee_count屬性會重置為0
D. name屬性會被共享到所有實例
10. 在一個動物園管理軟件中,你需要創建一個Bird類作為Animal類的子類。創建一個Bird實例時,以下哪個描述是正確的?( )
class Animal:
def init(self, name):
self.name = name
class Bird(Animal):
def fly(self):
print(“Flapping wings”)
A. Bird實例不能訪問name屬性
B. Bird類需要重新定義一個__init__方法來設置name屬性
C. 創建Bird實例時必須提供name參數
D. Bird類的fly方法會覆蓋Animal類的任何方法
11. 有Python程序段如下,下列選項錯誤的是?( )
class Cat():
def init(self,name,color):
self.name=name
self.color=color
def sit(self):
print(self.color +self.name+“is sitting.”)
A. A.使用class關鍵字來定義一個Cat類,類名的首字母必須要大寫
B. 方法__init()__定義了三個參數:self、name和color,其中self參數可省略
C. 語句“self.color=color”獲取存儲在參數color中的值并存儲到self的屬性color中
D. Cat類還定義了一個方法sit()
12. 有如下Python代碼:
with open(‘data.txt’) as f:
data1=f.readline()
print(data1)
運行程序后,下列說法正確的是?( )
A. 讀取data文件中的所有內容
B. 讀取data文件中的一行數據,返回的數據是列表
C. 讀取data文件中的一行數據,返回的數據是字符串
D. 無法打開data文件
13. 有如下Python代碼:
with open(‘data1.txt’) as f:
data=f.readline()
with open(‘data2.txt’,‘w’) as f:
f.write(data)
‘data1.txt’內容如下圖所示,‘data2.txt’文件無內容
執行該代碼后,下列說法正確的是?( )
A. data2.txt文件中仍舊無內容
B. data2.txt文件中的內容為’hello,world,’
C. data2.txt文件中的內容為’hello,world,I like Python’
D. data1.txt文件中的內容將丟失
14. 執行下列代碼,輸出結果是?( )
import numpy as np
x = [(0,1,2),(3,4,5),(6,7,8)]
a = np.asarray(x)
print (a)
A. [(0 1 2)
(3 4 5)
(6 7 8)]
B. [(0 1 2),
(3 4 5),
(6 7 8)]
C. [[0 1 2]
[3 4 5]
[6 7 8]]
D. [[0 1 2],
[3 4 5],
[6 7 8]]
15.使用matplotlib模塊繪制如圖所示的圖像:
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,2,50)
plt.plot(x,x2,label=“quadratic”)
plt.plot(x,x3,label=“cubic”)
plt.xlabel(“X axis”)
plt.title(“Simple Plot”)
plt.legend()
plt.show()
劃線處應填入的代碼為?( )
A. plt.ylabel()
B. plt.ylabel(“Y axis”)
C. plt.ylim(“Y axis”)
D. plt.ylim()
16. 下列關于plt.plot(x,y1,label=“sin(x )” ,color=“r”,linewidth=2)的說法,錯誤的是?( )
A. 繪制出的圖像為線形圖
B. color指定了圖表的背景色
C. label給線條指定了一個標簽名
D. linewidth設置了線條的寬度
17. 如圖所示是使用Python編程完成的一組圖像,其程序代碼如下:
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-1,1,5)
for i in _______________:
y=x*i
plt.scatter(x ,y)
plt.show()
橫線處為一個列表,該列表中有幾個元素?( )
A. 10
B. 5
C. 40
D. 1
18. 有如下Python程序:
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
arr = np.add(a, b)
print(arr[0][1])
運行程序后,輸出的結果是?( )
A. 6
B. 14
C. 8
D. 12
19. 以下哪個選項是正確地使用Python SQLite3模塊執行查詢并獲取所有結果的語句?( )
A. result = conn.execute(“SELECT * FROM employees”)
B. result = conn.execute(“SELECT * FROM employees”).fetchall()
C. result = conn.execute(“SELECT * FROM employees”).fetchone()
D. result = conn.execute(“CREAT * FROM employees”)
20. 下列關于SQLite數據庫的說法,不正確的是?( )
A. SQLite是一個輕量級的、跨平臺的關系型數據庫,具有獨立性、非服務式、元處理、開放性等特點
B. SQLite表SQLite和傳統數據庫(如Oracle、SQL Server等)相比,功能一樣強大
C. 可以使用create table用來創建表
D. connect( )可以用來連接SQLite數據庫
21. 有如下Python程序段:
import sqlite3
conn = sqlite3.connect(‘cj.db’)
cur = conn.cursor()
a= cur.execute(“SELECT * FROM student”).fetchall()
conn.commit()
conn.close
s=0
for i in a:
s=s+i[1]
pirnt(s)
其中’cj.db’的student表中內容如下圖所示,則執行程序后,打印的結果是?( )
A. 100
B. 294
C. 289
D. 李雷
22. 有如下Python代碼:
import sqlite3
conn = sqlite3.connect(‘cj.db’)
cursor = conn.cursor()
cursor.execute(‘SELECT * FROM student’)
rows = cursor.fetchmany(5)
for row in rows:
print(row)
cursor.close()
conn.close()
已知查詢的數據表中的數據超過5行,則執行程序后,下列說法正確的是?( )
A. 鏈接的數據庫文件是student
B. 查詢的數據表名稱是select
C. 若只想獲取一條查詢數據,可以將fetchmany()換成fetchall()
D. print(len(rows))得到的結果是5
23. 有如下Python代碼:
def f(*args):
print(comboxlist.get()) #打印選中的值
import tkinter as tk
from tkinter import ttk
root=tk.Tk() #構造窗體
comboxlist=ttk.Combobox(root)
comboxlist[“values”]=(“a”,“b”,“c”,“d”)
comboxlist.current(0)
comboxlist.bind(“<>”,f)
comboxlist.pack()
以下說法正確的是?( )
A. comboxlist下拉框中的選項有1,2,3,4
B. comboxlist沒有和任何事件綁定
C. 點擊下拉框中的a,a會被輸出打印
D. 下拉框不會顯示在窗體中
24. 有如下Python代碼:
from tkinter import *
def close_app():
root.destroy()
root = Tk()
root.geometry(‘300x200’)
root.title(‘my window’)
btn1 = Button(root,text=‘按鈕1’,bg=‘blue’,command=close_app)
btn1.pack(side=BOTTOM)
root.mainloop()
執行代碼后,說法正確的是?( )
A. 窗口的標題是’按鈕1’
B. 按鈕的顏色是白色
C. 按鈕位于窗口最下方
D. 單擊按鈕后,窗口不會關閉
25. 在使用csv.writer向CSV文件寫入數據時,newline=''參數的作用是?( )
A. 它指定了寫入文件的新行字符
B. 它防止在Windows系統上寫入額外的空行
C. 它防止在Windows系統上寫入額外的換行符
D. 它用于指定編碼方式
二、判斷題(共10題,共20分)
26. Python的tkinter庫中,使用grid()方法管理布局,需要將Label標簽放入第一行第一列的寫法是:grid(row=1,column=1) 。( )
27. JSON數據是純文本格式,因此它可以很容易地被機器解析和生成。( )
28. 使用json.loads( )函數將Python對象轉換為JSON字符串。( )
json.loads( )將已編碼的 JSON 字符串解碼為 Python 對象。
29. 繪制圖形如圖所示,畫線處的的語句填寫是否正確 ?( )
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.xlabel(‘y=x**2’)
plt.show()
30. 有一個文本文件(data.txt),該文件中包含多行文本。每行有一個整數。下面這段程序能從該文件中讀取每一行的內容,并將其轉換為整數,然后求和并輸出結果。( )
with open(“data.txt”) as f:
total = 0
for line in f:
total += int(line)
print(total)
31. 使用matplotlib的 plt.text() 函數可以在圖表的任意位置添加文本注釋,但是無法控制文本的對齊方式。( )
32. 在Python中,子類可以覆蓋父類中的方法,但不能覆蓋父類中的屬性。( )
33. readline()函數讀取文本文件內容,返回的是一個列表,其中每一行的數據為一個元素。( )
34. Python的SQLite庫中的execute()方法只能執行SQL查詢,不能執行SQL命令。( )
35. __init__是一個類的構造器,Python中每個類只能有一個__init__方法。( )
三、編程題(共3題,共30分)
36. 計算圓形的面積
編寫一個父類Shape,具有一個屬性color和一個方法get_area(),用于計算形狀的面積。然后,基于Shape類創建子類Circle,表示圓形。子類需要實現父類的方法get_area()來計算自身的面積。
具體要求:
(1)Shape類包含以下屬性和方法:
屬性:color(字符串類型,表示顏色)
方法:get_area()(計算并返回面積,方法體為空,由子類實現)
(2)Circle類是Shape類的子類,包含以下屬性和方法:
屬性:radius(浮點數類型,表示圓形的半徑)
方法:重寫并實現get_area()方法,根據圓形的半徑計算并返回面積。
請根據要求,補全代碼。
import math
class Shape:
def ① :
self.color = color
def get_area(self):
Pass
class Circle( ② ):
def ③ :
super().init(color)
self.radius = radius
def get_area(self):
return math.pi * self.radius ** 2
circle = ④ (“blue”, 2)
print( ⑤ )
評分標準:
(1)init(self,color) 或等效答案;(2分)
(2)Shape 或等效答案;(2分)
(3)init(self,color,radius) 或等效答案;(3分)
(4)Circle 或等效答案;(1分)
(5)circle.get_area() 或等效答案。(2分)
37. 學生數據庫
編寫程序操作SQLite數據庫,并讀出表中的數據。
具體要求如下:
(1)打開數據庫連接;
(2)清除已存在的表 -students;
(3)創建一個表students;
(4)向新表插入數據;
(5)讀取表students中數據。
(本題無需運行通過,寫入代碼即可)
import sqlite3
#打開數據庫連接
conn = sqlite3. ① (‘test.db’)
print(“Opend database successfully”)
#清除已存在的表 -students
conn. ② (‘’‘DROP TABLE students’‘’);
conn. ③
#創建一個表students
conn.execute(‘’’ ④ students
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL);‘’‘)
print(“Table created successfully”);
conn.commit()
#插入數據
conn.execute(“INSERT INTO students(ID,NAME,AGE) VALUES(1,‘Allen’,25)”);
conn.execute(“INSERT INTO students(ID,NAME,AGE) VALUES(2,‘Maxsu’,20)”);
conn.execute(“INSERT INTO students(ID,NAME,AGE) VALUES(3,‘Teddy’,24)”);
conn.commit()
print(“Records Insert successfully”);
print(“-------------------”);
#讀取表students
⑤ =conn.execute(“SELECT * from students”)
print (“ID NAME AGE”)
for it in cursor:
for i in range(len(it)):
print(it[i])
print (’\n’)
conn.close()
評分標準:
(1)connect 或等效答案;(2分)
(2)execute 或等效答案;(2分)
(3)commit() 或等效答案;(2分)
(4)CREATE TABLE 或等效答案;(2分)
(5)cursor 或等效答案。(2分)
38. 進制問題
如圖所示為’data1.txt’中存儲的數據,其中每一行都為24個由’0’和’1’組成的二進制數字,現編寫Python程序讀取’data1.txt’文件中的數據,并將二進制數字轉換成十進制數字,轉換規則為每八位二進制數字轉換為一個十進制數,相應的Python代碼如下,請補充完整。
f=open(‘/data/ ① ‘,‘r’)
line=f.readline().strip(’\n’)
s=0
s1=‘’
while line:
for i in range(len(line)):
s=s*2+ ②
if (i+1)%8==0:
s1=s1+str(s)+‘,’
③
s1=s1+‘\n’
line= ④ .strip(‘\n’)
print(s1)
評分標準:
(1)data1.txt 或等效答案;(2分)
(2)int(line[i]) 或等效答案;(2分)
(3)s=0 或等效答案;(3分)
(4)f.readline() 或等效答案。(3分)
答案解析:https://blog.csdn.net/qq_33897084/article/details/147341458