Python NumPy MCQ
1). What is the output of the following code?
import numpy as np
x = np.array([[5, 3],
[7, 4]])
print("Sum of each row : ", np.sum(x, axis=1))
a) [8, 11]
b) [12, 17]
c) [6, 11]
d) [9, 14]
Correct answer is: a) [8, 11]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[5, 3], [7, 4]]. The `np.sum()` function is then used to calculate the sum of each row in the array `x` along `axis=1`.
2). What is the output of the following code?
import numpy as np
x = np.array([[5, 3],
[7, 4]])
print("Sum of each column: ", np.sum(x, axis=0))
a) [12, 7]
b) [8, 9]
c) [19, 7]
d) [5, 3, 7, 4]
Correct answer is: a) [12, 7]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[5, 3], [7, 4]]. The `np.sum()` function is then used to calculate the sum of each column in the array `x` along `axis=0`.
3). What is the output of the following code?
import numpy as np
x = np.array([5, 3])
y = np.array([7, 4])
print("Inner-dot product: ", np.dot(x, y))
a) 31
b) 29
c) 39
d) 33
Correct answer is: a) 47
Explanation: The given code imports the NumPy library and creates two 1-dimensional NumPy arrays `x` and `y` with the elements [5, 3] and [7, 4] respectively. The `np.dot()` function is then used to calculate the inner-dot product of the arrays `x` and `y`.
4). What is the output of the following code?
import numpy as np
x = np.array([[5, 3],
[7, 4]])
print("Original matrix: \n", x)
vector = np.array([2, 5])
print("Vector to be added: \n", vector)
for i in range(len(x)):
x[i, :] = x[i, :] + vector
print("Matrix after adding a vector: \n", x)
a)
Original matrix:
[[5 3]
[7 4]]
Vector to be added:
[2 5]
Matrix after adding a vector:
[[7 8]
[9 9]]
b)
Original matrix:
[[5 3]
[7 4]]
Vector to be added:
[2 5]
Matrix after adding a vector:
[[ 7 8]
[ 9 10]]
c)
Original matrix:
[[5 3]
[7 4]]
Vector to be added:
[2 5]
Matrix after adding a vector:
[[ 7 5]
[ 9 4]]
d)
Original matrix:
[[5 3]
[7 4]]
Vector to be added:
[2 5]
Matrix after adding a vector:
[[ 7 5]
[ 9 9]]
Correct answer is: a)
Original matrix:
[[5 3]
[7 4]]
Vector to be added:
[2 5]
Matrix after adding a vector:
[[7 8]
[9 9]]
Explanation: The given code initializes a 2-dimensional NumPy array `x` with the elements [[5, 3], [7, 4]] and a 1-dimensional NumPy array `vector` with the elements [2, 5]. The code then adds the `vector` to each row of the `x` array in a loop.
5). What is the purpose of the following code?
import numpy as np
x = np.array([[3, 0],
[6, 4]])
print("Matrix: \n", x)
y = x.tolist()
print("List: ", y)
a) The code calculates the sum of all elements in the NumPy array `x`.
b) The code converts the NumPy array `x` into a list `y`.
c) The code reshapes the NumPy array `x` into a 1-dimensional array.
d) The code transposes the rows and columns of the NumPy array `x`.
Correct answer is: b) The code converts the NumPy array `x` into a list `y`.
Explanation: The given code snippet demonstrates the use of NumPy and Python’s built-in `tolist()` function.
6). What is the output of the following code?
import numpy as np
x = np.array([[1, 5, np.nan],
[8, np.nan, 9]])
print(np.isnan(x))
a) [[False, False, True], [False, True, False]]
b) [[True, True, False], [False, False, True]]
c) [[False, True, False], [True, False, False]]
d) [[False, False, False], [False, False, False]]
Correct answer is: a) [[False, False, True], [False, True, False]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with some elements being NaN (Not a Number). The `np.isnan()` function is then used to check if the elements in the array are NaN or not. The output of `np.isnan(x)` will be a Boolean array with the same shape as `x`, where each element is `True` if the corresponding element in `x` is NaN, and `False` otherwise.
7). What is the output of the following code?
import numpy as np
x = np.array([[5, 6],
[7, 8],
[9, 10]])
print(x.transpose())
a) [[5, 6],
[7, 8],
[9, 10]]
b) [[5, 7, 9],
[6, 8, 10]]
c) [[5, 7, 9],
[6, 8, 10],
[9, 10]]
d) [[5, 6],
[6, 7],
[7, 8],
[8, 9],
[9, 10]]
Correct answer is: b) [[5, 7, 9],
[6, 8, 10]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[5, 6], [7, 8], [9, 10]]. The `x.transpose()` function is then used to calculate the transpose of the array `x`. The transpose of a 2-dimensional array is obtained by flipping the rows and columns. So, the resulting array will have the elements of `x` interchanged between rows and columns.
8). What is the output of the following code?
import numpy as np
x = np.array([[6, 5],
[10, 9],
[8, 7]])
print("Sorting matrix by row: ")
print(np.sort(x))
a) [[5 6]
[9 10]
[7 8]]
b) [[6 5]
[10 9]
[8 7]]
c) [[5 6]
[7 8]
[9 10]]
d) [[10 9]
[8 7]
[6 5]]
Correct answer is: c) [[5 6]
[7 8]
[9 10]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. The `np.sort()` function is then used to sort the elements of `x` along each row.
9). What is the output of the following code?
import numpy as np
x = np.array([[6, 5],
[10, 9],
[8, 7]])
print("Sorting matrix by column: ")
print(np.sort(x, axis=0))
a)
[[ 5 5]
[ 8 7]
[10 9]]
b)
[[ 5 6]
[ 7 8]
[ 9 10]]
c)
[[ 6 5]
[ 9 10]
[ 7 8]]
d)
[[ 7 5]
[ 8 6]
[10 9]]
Correct answer is: b)
[[ 5 6]
[ 7 8]
[ 9 10]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. The `np.sort()` function is then used to sort the elements of the array `x` along axis 0, which means sorting along the columns.
10). What is the output of the following code?
import numpy as np
x = np.array([[6, 5],
[10, 9],
[8, 7]])
print("Values bigger than 7 =", x[x>7])
a) Values bigger than 7 = [10, 9, 8, 7]
b) Values bigger than 7 = [10, 9, 8]
c) Values bigger than 7 = [8, 7]
d) Values bigger than 7 = [6, 5]
Correct answer is: b) Values bigger than 7 = [10, 9, 8]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. The code then uses boolean indexing (`x>7`) to extract the elements from `x` that are bigger than 7.
11). What is the output of the following code?
import numpy as np
x = np.array([[6, 5],
[10, 9],
[8, 7]])
x[x >= 7] = 0
print("New matrix")
print(x)
a) [[0, 0], [0, 0], [0, 0]]
b) [[6, 5], [10, 9], [8, 7]]
c) [[6, 5], [0, 0], [0, 0]]
d) [[0, 5], [0, 9], [0, 7]]
Correct answer is: c) [[6, 5], [0, 0], [0, 0]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. It then modifies the elements of the array `x` based on the condition `x >= 7`. Any element in the array that is greater than or equal to 7 is replaced with 0.
12). What is the purpose of the following code?
import numpy as np
x = np.array([[6, 5],
[10, 9],
[8, 7]])
print("Original array: ", x)
print("After swapping")
new = print(x[::-1, :])
a) The code transposes the original array.
b) The code reverses the rows of the original array.
c) The code reverses the columns of the original array.
d) The code prints the original array in reverse order.
Correct answer is: b) The code reverses the rows of the original array.
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. It then prints the original array using `print(“Original array: “, x)`. The line `new = print(x[::-1, :])` involves array slicing. Specifically, `x[::-1, :]` reverses the rows of the original array `x`.
13). What is the output of the following code?
import numpy as np
x = np.array([[6, 5],
[10, 9],
[8, 7]])
x[0,:] = x[0,:]*2
print("After multiplying the first row by 2: ", x)
a) [[12, 10], [10, 9], [8, 7]]
b) [[12, 10], [20, 18], [16, 14]]
c) [[6, 5], [10, 9], [8, 7]]
d) [[6, 5], [10, 9], [8, 7, 2]]
Correct answer is: a) [[12, 10], [10, 9], [8, 7]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. Next, it modifies the first row of the array `x` by multiplying it by 2.
14). What is the purpose of the following code?
import numpy as np
x = np.array([[6, 5],
[10, 9],
[8, 7]])
x[0,:] = x[0,:] + x[1,:]
print("After addition: ", x)
a) It transposes the matrix `x`
b) It calculates the element-wise addition of the first and second rows in the matrix `x`
c) It calculates the dot product of the first and second rows in the matrix `x`
d) It reshapes the matrix `x` into a 1-dimensional array
Correct answer is: b) It calculates the element-wise addition of the first and second rows in the matrix `x`
Explanation: The given code snippet imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. The line `x[0,:] = x[0,:] + x[1,:]` performs element-wise addition of the first row (`[6, 5]`) and the second row (`[10, 9]`) of the matrix `x`. The result is then assigned back to the first row of the matrix `x`.
15). What is the output of the following code?
import numpy as np
x = np.array([6+2j, 10+5j])
print("Real part: ", x.real)
print("Imaginary part: ", x.imag)
a) Real part: [6, 10], Imaginary part: [2, 5]
b) Real part: [8, 15], Imaginary part: [2, 5]
c) Real part: [6, 10], Imaginary part: [2j, 5j]
d) Real part: [8, 15], Imaginary part: [2j, 5j]
Correct answer is: a) Real part: [6, 10], Imaginary part: [2, 5]
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with complex numbers: [6+2j, 10+5j]. The `real` attribute of the complex array `x` gives the real parts of the complex numbers, and the `imag` attribute gives the imaginary parts.
16). What is the output of the following code?
import numpy as np
x = np.array([56, 18, 28, 36])
y = np.array([76, 36, 56])
print(np.intersect1d(x, y))
a) [56, 18, 28, 36]
b) [56, 36]
c) [18, 28, 76]
d) [76, 18, 28]
Correct answer is: b) [56, 36]
Explanation: The given code imports the NumPy library and creates two 1-dimensional NumPy arrays, `x` and `y`, with the elements [56, 18, 28, 36] and [76, 36, 56] respectively. The `np.intersect1d()` function is then used to find the intersection of the elements between the arrays `x` and `y`.
17). What is the output of the following code?
import numpy as np
x = np.array([25, 33, 10, 45, 33, 10])
print(np.unique(x))
a) [10, 25, 33, 45]
b) [10, 33, 45]
c) [25, 33, 10, 45]
d) [25, 33, 10]
Correct answer is: a) [10, 25, 33, 45]
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [25, 33, 10, 45, 33, 10]. The `np.unique()` function is then used to find the unique elements in the array `x`.
18). What is the output of the following code?
import numpy as np
x = np.array([56, 18, 28, 36])
y = np.array([76, 36, 56])
print(np.setdiff1d(x, y))
a) [56, 18, 28, 36]
b) [18, 28]
c) [76]
d) [18, 28, 76]
Correct answer is: b) [18, 28]
Explanation: The given code imports the NumPy library and creates two 1-dimensional NumPy arrays, `x` and `y`, with the elements [56, 18, 28, 36] and [76, 36, 56], respectively. The `np.setdiff1d()` function is then used to find the set difference between the arrays `x` and `y`. The set difference between two arrays contains the elements that are present in the first array but not in the second array.
19). What is the output of the following code?
import numpy as np
x = np.array([56, 18, 28, 36])
y = np.array([76, 36, 56, 90])
print(np.union1d(x, y))
a) [18, 28, 36, 56, 76, 90]
b) [56, 18, 28, 36, 76, 90]
c) [56, 18, 28, 36]
d) [18, 28, 36, 76, 90]
Correct answer is: a) [18, 28, 36, 56, 76, 90]
Explanation: The given code imports the NumPy library and creates two 1-dimensional NumPy arrays, `x` and `y`, with the elements [56, 18, 28, 36] and [76, 36, 56, 90] respectively. The `np.union1d()` function is then used to find the unique values that are in either of the arrays. The union of `x` and `y` will contain all the unique elements present in both arrays, without any duplicates. The output will be a new sorted 1-dimensional array with these unique elements.
20). What is the output of the following code?
import numpy as np
x = np.array([76, 36, 56, 90])
print("Index of maximum Values: ", np.argmax(x))
a) 0
b) 1
c) 2
d) 3
Correct answer is: d) 3
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [76, 36, 56, 90]. The `np.argmax()` function is then used to find the index of the maximum value in the array `x`.
21). What is the output of the following code?
import numpy as np
x = np.array([76, 36, 56, 90])
print("Index of maximum Values: ", np.argmin(x))
a) 1
b) 2
c) 3
d) 4
Correct answer is: b) 2
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [76, 36, 56, 90]. The `np.argmin()` function is then used to find the index of the minimum value in the array `x`.
22). What is the output of the following code?
import numpy as np
x = np.array([[4, 8, 7], [2, 3, 6]])
print("Fifth element: ", x[1, 1])
a) 4
b) 8
c) 3
d) 6
Correct answer is: c) 3
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[4, 8, 7], [2, 3, 6]]. In NumPy arrays, indexing starts from 0. The expression `x[1, 1]` accesses the element at the second row and second column of the array `x`.
23). What is the output of the following code?
import numpy as np
x = np.zeros((2, 1, 3))
print("After removing single-dimensional entries: ", np.squeeze(x).shape)
a) (2, 1, 3)
b) (2, 3)
c) (1, 3)
d) (3,)
Correct answer is: b) (2, 3)
Explanation: The given code imports the NumPy library and creates a 3-dimensional NumPy array `x` with the shape (2, 1, 3) filled with zeros. The `np.squeeze()` function is then used to remove single-dimensional entries from the array `x`. The `np.squeeze()` function removes any dimensions from the array that have a size of 1. In this case, the second dimension has a size of 1, so it is removed from the array. Before squeezing, the shape of `x` is (2, 1, 3), which means it has 2 elements along the first dimension, 1 element along the second dimension, and 3 elements along the third dimension. After squeezing, the shape becomes (2, 3) because the second dimension with a size of 1 is removed. Therefore, the correct answer is (2, 3).
24). What is the output of the following code?
import numpy as np
x = np.array((3, 8, 5))
y = np.array((4, 0, 7))
z = np.column_stack((x, y))
print(z)
a) [[3, 4], [8, 0], [5, 7]]
b) [[3, 8, 5], [4, 0, 7]]
c) [[3, 8], [4, 0], [5, 7]]
d) [[4, 3], [0, 8], [7, 5]]
Correct answer is: a) [[3, 4], [8, 0], [5, 7]]
Explanation: The given code imports the NumPy library and creates two 1-dimensional NumPy arrays `x` and `y` with the elements (3, 8, 5) and (4, 0, 7) respectively. Then, the `np.column_stack()` function is used to stack the arrays `x` and `y` column-wise, forming a new 2-dimensional array `z`.
25). What is the output of the following code?
import numpy as np
x = np.arange(1, 11)
print("Original array:", x)
print("After splitting:")
print(np.split(x, [2, 5]))
a) Original array: [1 2 3 4 5 6 7 8 9 10]
After splitting: [array([1, 2]), array([3, 4, 5]), array([ 6, 7, 8, 9, 10])]
b) Original array: [1 2 3 4 5 6 7 8 9 10]
After splitting: [array([1, 2]), array([3, 4, 5, 6]), array([7, 8, 9, 10])]
c) Original array: [1 2 3 4 5 6 7 8 9 10]
After splitting: [array([1, 2]), array([3, 4, 5]), array([6, 7, 8, 9, 10])]
d) Original array: [1 2 3 4 5 6 7 8 9 10]
After splitting: [array([1, 2]), array([3, 4]), array([5, 6, 7, 8, 9, 10])]
Correct answer is: a) Original array: [1 2 3 4 5 6 7 8 9 10]
After splitting: [array([1, 2]), array([3, 4, 5]), array([ 6, 7, 8, 9, 10])]
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. The `np.split()` function is then used to split the array into subarrays. The `np.split(x, [2, 5])` function call splits the array `x` at the indices [2, 5]. It means the array will be split into three subarrays: from index 0 to index 2 (exclusive), from index 2 to index 5 (exclusive), and from index 5 to the end of the array.