Unit 6: Introduction to NumPy - Practice Quiz

ECAP776 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which Python library provides the ndarray data type?

Arrays vs lists Easy
A. NumPy
B. Requests
C. Pandas
D. Matplotlib

2 What does multiplying np.array([1, 2, 3]) by 2 produce?

Arrays vs lists Easy
A. [1, 2, 3, 1, 2, 3]
B. [2, 4, 6]
C. [1, 4, 9]
D. [3, 4, 5]

3 What does multiplying the Python list [1, 2] by 2 produce?

Arrays vs lists Easy
A. [2, 4]
B. [2, 2]
C. [1, 2, 1, 2]
D. [1, 4]

4 Which statement about NumPy arrays is generally true?

Arrays vs lists Easy
A. Their elements must all be strings
B. Their size can never be inspected
C. Their elements share one data type
D. Their indexes always begin at one

5 Which attribute gives the dimensions of a NumPy array?

Arrays vs lists Easy
A. append
B. split
C. shape
D. keys

6 Which function creates a NumPy array filled with zeros?

Array creation routines Easy
A. np.empty()
B. np.full()
C. np.zeros()
D. np.ones()

7 What does np.ones(4) create?

Array creation routines Easy
A. An array containing only four
B. An array of four zeros
C. An array of four ones
D. An array from zero to four

8 Which function creates evenly spaced values within a specified interval using a requested number of values?

Array creation routines Easy
A. np.linspace()
B. np.asarray()
C. np.reshape()
D. np.zeros()

9 What values are produced by np.arange(1, 5)?

Array creation routines Easy
A. [0, 1, 2, 3, 4]
B. [0, 1, 2, 3, 4, 5]
C. [1, 2, 3, 4, 5]
D. [1, 2, 3, 4]

10 Which expression creates a array filled with zeros?

Array creation routines Easy
A. np.zeros((2, 3))
B. np.zeros(2, 3)
C. np.arange((2, 3))
D. np.ones((2, 3))

11 Which expression converts the list [2, 4, 6] into a NumPy array?

Arrays from existing data Easy
A. np.array([2, 4, 6])
B. np.arange([2, 4, 6])
C. np.shape([2, 4, 6])
D. np.zeros([2, 4, 6])

12 Which existing Python object can be passed directly to np.array()?

Arrays from existing data Easy
A. An import statement
B. A tuple
C. A module
D. A function definition

13 What is the shape of np.array([[1, 2], [3, 4]])?

Arrays from existing data Easy
A. (2, 1)
B. (4, 2)
C. (2, 2)
D. (1, 4)

14 Which NumPy function can convert existing data to an array while avoiding a copy when possible?

Arrays from existing data Easy
A. np.asarray()
B. np.linspace()
C. np.zeros()
D. np.sort()

15 What is the number of dimensions of np.array([5, 10, 15])?

Arrays from existing data Easy
A. One dimension
B. Three dimensions
C. Zero dimensions
D. Two dimensions

16 Given a = np.array([10, 20, 30]), what is the value of a[0]?

Indexing and slicing Easy
A. 30
B. 20
C. 0
D. 10

17 Given a = np.array([10, 20, 30, 40]), what does a[1:3] return?

Indexing and slicing Easy
A. [10, 20]
B. [20, 30, 40]
C. [20, 30]
D. [10, 20, 30]

18 Which index selects the last element of a one-dimensional NumPy array?

Indexing and slicing Easy
A. 0
B. 1
C. -2
D. -1

19 Given a = np.array([[1, 2], [3, 4]]), what is the value of a[1, 0]?

Indexing and slicing Easy
A. 3
B. 1
C. 4
D. 2

20 Given a = np.array([2, 4, 6, 8]), what does a[::2] return?

Indexing and slicing Easy
A. [2, 6]
B. [6, 8]
C. [2, 4]
D. [4, 8]

21 What is the output of (a + a).tolist(), b + b when a = np.array([1, 2]) and b = [1, 2]?

Arrays vs lists Medium
A. ([2, 4], [2, 4])
B. ([2, 4], [1, 2, 1, 2])
C. ([1, 2, 1, 2], [1, 2, 1, 2])
D. ([1, 2, 1, 2], [2, 4])

22 Given a = np.array([2, 3]) and b = [2, 3], what are the results of a * 2 and b * 2?

Arrays vs lists Medium
A. array([4, 6]) and [2, 3, 2, 3]
B. array([2, 3, 2, 3]) and [4, 6]
C. array([4, 6]) and [4, 6]
D. array([2, 3]) and [2, 3]

23 What does np.array([1, 2]) + [10, 20] produce?

Arrays vs lists Medium
A. array([11, 22])
B. [11, 22]
C. A TypeError
D. array([1, 2, 10, 20])

24 What is the result of np.array([1, 2.5, 3]).dtype on a typical NumPy installation?

Arrays vs lists Medium
A. An integer dtype
B. A Boolean dtype
C. A floating-point dtype
D. An object dtype

25 Which statement correctly compares a NumPy array with a Python list?

Arrays vs lists Medium
A. An array may mix arbitrary types without changing its dtype
B. A list always uses less memory than an equivalent array
C. An array usually stores one dtype, while a list may mix types
D. A list supports vectorized arithmetic, while an array does not

26 What array is created by np.arange(2, 11, 3)?

Array creation routines Medium
A. array([2, 5, 8])
B. array([3, 6, 9])
C. array([2, 5, 8, 11])
D. array([2, 4, 6, 8, 10])

27 Which result is produced by np.linspace(0, 1, 5)?

Array creation routines Medium
A. array([0.0, 0.2, 0.4, 0.6, 0.8])
B. array([0.0, 0.5, 1.0, 1.5, 2.0])
C. array([0.0, 0.25, 0.5, 0.75])
D. 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?

Array creation routines Medium
A. (2, 3) and 5
B. (3, 2) and 6
C. (2, 3) and 6
D. (6,) and 6

29 Which expression creates a integer array in which every element is 7?

Array creation routines Medium
A. np.ones((2, 3), dtype=int)
B. np.zeros((2, 3), dtype=int) + 1
C. np.full((2, 3), 7, dtype=int)
D. np.arange(7, 13).reshape(2, 3)

30 How many elements equal 1 in the array created by np.eye(3, k=1)?

Array creation routines Medium
A. 1
B. 3
C. 6
D. 2

31 What are the shape and number of dimensions of np.array([[1, 2], [3, 4]])?

Arrays from existing data Medium
A. Shape (2,) and 2 dimensions
B. Shape (2, 2) and 2 dimensions
C. Shape (1, 4) and 2 dimensions
D. Shape (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?

Arrays from existing data Medium
A. array([1, 2, 3])
B. array([9, 9, 9])
C. array([1, 9, 3])
D. 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?

Arrays from existing data Medium
A. array([1, 2, 8])
B. array([1, 2, 3])
C. array([8, 2, 3])
D. array([1, 8, 3])

34 What does np.fromiter((x * x for x in range(4)), dtype=int) create?

Arrays from existing data Medium
A. array([0, 1, 4, 9])
B. array([0, 1, 2, 3])
C. array([0, 2, 4, 6])
D. array([1, 4, 9, 16])

35 What does np.fromstring("2 4 6 8", dtype=int, sep=" ") produce?

Arrays from existing data Medium
A. array([2, 4, 6])
B. array([8, 6, 4, 2])
C. array([2, 46, 8])
D. array([2, 4, 6, 8])

36 Let a = np.arange(12).reshape(3, 4). What does a[1:, ::2] return?

Indexing and slicing Medium
A. array([[0, 2], [4, 6]])
B. array([[4, 6], [8, 10]])
C. array([[4, 5], [8, 9]])
D. array([[5, 7], [9, 11]])

37 Given a = np.array([[10, 20, 30], [40, 50, 60]]), what is a[-1, -2]?

Indexing and slicing Medium
A. 40
B. 60
C. 50
D. 20

38 Consider a = np.array([1, 2, 3, 4]), b = a[1:3], and then b[0] = 9. What is a afterward?

Indexing and slicing Medium
A. array([1, 9, 9, 4])
B. array([1, 2, 3, 4])
C. array([9, 2, 3, 4])
D. array([1, 9, 3, 4])

39 What does a[(a > 2) & (a < 6)] return when a = np.array([1, 3, 5, 7])?

Indexing and slicing Medium
A. array([1, 7])
B. array([1, 3, 5])
C. array([3, 5])
D. array([3, 5, 7])

40 Let a = np.arange(9).reshape(3, 3). What does a[[2, 0], [1, 2]] return?

Indexing and slicing Medium
A. array([7, 2])
B. array([[7], [2]])
C. array([7, 0])
D. 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]

Arrays vs lists Hard
A. a is [1, 2, 3, 4]; L is [1, 2, 3, 4]
B. a is [1, 2, 3, 4]; L is [1, 0, 0, 4]
C. a is [1, 0, 0, 4]; L is [1, 2, 3, 4]
D. 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?

Arrays vs lists Hard
A. [1, 2, 3, 1, 2, 3] and [2, 4, 6]
B. [1, 2, 3, 1, 2, 3] and [1, 2, 3, 1, 2, 3]
C. [2, 4, 6] and [1, 2, 3, 1, 2, 3]
D. [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

Arrays vs lists Hard
A. a is [2, 2, 3]; L is [2.9, 2, 3]
B. a is [2.9, 2, 3]; L is [2.9, 2, 3]
C. a is [2.9, 2, 3]; L is [2, 2, 3]
D. 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]

Arrays vs lists Hard
A. b is [1, 2]; M is [1, 2]
B. b is [11, 12]; M is [1, 2, 10]
C. b is [1, 2]; M is [1, 2, 10]
D. 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?

Arrays vs lists Hard
A. [[11, 21, 31], [12, 22, 32]] and [[1], [2], [10, 20, 30]]
B. [[11], [12], [20], [30]] and [[1], [2], [10, 20, 30]]
C. [[11], [22], [30]] and [[11], [22], [30]]
D. [[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")?

Array creation routines Hard
A. [[2, 8, 10], [4, 6, 12]]
B. [[2, 6, 10], [4, 8, 12]]
C. [[2, 4, 6], [8, 10, 12]]
D. [[2, 4, 10], [6, 8, 12]]

47 What does np.linspace(2, 10, num=4, endpoint=False, retstep=True) return?

Array creation routines Hard
A. The pair (array([2., 3.5, 5., 6.5]), 1.5)
B. The pair (array([2., 4., 6., 10.]), 2.0)
C. The pair (array([2., 4.6667, 7.3333, 10.]), 2.6667)
D. The pair (array([2., 4., 6., 8.]), 2.0)

48 Which array is produced by np.eye(3, 4, k=1, dtype=int)?

Array creation routines Hard
A. [[0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
B. [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]
C. [[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0]]
D. [[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)?

Array creation routines Hard
A. [4, 4, 4] with dtype int16
B. [3.8, 3.8, 3.8] with dtype float64
C. [3.8, 3.8, 3.8] with dtype int16
D. [3, 3, 3] with dtype int16

50 What is produced by np.fromfunction(lambda i, j: 10 * i + j, (2, 3), dtype=int)?

Array creation routines Hard
A. [[0, 10, 20], [1, 11, 21]]
B. [[0, 1, 2], [10, 11, 12]]
C. [[0, 1], [10, 11], [20, 21]]
D. [[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

Arrays from existing data Hard
A. a is [0, 1, 2, 3]; c is [0, 8, 2, 3]
B. a is [9, 8, 2, 3]; c is [9, 8, 2, 3]
C. a is [9, 1, 2, 3]; c is [9, 8, 2, 3]
D. 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

Arrays from existing data Hard
A. [3, 2]
B. [1, 2]
C. [3, 0, 2, 0]
D. [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)

Arrays from existing data Hard
A. [0, 1, 4] and [4, 9, 16]
B. [0, 1, 4] and [0, 1, 4, 9, 16]
C. [0, 1, 4] and [9, 16]
D. [0, 1, 4, 9, 16] and []

54 What are the inferred dtype and values of np.array([1, 2.5, True, 4 + 0j])?

Arrays from existing data Hard
A. int64 and [1, 2, 1, 4]
B. object and [1, 2.5, True, (4+0j)]
C. complex128 and [(1+0j), (2.5+0j), (1+0j), (4+0j)]
D. 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

Arrays from existing data Hard
A. x is [[9, 2], [3, 8]]; b is [[9, 2], [3, 8]]
B. x is [[9, 2], [3, 4]]; b is [[9, 2], [3, 8]]
C. x is [[1, 2], [3, 4]]; b is [[1, 2], [3, 8]]
D. 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?

Indexing and slicing Hard
A. [[1, 3], [9, 11]]
B. [1, 11]
C. [1, 3, 9, 11]
D. [[4, 8], [6, 10]]

57 Let a = np.arange(24).reshape(2, 3, 4). What is a[:, [2, 0], 1:3]?

Indexing and slicing Hard
A. [[[1, 2], [9, 10]], [[13, 14], [21, 22]]]
B. [[[9, 1], [10, 2]], [[21, 13], [22, 14]]]
C. [[[9, 10], [1, 2]], [[21, 22], [13, 14]]]
D. [[[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

Indexing and slicing Hard
A. a is [[0, 1, 2], [3, 4, 5]]; b is [99, 2, 4]
B. a is [[99, 1, 2], [3, 4, 5]]; b is [99, 2, 4]
C. a is [[0, 1, 2], [3, 4, 5]]; b is [[99, 2, 4]]
D. 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

Indexing and slicing Hard
A. [1, 1, 0]
B. [1, 0, 1]
C. [2, 1, 0]
D. [2, 0, 1]

60 What is the final value of a?

a = np.arange(5)

a[[1, 3]][0] = 99

Indexing and slicing Hard
A. [0, 99, 2, 3, 4]
B. [0, 1, 2, 99, 4]
C. [0, 99, 2, 99, 4]
D. [0, 1, 2, 3, 4]