Python NumPy Arrays Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering NumPy array creation, shapes, indexing, slicing, broadcasting, and aggregation.

35 Questions
~70 minutes
1

Question 1

Why are NumPy arrays generally preferred over standard Python lists for scientific computing?

A
They can hold elements of different data types simultaneously.
B
They are more memory efficient and offer faster numerical operations.
C
They have built-in support for SQL queries.
D
They are automatically resized like dynamic lists.
2

Question 2

You need to initialize a neural network weight matrix of size 3x3 with all zeros. Which command achieves this?

javascript

import numpy as np
# Create 3x3 zero matrix
                
A
np.zeros(3, 3)
B
np.array([0] * 9).reshape(3, 3)
C
np.zeros((3, 3))
D
np.empty((3, 3))
3

Question 3

What is the key difference between `np.arange()` and `np.linspace()`?

A
arange excludes the stop value, while linspace includes it by default.
B
arange generates floating point numbers only.
C
linspace uses a step size, while arange uses the number of samples.
D
There is no difference; they are aliases.
4

Question 4

Consider the following code snippet creating an array. What will be the resulting data type?

javascript

import numpy as np
arr = np.array([1, 2, 3.5, 4])
print(arr.dtype)
                
A
int64
B
float64
C
object
D
mixed
5

Question 5

How would you create a 4x4 Identity matrix (1s on the diagonal, 0s elsewhere)?

A
np.identity(4)
B
np.eye(4)
C
np.diag(4)
D
Both A and B are correct.
6

Question 6

What happens if you try to create a NumPy array with a mix of strings and numbers?

javascript

arr = np.array([1, "2", 3])
                
A
It raises a TypeError.
B
It creates an array of integers, parsing the string.
C
It converts all elements to strings (U type).
D
It creates a list instead.
7

Question 7

Which attribute would you check to determine the dimensions (rows, columns, etc.) of an array?

A
.size
B
.ndim
C
.shape
D
.length
8

Question 8

You have an array with 12 elements. You want to reshape it into a matrix with 3 rows and an automatically calculated number of columns. How do you do this?

javascript

arr = np.arange(12)
# Reshape command
                
A
arr.reshape(3, 0)
B
arr.reshape(3, -1)
C
arr.reshape(3, None)
D
arr.reshape(3, auto)
9

Question 9

What is the difference between `arr.flatten()` and `arr.ravel()`?

A
flatten() returns a copy, while ravel() returns a view if possible.
B
ravel() returns a copy, while flatten() returns a view.
C
flatten() only works on 2D arrays.
D
There is no difference.
10

Question 10

Given a 3D array of shape `(2, 3, 4)`, what does `.ndim` return?

A
24
B
3
C
(2, 3, 4)
D
9
11

Question 11

How do you transpose a 2D matrix `arr` (swap rows and columns)?

A
arr.swap()
B
arr.T
C
arr.transpose(0, 1)
D
arr.flip()
12

Question 12

If you reshape an array `arr` of shape `(4, 4)` to `(2, 8)`, does the underlying data change?

A
Yes, the data is reordered in memory.
B
No, only the metadata (strides and shape) defining how to interpret the data changes.
C
Yes, half the data is lost.
D
It depends on the operating system.
13

Question 13

Consider the array `M`. How do you access the element in the second row and third column?

javascript

M = np.array([[1, 2, 3], 
              [4, 5, 6], 
              [7, 8, 9]])
                
A
M[1][2]
B
M[1, 2]
C
M[2, 3]
D
Both A and B work, but B is preferred.
14

Question 14

You want to set all negative values in an array `data` to 0. Which operation uses Boolean Masking correctly?

javascript

data = np.array([-1, 2, -3, 4, 5])
                
A
data[data < 0] = 0
B
if data < 0: data = 0
C
data.replace(data < 0, 0)
D
data[0] = 0
15

Question 15

What is the result of this 'Fancy Indexing' operation?

javascript

arr = np.array([10, 20, 30, 40, 50])
indices = [0, 3, 4]
print(arr[indices])
                
A
[10, 40, 50]
B
[10, 30, 40]
C
IndexError
D
(10, 40, 50)
16

Question 16

How does NumPy handle indexing with a tuple of integers vs a list of integers?

A
Tuple is treated as coordinates for dimensions; List is treated as fancy indexing for the first dimension.
B
They are identical.
C
Tuple creates a copy, List creates a view.
D
List is for slicing, Tuple is for values.
17

Question 17

What happens if you assign a float value to an integer array element?

javascript

arr = np.array([1, 2, 3], dtype='int32')
arr[0] = 10.9
print(arr)
                
A
[10.9, 2, 3]
B
[10, 2, 3]
C
[11, 2, 3]
D
TypeError
18

Question 18

You have a 2D array. You want to select the first and third rows, and for those rows, only the second column. Which syntax is correct?

javascript

arr = np.array([[1, 2], [3, 4], [5, 6]])
# Target: [2, 6]
                
A
arr[[0, 2], 1]
B
arr[0, 2, 1]
C
arr[[0, 2]][1]
D
arr[0:3:2, 1]
19

Question 19

What does the slice `arr[::-1]` do?

A
Returns the last element.
B
Reverses the array.
C
Returns the array sorted descending.
D
Deletes the last element.
20

Question 20

Given a 2D array `grid`, how do you select the entire second column?

javascript

grid = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])
                
A
grid[1]
B
grid[:, 1]
C
grid[1, :]
D
grid[][1]
21

Question 21

What is the key difference between slicing and fancy indexing regarding memory?

A
Slicing returns a view; Fancy Indexing returns a copy.
B
Slicing returns a copy; Fancy Indexing returns a view.
C
Both return views.
D
Both return copies.
22

Question 22

What happens when you modify a slice of an array?

javascript

arr = np.array([0, 1, 2, 3, 4])
sub = arr[0:2]
sub[0] = 99
print(arr[0])
                
A
0
B
99
C
1
D
Error
23

Question 23

How do you select the last 3 elements of an array?

A
arr[3:]
B
arr[:-3]
C
arr[-3:]
D
arr[last-3]
24

Question 24

Consider the array `img` of shape `(100, 100, 3)`. What does `img[:50, :50, :]` select?

A
The top-left 50x50 quadrant of the image, including all color channels.
B
The first 50 pixels of the first row only.
C
The center crop of the image.
D
The red channel only.
25

Question 25

What is 'Broadcasting' in NumPy?

A
Sending array data over a network.
B
A mechanism to perform arithmetic on arrays of different shapes.
C
Printing array contents to the console.
D
Expanding an array to fill memory.
26

Question 26

Which of the following shapes are compatible for broadcasting?

A
(3, 2) and (3,)
B
(3, 2) and (2, 3)
C
(4, 3) and (3,)
D
(5, 5) and (2, 2)
27

Question 27

What is the result of adding a scalar to an array?

javascript

arr = np.array([1, 2, 3])
print(arr + 10)
                
A
[1, 2, 3, 10]
B
[11, 12, 13]
C
TypeError
D
[10, 20, 30]
28

Question 28

Predict the shape of the result: `A` is `(3, 1)` and `B` is `(1, 3)`.

javascript

A = np.zeros((3, 1))
B = np.zeros((1, 3))
C = A + B
                
A
(3, 1)
B
(1, 3)
C
(3, 3)
D
Error
29

Question 29

Why is broadcasting efficient?

A
It compresses the data.
B
It avoids making unnecessary memory copies of the data.
C
It uses GPU acceleration automatically.
D
It converts arrays to lists.
30

Question 30

You want to subtract the mean of each column from a 2D array `X` of shape `(100, 5)`. The mean vector `mu` has shape `(5,)`. Does `X - mu` work?

A
Yes, because the last dimensions match (5 == 5).
B
No, you must reshape mu to (1, 5).
C
No, shapes must be identical.
D
Yes, but it subtracts row-wise.
31

Question 31

What does `np.sum(arr, axis=0)` do on a 2D array?

A
Sums all elements.
B
Sums elements along the rows (collapses rows), resulting in column sums.
C
Sums elements along the columns (collapses columns), resulting in row sums.
D
Returns the cumulative sum.
32

Question 32

How do you find the *index* of the maximum value in an array?

javascript

arr = np.array([10, 5, 99, 2])
# Wanted: 2 (index of 99)
                
A
np.max(arr)
B
np.argmax(arr)
C
arr.index(99)
D
np.where(arr == max)
33

Question 33

Why is `np.sum()` faster than Python's built-in `sum()` on NumPy arrays?

A
It isn't; they are the same.
B
np.sum executes a compiled C loop, avoiding Python interpreter overhead.
C
np.sum uses multiple threads by default.
D
Python's sum() converts the array to a string first.
34

Question 34

What is the result of `arr.mean()` if `arr` contains integers?

javascript

arr = np.array([1, 2])
print(arr.mean())
                
A
1 (integer division)
B
1.5 (float)
C
2
D
Error
35

Question 35

You want to calculate the cumulative sum of an array. Which function should you use?

javascript

arr = np.array([1, 2, 3])
# Result: [1, 3, 6]
                
A
np.sum(arr)
B
np.cumsum(arr)
C
np.add.accumulate(arr)
D
Both B and C

QUIZZES IN Python