Unit 6: Introduction to NumPy - Practice Quiz
1
Which Python library provides the ndarray data type?
2
What does multiplying np.array([1, 2, 3]) by 2 produce?
[1, 2, 3, 1, 2, 3]
[2, 4, 6]
[1, 4, 9]
[3, 4, 5]
3
What does multiplying the Python list [1, 2] by 2 produce?
[2, 4]
[2, 2]
[1, 2, 1, 2]
[1, 4]
4 Which statement about NumPy arrays is generally true?
5 Which attribute gives the dimensions of a NumPy array?
append
split
shape
keys
6 Which function creates a NumPy array filled with zeros?
np.empty()
np.full()
np.zeros()
np.ones()
7
What does np.ones(4) create?
8 Which function creates evenly spaced values within a specified interval using a requested number of values?
np.linspace()
np.asarray()
np.reshape()
np.zeros()
9
What values are produced by np.arange(1, 5)?
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
10 Which expression creates a array filled with zeros?
np.zeros((2, 3))
np.zeros(2, 3)
np.arange((2, 3))
np.ones((2, 3))
11
Which expression converts the list [2, 4, 6] into a NumPy array?
np.array([2, 4, 6])
np.arange([2, 4, 6])
np.shape([2, 4, 6])
np.zeros([2, 4, 6])
12
Which existing Python object can be passed directly to np.array()?
13
What is the shape of np.array([[1, 2], [3, 4]])?
(2, 1)
(4, 2)
(2, 2)
(1, 4)
14 Which NumPy function can convert existing data to an array while avoiding a copy when possible?
np.asarray()
np.linspace()
np.zeros()
np.sort()
15
What is the number of dimensions of np.array([5, 10, 15])?
16
Given a = np.array([10, 20, 30]), what is the value of a[0]?
30
20
0
10
17
Given a = np.array([10, 20, 30, 40]), what does a[1:3] return?
[10, 20]
[20, 30, 40]
[20, 30]
[10, 20, 30]
18 Which index selects the last element of a one-dimensional NumPy array?
0
1
-2
-1
19
Given a = np.array([[1, 2], [3, 4]]), what is the value of a[1, 0]?
3
1
4
2
20
Given a = np.array([2, 4, 6, 8]), what does a[::2] return?
[2, 6]
[6, 8]
[2, 4]
[4, 8]
21
What is the output of (a + a).tolist(), b + b when a = np.array([1, 2]) and b = [1, 2]?
22
Given a = np.array([2, 3]) and b = [2, 3], what are the results of a * 2 and b * 2?
array([4, 6]) and [2, 3, 2, 3]
array([2, 3, 2, 3]) and [4, 6]
array([4, 6]) and [4, 6]
array([2, 3]) and [2, 3]
23
What does np.array([1, 2]) + [10, 20] produce?
array([11, 22])
[11, 22]
TypeError
array([1, 2, 10, 20])
24
What is the result of np.array([1, 2.5, 3]).dtype on a typical NumPy installation?
25 Which statement correctly compares a NumPy array with a Python list?
26
What array is created by np.arange(2, 11, 3)?
array([2, 5, 8])
array([3, 6, 9])
array([2, 5, 8, 11])
array([2, 4, 6, 8, 10])
27
Which result is produced by np.linspace(0, 1, 5)?
array([0.0, 0.2, 0.4, 0.6, 0.8])
array([0.0, 0.5, 1.0, 1.5, 2.0])
array([0.0, 0.25, 0.5, 0.75])
array([0.0, 0.25, 0.5, 0.75, 1.0])
28
After a = np.zeros((2, 3)), what are a.shape and a.size?
(2, 3) and 5
(3, 2) and 6
(2, 3) and 6
(6,) and 6
29
Which expression creates a integer array in which every element is 7?
np.ones((2, 3), dtype=int)
np.zeros((2, 3), dtype=int) + 1
np.full((2, 3), 7, dtype=int)
np.arange(7, 13).reshape(2, 3)
30
How many elements equal 1 in the array created by np.eye(3, k=1)?
1
3
6
2
31
What are the shape and number of dimensions of np.array([[1, 2], [3, 4]])?
(2,) and 2 dimensions
(2, 2) and 2 dimensions
(1, 4) and 2 dimensions
(4,) and 1 dimension
32
Consider a = np.array([1, 2, 3]), b = np.asarray(a), and then b[0] = 9. What is a afterward?
array([1, 2, 3])
array([9, 9, 9])
array([1, 9, 3])
array([9, 2, 3])
33
Consider a = np.array([1, 2, 3]), b = np.array(a, copy=True), and then b[1] = 8. What is a afterward?
array([1, 2, 8])
array([1, 2, 3])
array([8, 2, 3])
array([1, 8, 3])
34
What does np.fromiter((x * x for x in range(4)), dtype=int) create?
array([0, 1, 4, 9])
array([0, 1, 2, 3])
array([0, 2, 4, 6])
array([1, 4, 9, 16])
35
What does np.fromstring("2 4 6 8", dtype=int, sep=" ") produce?
array([2, 4, 6])
array([8, 6, 4, 2])
array([2, 46, 8])
array([2, 4, 6, 8])
36
Let a = np.arange(12).reshape(3, 4). What does a[1:, ::2] return?
array([[0, 2], [4, 6]])
array([[4, 6], [8, 10]])
array([[4, 5], [8, 9]])
array([[5, 7], [9, 11]])
37
Given a = np.array([[10, 20, 30], [40, 50, 60]]), what is a[-1, -2]?
40
60
50
20
38
Consider a = np.array([1, 2, 3, 4]), b = a[1:3], and then b[0] = 9. What is a afterward?
array([1, 9, 9, 4])
array([1, 2, 3, 4])
array([9, 2, 3, 4])
array([1, 9, 3, 4])
39
What does a[(a > 2) & (a < 6)] return when a = np.array([1, 3, 5, 7])?
array([1, 7])
array([1, 3, 5])
array([3, 5])
array([3, 5, 7])
40
Let a = np.arange(9).reshape(3, 3). What does a[[2, 0], [1, 2]] return?
array([7, 2])
array([[7], [2]])
array([7, 0])
array([6, 2])
41
What are the final values of a and L?
a = np.array([1, 2, 3, 4])
v = a[1:3]
L = [1, 2, 3, 4]
s = L[1:3]
v[:] = 0
s[:] = [0, 0]
a is [1, 2, 3, 4]; L is [1, 2, 3, 4]
a is [1, 2, 3, 4]; L is [1, 0, 0, 4]
a is [1, 0, 0, 4]; L is [1, 2, 3, 4]
a is [1, 0, 0, 4]; L is [1, 0, 0, 4]
42
Given a = np.array([1, 2, 3]) and L = [1, 2, 3], what are the values of a * 2 and L * 2?
[1, 2, 3, 1, 2, 3] and [2, 4, 6]
[1, 2, 3, 1, 2, 3] and [1, 2, 3, 1, 2, 3]
[2, 4, 6] and [1, 2, 3, 1, 2, 3]
[2, 4, 6] and [2, 4, 6]
43
What are the final contents of a and L?
a = np.array([1, 2, 3], dtype=np.int64)
L = [1, 2, 3]
a[0] = 2.9
L[0] = 2.9
a is [2, 2, 3]; L is [2.9, 2, 3]
a is [2.9, 2, 3]; L is [2.9, 2, 3]
a is [2.9, 2, 3]; L is [2, 2, 3]
a is [3, 2, 3]; L is [2.9, 2, 3]
44
What do b and M contain after this code?
a = np.array([1, 2])
b = a
a += 10
L = [1, 2]
M = L
L = L + [10]
b is [1, 2]; M is [1, 2]
b is [11, 12]; M is [1, 2, 10]
b is [1, 2]; M is [1, 2, 10]
b is [11, 12]; M is [1, 2]
45
Consider a = np.array([[1], [2]]), b = np.array([10, 20, 30]), L = [[1], [2]], and M = [[10, 20, 30]]. Which pair gives a + b and L + M?
[[11, 21, 31], [12, 22, 32]] and [[1], [2], [10, 20, 30]]
[[11], [12], [20], [30]] and [[1], [2], [10, 20, 30]]
[[11], [22], [30]] and [[11], [22], [30]]
[[11, 21, 31], [12, 22, 32]] and [[11, 21, 31], [12, 22, 32]]
46
What array is created by np.arange(2, 14, 2).reshape(2, 3, order="F")?
[[2, 8, 10], [4, 6, 12]]
[[2, 6, 10], [4, 8, 12]]
[[2, 4, 6], [8, 10, 12]]
[[2, 4, 10], [6, 8, 12]]
47
What does np.linspace(2, 10, num=4, endpoint=False, retstep=True) return?
(array([2., 3.5, 5., 6.5]), 1.5)
(array([2., 4., 6., 10.]), 2.0)
(array([2., 4.6667, 7.3333, 10.]), 2.6667)
(array([2., 4., 6., 8.]), 2.0)
48
Which array is produced by np.eye(3, 4, k=1, dtype=int)?
[[0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]
[[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0]]
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
49
What are the value and dtype of np.full_like(np.array([1, 2, 3], dtype=np.int16), 3.8)?
[4, 4, 4] with dtype int16
[3.8, 3.8, 3.8] with dtype float64
[3.8, 3.8, 3.8] with dtype int16
[3, 3, 3] with dtype int16
50
What is produced by np.fromfunction(lambda i, j: 10 * i + j, (2, 3), dtype=int)?
[[0, 10, 20], [1, 11, 21]]
[[0, 1, 2], [10, 11, 12]]
[[0, 1], [10, 11], [20, 21]]
[[0, 10, 20], [10, 20, 30]]
51
What are the final contents of a and c?
a = np.arange(4)
b = np.asarray(a)
c = np.array(a)
b[0] = 9
c[1] = 8
a is [0, 1, 2, 3]; c is [0, 8, 2, 3]
a is [9, 8, 2, 3]; c is [9, 8, 2, 3]
a is [9, 1, 2, 3]; c is [9, 8, 2, 3]
a is [9, 1, 2, 3]; c is [0, 8, 2, 3]
52
What does a.tolist() return after this code on any machine?
buf = bytearray([1, 0, 2, 0])
a = np.frombuffer(buf, dtype="<u2")
buf[0] = 3
[3, 2]
[1, 2]
[3, 0, 2, 0]
[768, 512]
53
What are a.tolist() and b.tolist()?
g = (i * i for i in range(5))
a = np.fromiter(g, dtype=int, count=3)
b = np.fromiter(g, dtype=int)
[0, 1, 4] and [4, 9, 16]
[0, 1, 4] and [0, 1, 4, 9, 16]
[0, 1, 4] and [9, 16]
[0, 1, 4, 9, 16] and []
54
What are the inferred dtype and values of np.array([1, 2.5, True, 4 + 0j])?
int64 and [1, 2, 1, 4]
object and [1, 2.5, True, (4+0j)]
complex128 and [(1+0j), (2.5+0j), (1+0j), (4+0j)]
float64 and [1.0, 2.5, 1.0, 4.0]
55
What are the final contents of x and b?
x = np.array([[1, 2], [3, 4]], order="F")
a = np.asarray(x)
b = np.asarray(x, order="C")
a[0, 0] = 9
b[1, 1] = 8
x is [[9, 2], [3, 8]]; b is [[9, 2], [3, 8]]
x is [[9, 2], [3, 4]]; b is [[9, 2], [3, 8]]
x is [[1, 2], [3, 4]]; b is [[1, 2], [3, 8]]
x is [[9, 2], [3, 4]]; b is [[1, 2], [3, 8]]
56
For a = np.arange(12).reshape(3, 4), what does a[[0, 2], [1, 3]] return?
[[1, 3], [9, 11]]
[1, 11]
[1, 3, 9, 11]
[[4, 8], [6, 10]]
57
Let a = np.arange(24).reshape(2, 3, 4). What is a[:, [2, 0], 1:3]?
[[[1, 2], [9, 10]], [[13, 14], [21, 22]]]
[[[9, 1], [10, 2]], [[21, 13], [22, 14]]]
[[[9, 10], [1, 2]], [[21, 22], [13, 14]]]
[[[8, 9], [0, 1]], [[20, 21], [12, 13]]]
58
What are the final values of a and b?
a = np.arange(6).reshape(2, 3)
b = a[a % 2 == 0]
b[0] = 99
a is [[0, 1, 2], [3, 4, 5]]; b is [99, 2, 4]
a is [[99, 1, 2], [3, 4, 5]]; b is [99, 2, 4]
a is [[0, 1, 2], [3, 4, 5]]; b is [[99, 2, 4]]
a is [[99, 1, 2], [3, 4, 5]]; b is [0, 2, 4]
59
What is the final value of a?
a = np.zeros(3, dtype=int)
a[[0, 0, 1]] += 1
[1, 1, 0]
[1, 0, 1]
[2, 1, 0]
[2, 0, 1]
60
What is the final value of a?
a = np.arange(5)
a[[1, 3]][0] = 99
[0, 99, 2, 3, 4]
[0, 1, 2, 99, 4]
[0, 99, 2, 99, 4]
[0, 1, 2, 3, 4]
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →