计算机基础期末考试模拟试卷(新版)
一、单选题(共15题,30.0分)
1. (单选题, 2.0分) 在类中定义私有成员的方法是____。
A. 以public关键字声明
B. 以一个下划线开始
C. 以两个下划线开始
D. 不以下划线开始
2. (单选题, 2.0分) 在下列关于Python模块和包的说法中,正确的是____。
A. 一个包中的__init__.py文件必须存在
B. import语句只能导入标准库
C. 一个.py文件可以作为一个模块
D. 包不能包含子包
3. (单选题, 2.0分) 执行下面的代码后,在第2行第3列的子图区域绘制散点图的语句是____。
python
fig, ax = plt.subplots(nrows=3, ncols=4, figsize=(6,4))
A. ax[1][2].scatter([1,2,3], [4,5,6])
B. ax[2][3].scatter([1,2,3], [4,5,6])
C. ax[5].scatter([1,2,3], [4,5,6])
D. ax[1,2].scatter([1,2,3], [4,5,6])
4. (单选题, 2.0分) 以下列表推导式中,能正确生成1到20之间所有能被3整除的数的平方组成的列表是____。
A. [x**2 for x in range(1,21) if x%3==0]
B. [x**2 for x in range(3,21) if x%3!=0]
C. [x**3 for x in range(1,21) if x%3==0]
D. [x**2 for x in range(1,21) if x%3:=0]
5. (单选题, 2.0分) 执行语句from PIL import Image后,要保存图像文件,从语法上来说,下面____是正确的。
A. im.save("D:\Image\test.png")
B. Image.save("D:\Image\test.png")
C. im.save(r"D:\Image\test.png")
D. save(im, "D:\Image\test.png")
6. (单选题, 2.0分) fp是文件对象,语句fp.read()的返回值数据类型是____。
A. 列表
B. 字符串
C. 字典
D. 元组
7. (单选题, 2.0分) 在下列关于异常处理的说法中,错误的是____。
python
try:
n = int(input())
k = int(input())
m = n / k
print(m)
except ValueError:
print("error1")
except ZeroDivisionError:
print("error2")
finally:
print("end")
A. 如果k输入0,会执行print("error2")
B. finally语句块一定会执行
C. 如果n输入abc,会执行print("error1")
D. 如果没有异常,不会执行print("end")
8. (单选题, 2.0分) jieba库中,下列语句属于搜索引擎模式的是____。
A. lst = jieba.lcut('Python是一门编程语言')
B. lst = jieba.lcut('Python是一门编程语言', cut_all=True)
C. lst = jieba.lcut_for_search('Python是一门编程语言')
D. lst = jieba.analyse.extract_tags('Python是一门编程语言')
9. (单选题, 2.0分) 已知str1 = 'hello**world*python',则str1.split('*')的返回值是____。
A. ['hello', 'world', 'python']
B. ['hello', '', 'world', 'python']
C. ['hello', '', '', 'world', '', 'python']
D. 报错
10. (单选题, 2.0分) 下面属于面向对象三大特征的是____。
A. 封装、继承、多态
B. 属性、方法、事件
C. 类、对象、模块
D. 数据、函数、过程
11. (单选题, 2.0分) 在下列关于CSV文件的说法中,正确的是____。
A. CSV文件只能用Excel打开
B. CSV文件数据之间必须用分号分隔
C. CSV文件是二进制文件
D. CSV文件可以用记事本打开
12. (单选题, 2.0分) 利用matplotlib绘图时,用于设置坐标轴标签的函数是____。
A. xlabel() 和 ylabel()
B. title()
C. legend()
D. grid()
13. (单选题, 2.0分) 假定执行了如下的语句,则____。
python
fp = open("data.txt", "r")
A. fp.write("hello")是正确的
B. fp.read()是正确的
C. fp.writelines(["a","b"])是正确的
D. 以上都不正确
14. (单选题, 2.0分) 绘制一条从点(1,2)到点(3,4)的直线,正确的语句是____。
A. plt.plot([1,3], [2,4])
B. plt.plot((1,2), (3,4))
C. plt.plot(1,2,3,4)
D. plt.plot([2,4], [1,3])
15. (单选题, 2.0分) PIL库中用于图像旋转的方法是____。
A. rotate()
B. resize()
C. thumbnail()
D. transpose()
二、程序填空题(共2题,24.0分)
16. (程序填空题, 12.0分) 程序的功能是读取彩色图像,将其转换为灰度图像,统计每个灰度级的像素数量,并绘制条形图。
python
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
__(1)__:
im = Image.open(r"flower.jpg")
im = im.__(2)__("L")
arr = np.array(im)
gray_count = [0] * 256
for row in arr:
for pixel in row:
gray_count[__(3)__] += 1
plt.__(4)__(range(256), gray_count, width=1, color='gray')
plt.xlabel('灰度值')
plt.ylabel('像素数量')
plt.title('灰度直方图')
plt.show()
except FileNotFoundError:
print("文件未找到!")
第1空:________
第2空:________
第3空:________
第4空:________
17. (程序填空题, 12.0分) 程序的功能是读取文本文件,使用jieba进行分词,统计词频并输出前10个高频词。
python
import jieba
file = open(r"article.txt", "r", __(1)__='utf-8')
text = file.read()
file.close()
words = __(2)__(text)
word_count = {}
for word in words:
if len(word) >= 2: # 只统计长度>=2的词
if word __(3)__ word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 按词频排序
items = list(word_count.items())
items.sort(key=lambda x: x[1], __(4)__=True)
print("前10个高频词:")
for i in range(min(10, len(items))):
print(f"{items[i][0]}: {items[i][1]}")
第1空:________
第2空:________
第3空:________
第4空:________
三、程序调试题(共2题,20.0分)
18. (程序调试题, 10.0分) 下面的程序有4个错误,请改正。改正的语句后面添加注释:#此行有错误,已经改正
程序功能:绘制某产品季度销售额的柱状图和折线图组合。
python
import matplotlib as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
fig = plt.figure(figsize=(10,6))
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
sales = [120, 150, 135, 180]
growth = [0, 25, -10, 33]
plt.subplot(1,2,1)
plt.hist(range(4), sales)
plt.title('季度销售额')
plt.xticks(range(4), quarters)
plt.ylabel('销售额(万元)')
plt.subplot(1,2,2)
plt.plot(range(4), growth, 'ro-')
plt.title('销售增长率')
plt.yticks(range(4), quarters)
plt.ylabel('增长率(%)')
plt.show()
请将改正后的完整程序粘贴在答题框中。
19. (程序调试题, 10.0分) 下面的程序有4个错误,请改正。改正的语句后面添加注释:#此行有错误,已经改正
程序功能:随机生成20个1-100的整数,统计并输出能被2整除和能被5整除的数字个数,最后按从小到大排序输出。
python
import random
def count_nums(data, result):
count_2 = 0
count_5 = 0
for num in data:
if num % 2 = 0:
count_2 += 1
else num % 5 == 0:
count_5 += 1
result[0] = count_2
result[1] = count_5
return
nums = {}
for i in range(20):
n = random.randint(1, 100)
nums.append(n)
result = [0]
count_nums(nums, result)
print(f"能被2整除的个数:{result[0]}")
print(f"能被5整除的个数:{result[1]}")
nums.sort()
print("排序后的数据:")
for i in range(0, 20, 5):
print(nums[i:i+5])
请将改正后的完整程序粘贴在答题框中。
四、编程题(共2题,26.0分)
20. (编程题, 14.0分)
文本文件students.csv中存放了学生的姓名、语文、数学、英语成绩,文件内容如图所示:
姓名,语文,数学,英语
张三,85,92,78
李四,90,88,95
王五,78,85,82
赵六,92,90,88
孙七,88,78,90
请编写程序,完成如下功能:
- 从文件中读出数据
- 计算每个学生的总分和平均分
- 找出总分最高的学生
- 将每个学生的姓名、总分、平均分写入新文件result.csv
输出示例:
总分最高的学生:李四,总分:273
result.csv内容示例:
姓名,总分,平均分
张三,255,85.0
李四,273,91.0
王五,245,81.67
赵六,270,90.0
孙七,256,85.33
请将完整程序代码粘贴在答题框中。
21. (编程题, 12.0分)
设计一个表示矩形的Rectangle类,包含以下内容:
- 数据成员:width(宽)、height(高)
- 构造函数:初始化宽和高
- area()方法:计算并返回面积
- perimeter()方法:计算并返回周长
- display()方法:输出矩形的基本信息(宽、高、面积、周长)
编写主程序测试所定义的类。要求:随机生成3个矩形的数据(宽和高范围:1-20),输出界面如下图所示。
矩形1:
宽:15,高:8
面积:120
周长:46
矩形2:
宽:12,高:18
面积:216
周长:60
矩形3:
宽:6,高:10
面积:60
周长:32
请将完整程序代码粘贴在答题框中。
参考答案
一、单选题答案
- C 2. C 3. A 4. A 5. C
- B 7. D 8. C 9. B 10. A
- D 12. A 13. B 14. A 15. A
二、程序填空题答案
16题答案:
- try
- convert
- pixel
- bar
17题答案:
- encoding
- jieba.lcut
- in
- reverse
三、程序调试题参考答案
18题参考答案:
python
import matplotlib.pyplot as plt #此行有错误,已经改正
plt.rcParams['font.sans-serif'] = ['SimHei']
fig = plt.figure(figsize=(10,6))
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
sales = [120, 150, 135, 180]
growth = [0, 25, -10, 33]
plt.subplot(1,2,1)
plt.bar(range(4), sales) #此行有错误,已经改正
plt.title('季度销售额')
plt.xticks(range(4), quarters)
plt.ylabel('销售额(万元)')
plt.subplot(1,2,2)
plt.plot(range(4), growth, 'ro-')
plt.title('销售增长率')
plt.xticks(range(4), quarters) #此行有错误,已经改正
plt.ylabel('增长率(%)')
plt.tight_layout() #此行有错误,已经改正
plt.show()
19题参考答案:
python
import random
def count_nums(data, result):
count_2 = 0
count_5 = 0
for num in data:
if num % 2 == 0: #此行有错误,已经改正
count_2 += 1
elif num % 5 == 0: #此行有错误,已经改正
count_5 += 1
result[0] = count_2
result[1] = count_5
return
nums = [] #此行有错误,已经改正
for i in range(20):
n = random.randint(1, 100)
nums.append(n)
result = [0, 0] #此行有错误,已经改正
count_nums(nums, result)
print(f"能被2整除的个数:{result[0]}")
print(f"能被5整除的个数:{result[1]}")
nums.sort()
print("排序后的数据:")
for i in range(0, 20, 5):
print(nums[i:i+5])
四、编程题参考答案
20题参考答案:
python
fp = open(r"students.csv", "r", encoding='utf-8')
lines = fp.readlines()
fp.close()
students = []
for i in range(1, len(lines)):
data = lines[i].strip().split(',')
name = data[0]
chinese = int(data[1])
math = int(data[2])
english = int(data[3])
total = chinese + math + english
avg = total / 3
students.append([name, total, avg])
max_total = max(students, key=lambda x: x[1])
print(f"总分最高的学生:{max_total[0]},总分:{max_total[1]}")
fp_out = open("result.csv", "w", encoding='utf-8')
fp_out.write("姓名,总分,平均分\n")
for stu in students:
fp_out.write(f"{stu[0]},{stu[1]},{stu[2]:.2f}\n")
fp_out.close()
21题参考答案:
python
import random
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
def display(self):
print(f"宽:{self.width},高:{self.height}")
print(f"面积:{self.area()}")
print(f"周长:{self.perimeter()}")
for i in range(3):
w = random.randint(1, 20)
h = random.randint(1, 20)
rect = Rectangle(w, h)
print(f"矩形{i+1}:")
rect.display()
print()