首页 › 题库 › Python编程考级 › 2 数组

Python编程六级 题库

共 98 道题 ,第 2 / 5 页 · 按题型筛选,点「查看答案」核对解析
21 单选题 适中 Python编程考级 ·Python编程六级 · 2.1 概念与特性

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

import numpy as np
x = np.array([1, 2, 3, 4, 5])
print(x[2:4])
A

[3 4]

B

[2 3]

C

[2 3 4]

D

[2 4]

答案与解析
正确答案:A
这道题暂未补解析。
题目详情
22 单选题 适中 Python编程考级 ·Python编程六级 · 2.1 概念与特性

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

import numpy as np
arr = np.array([[1, 2],
                      [3, 4]])
print(arr.sum())
A

3

B

4

C

6

D

10

答案与解析
正确答案:D
这道题暂未补解析。
题目详情
23 单选题 适中 Python编程考级 ·Python编程六级 · 2.2 基本操作

假设你正在开发一个电子商务网站,你需要存储用户订单信息。需要创建一个名为 orders 的表,下面哪个 SQL 语句最合适?(   )

A
CREATE TABLE orders (id INTEGER PRIMARY KEY, user_id INTEGER, product_name TEXT, quantity INTEGER, price REAL)
B
CREATE TABLE orders (user_id INTEGER, product_name TEXT, quantity INTEGER, price REAL)
C
CREATE TABLE orders (id INTEGER PRIMARY KEY, user_id INTEGER, product_name TEXT)
D
CREATE TABLE orders (id INTEGER, user_id INTEGER, product_name TEXT, quantity INTEGER, price REAL)
答案与解析
正确答案:A

选项A包含了所有必要的列(ID、用户ID、产品名称、数量和价格),并将ID列设置为主键。

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

给定以下 Python 代码,连接到一个 SQLite 数据库并查询表 students。请问,查询结果中包含多少个学生?(   )

import sqlite3
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE students (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    score INTEGER NOT NULL
);
""")
cursor.execute("INSERT INTO students (name, score) VALUES ('小明', 90)")
cursor.execute("INSERT INTO students (name, score) VALUES ('小芳', 85)")
cursor.execute("INSERT INTO students (name, score) VALUES ('轩轩', 92)")
cursor.execute("INSERT INTO students (name, score) VALUES ('乐乐', 88)")
conn.commit()
cursor.execute("SELECT * FROM students WHERE score >= 90")
result = cursor.fetchall()
conn.close()
print(result)
A
0
B
1
C
2
D
3
答案与解析
正确答案:C

该代码首先创建了一个 SQLite 数据库并创建了一个名为 students 的表。表中有4条学生记录,分别为小明、小芳、轩轩 和 乐乐。然后查询分数大于等于90的学生,结果为 小明 和 轩轩,共2个学生。

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

利用Python列表创建一个二维数组,不正确的方法是?(   )

A
a=[[1,1,3],[2,6,7],[4,7,1]]
B
b=[[0 for i in range(3)] for j in range(2)]
C
c=[0]*4*4
D
d=[[0]*3,[9]*3,[4]*3]
答案与解析
正确答案:C

用选项C方法创建了一维数组。

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

有如下Python程序段:

a=[1,2,3,5,6,8,9,11,15,0] #0表示该位置未存储元素
num=int(input( "输入需要插入的数据:"))
for i in range(len(a)):
    if a[i]>num:
        for j in range(len(a)-1,i-1,-1):
            a[j]=a[j-1]
        a[i]=num
        break
    else:
        a[-1]=num
print(a)

执行程序后,输入数字9,则位置下标发生改变的数据个数?(   )

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

当数组arr中的元素arr[i]大于新数据num时,则将位置i及其之后的数据都向后移动,所以当输入的数字为9时,11大于num,则11和15的位置下标将发生改变。

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

创建一个3*3的数组,下面代码错误的是?(   )

A
import numpy as npnp.arange(0,9).reshape(3,3)
B
import numpy as npnp.random.random([3,3,3])
C
import numpy as npnp.eye(3)
D
import numpy as npnp.mat('1,2,3;4,5,6;7,8,9')
答案与解析
正确答案:B

np.random.random([3,3,3]) 生成的是个三维数组,每个数组都是3行3列。

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

import numpy as np 

np.arange(16).reshape(4,4),求点(3,2)的值是?(   )

A
14
B
9
C
10
D

6

答案与解析
正确答案:A

(3,2)求的是索引位3的行,索引位2列的数,索引是从0开始,所求的值应该是位于数组的第4行第3列,故为14。

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

要生成一个 3*4 的数组,并计算数组中偶数值之和,代码是?()

A
import numpy as nparr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])np.sum(arr[arr%2==0])
B
import numpy as nparr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])np.sum(arr[:,arr%2==0])
C
import numpy as nparr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])np.sum(arr[np.get(arr%2==0)])
D
import numpy as nparr=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])np.sum(arr[arr.even()])
答案与解析
正确答案:A

使用 np.array()生成数组,np.sum()计算和,arr%2==0 提取偶数值,所以选择 A 选项。

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

题目详情
31 单选题 适中 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 条记录。

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

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

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

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

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

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

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

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

题目详情
36 单选题 适中 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' 模式打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。

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

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

题目详情
38 单选题 适中 Python编程考级 ·Python编程六级 · 2.1 概念与特性
已知程序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()
A
1
B
2
C
3
D
4
答案与解析
正确答案:D

绘图的X坐标为索引号,Y值表示高度。

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

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

a=[[1,2,3],[4,5,6],[7,8,9]]
print(a[1][2])
A

2

B

4

C
5
D
6
答案与解析
正确答案:D

二维数组左上角坐标为[0,0]。

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

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

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

red

B

blue

C

green

D

black

答案与解析
正确答案:C

柱形图颜色将用列表中的颜色重复。

题目详情