NumPy MCQ
1). What is the output of the following code?
import numpy as np
x = np.array([6, 8, 3, 5])
print(np.all(x))
a) True
b) False
c) 0
d) [True, True, True, True]
Correct answer is: b) False
Explanation: The code snippet imports the NumPy library and creates an array `x` with the values [6, 8, 3, 5]. The `np.all()` function is then used to check if all elements in the array are true. However, in NumPy, the value `0` is considered as False, and any non-zero value is considered as True. In this case, the array `x` contains non-zero values (6, 8, 3, 5), so `np.all(x)` will return `True` if all elements in the array are non-zero. Since the array contains zero (`3`), the function will return `False`.
2). What is the output of the following code?
import numpy as np
x = np.array([6, 8, 3, np.inf])
print(np.isfinite(x))
a) [ True True True False]
b) [ True False True False]
c) [ True True True True]
d) [False False False False]
Correct answer is: a) [True True True False]
Explanation: The given code creates a NumPy array `x` containing four elements: `[6, 8, 3, np.inf]`. The `np.isfinite()` function is applied to the array, which returns a new Boolean array with the same shape as the input array. In the output, `True` represents finite numbers, and `False` represents non-finite numbers (such as `np.inf`).
3). What is the output of the following code?
import numpy as np
x = np.array([2, 4, np.nan, 7])
print(np.isnan(x))
a) [False, False, False, False]
b) [True, True, True, True]
c) [False, False, True, False]
d) [True, False, True, False]
Correct answer is: c) [False, False, True, False]
Explanation: The given code imports the NumPy library as np and creates a NumPy array ‘x’ with the values [2, 4, np.nan, 7]. The ‘np.isnan(x)’ function checks for NaN (Not a Number) values in the array ‘x’.
4). What is the output of the following code?
import numpy as np
x = np.array([2, 4, 6+9j, 7])
print(np.iscomplex(x))
a) [False, False, True, False]
b) [True, True, True, True]
c) [False, False, False, False]
d) [True, False, True, False]
Correct answer is: a) [False, False, True, False]
Explanation: The code snippet imports the NumPy library as np and creates an array ‘x’ containing four elements: 2, 4, 6+9j, and 7. The ‘np.iscomplex()’ function is then used to check whether each element in the array ‘x’ is complex or not. The ‘np.iscomplex()’ function returns a Boolean array with the same shape as the input array, where each element is ‘True’ if the corresponding element in the input array is complex, and ‘False’ otherwise.
5). What is the output of the following code?
import numpy as np
x = np.array([2, 4, 6 + 9j, 7])
print(np.isreal(x))
a) [ True True False True]
b) [ True True True True]
c) [ True True True False]
d) [ True True False False]
Correct answer is: a) [ True True False True]
Explanation: The code creates a NumPy array `x` with four elements: 2, 4, 6+9j (complex number), and 7. The `np.isreal()` function is then applied to this array. The `np.isreal()` function checks if each element in the array is a real number (not a complex number). It returns a Boolean array, where `True` indicates that the element is a real number, and `False` indicates that the element is a complex number.
6). What is the output of the following code?
import numpy as np
x = 8.9
print(np.isscalar(x))
y = [8.9]
print(np.isscalar(y))
a) True, True
b) False, False
c) True, False
d) False, True
Correct answer is: c) True, False
Explanation: The `np.isscalar(x)` function returns True if the input `x` is a scalar (a single numeric value) and False if it is an array or any other type. In the first case, `x` is assigned the value 8.9, which is a scalar (float), so the output of `np.isscalar(x)` is True. In the second case, `y` is assigned the value `[8.9]`, which is a list containing one element. Lists are not considered scalars in NumPy. Therefore, the output of `np.isscalar(y)` is False.
7). What is the output of the following code?
import numpy as np
x = np.array([3, 8, 5])
y = np.array([3, 8, 6])
print(x == y)
a) [True, True, True]
b) [False, False, False]
c) [True, True, False]
d) [False, True, True]
Correct answer is: c) [True, True, False]
Explanation: The code snippet creates two NumPy arrays, `x` and `y`, and then performs an element-wise comparison using the `==` operator. The resulting array will have the same shape as the input arrays, and each element in the output will be `True` if the corresponding elements in `x` and `y` are equal and `False` otherwise.
8). What is the output of the following code?
import numpy as np
x = np.array([8, 3])
y = np.array([9, 1])
print(x > y)
a) [False, False]
b) [True, True]
c) [False, True]
d) [True, False]
Correct answer is: c) [False, True]
Explanation: The code snippet creates two NumPy arrays `x` and `y`, each with two elements. It then applies the comparison `x > y`, which checks element-wise whether the corresponding elements of `x` are greater than the corresponding elements of `y`.
9). What is the output of the following code?
import numpy as np
x = np.array([6, 4])
y = np.array([6, 3])
print(x >= y)
a) [True, False]
b) [False, True]
c) [True, True]
d) [False, False]
Correct answer is: c) [True, True]
Explanation: The code snippet imports the NumPy library, creates two NumPy arrays ‘x’ and ‘y’, and then prints the result of the comparison ‘x >= y’. The comparison operator ‘>=’ checks if each element of ‘x’ is greater than or equal to the corresponding element in ‘y’. The result of the comparison is a Boolean array that shows the comparison result for each element.
10). What is the output of the following code?
import numpy as np
x = np.array([6, 4])
y = np.array([2, 9])
print(x < y)
a) [False, True]
b) [True, False]
c) [True, True]
d) [False, False]
Correct answer is: a) [False, True]
Explanation: The given code snippet imports the NumPy library as np and creates two NumPy arrays, x and y. The arrays are [6, 4] and [2, 9], respectively. The statement `x < y` performs element-wise comparison between the elements of arrays x and y.
11). What is the output of the following code?
import numpy as np
x = np.array([10, 34, 86, 26, 56])
print("Size of the array: ", x.size)
print("Memory size of one array element in bytes: ", x.itemsize)
print("Memory size of numpy array in bytes:", x.size * x.itemsize)
a) Size of the array: 5
Memory size of one array element in bytes: 4
Memory size of numpy array in bytes: 20
b) Size of the array: 5
Memory size of one array element in bytes: 8
Memory size of numpy array in bytes: 40
c) Size of the array: 5
Memory size of one array element in bytes: 16
Memory size of numpy array in bytes: 80
d) Size of the array: 5
Memory size of one array element in bytes: 32
Memory size of numpy array in bytes: 160
Correct answer is: a) Size of the array: 5
Memory size of one array element in bytes: 4
Memory size of numpy array in bytes: 20
Explanation: The given code snippet uses NumPy to create an array `x` with elements [10, 34, 86, 26, 56]. The three print statements then display information about the array.
1. Size of the array: The `x.size` attribute returns the number of elements in the array `x`, which is 5 in this case.
2. Memory size of one array element in bytes: The `x.itemsize` attribute returns the size of one array element in bytes. Since the array contains integer values, the size of each element is 4 bytes in most standard Python installations.
3. Memory size of numpy array in bytes: The memory size of the entire NumPy array can be calculated by multiplying the size of one array element (`x.itemsize`) with the number of elements in the array (`x.size`). In this case, the memory size of the numpy array is 20 bytes (5 elements * 4 bytes per element).
12). What is the output of the following code?
import numpy as np
x = np.ones(3, dtype=int)
print(x)
a) [1, 1, 1]
b) [1, 1, 1, 1]
c) [1.0, 1.0, 1.0]
d) [1, 1, 1.0]
Correct answer is: a) [1, 1, 1]
Explanation: The code snippet imports the NumPy library as np and creates a NumPy array called `x` using the `np.ones()` function. The `np.ones()` function creates an array of ones with the specified shape. In this case, it creates an array with 3 elements, all set to 1, and the data type is specified as `int`.
13). What is the output of the following code?
import numpy as np
x = np.arange(1, 11)
print("Array: ", x)
a) Array: [1 2 3 4 5 6 7 8 9 10]
b) [1 2 3 4 5 6 7 8 9 10]
c) [ 2 3 4 5 6 7 8 9 10]
d) Array: [ 1 2 3 4 5 6 7 8 9]
Correct answer is: a) Array: [1 2 3 4 5 6 7 8 9 10]]
Explanation: The code snippet imports the NumPy library as np and then uses np.arange(1, 11) to create a NumPy array `x` with elements starting from 1 up to, but not including, 11. The resulting array `x` is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. When printing the array using `print(“Array: “, x)`, it will display the array elements without the “Array:” label and in a 1-dimensional format.
14). What is the output of the following code?
import numpy as np
x = np.arange(2, 11, 2)
print("Array of even numbers: ", x)
a) Array of even numbers: [2, 4, 6, 8, 10]
b) Array of even numbers: [2, 3, 4, 5, 6, 7, 8, 9, 10]
c) Array of even numbers: [1, 2, 3, 4, 5]
d) Array of even numbers: [2, 4, 6, 8]
Correct answer is: a) Array of even numbers: [2, 4, 6, 8, 10]
Explanation: In the code snippet, the NumPy function `np.arange()` is used to generate an array of even numbers starting from 2, incrementing by 2, and ending before 11 (exclusive). The arguments for `np.arange()` are `start`, `stop`, and `step`.
15). What is the output of the following code?
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(x)
a) [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b) [1, 2, 3, 4, 5, 6, 7, 8, 9]
c) [[1 2 3]
[4 5 6]
[7 8 9]]
d) [[1 4 7]
[2 5 8]
[3 6 9]]
Correct answer is: c) [[1 2 3]
[4 5 6]
[7 8 9]]
Explanation: The code snippet imports the NumPy library as np and creates a 2-dimensional array called x using np.array(). The array contains three rows and three columns with the values:
[[1 2 3]
[4 5 6]
[7 8 9]]
16). What is the output of the following code?
import numpy as np
x = np.identity(3)
print(x)
a) [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
b) [[0, 1, 0], [1, 0, 1], [0, 1, 0]]
c) [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
d) [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Correct answer is: a) [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
Explanation: The given code imports the NumPy library and then creates a 3×3 identity matrix using the `np.identity(3)` function. The identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere. For a 3×3 identity matrix, it looks like this:
[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]
17). What is the purpose of the following code?
from numpy import random
for i in range(5):
rand_num = random.rand()
print(rand_num)
a) To generate an array of 5 random integers between 0 and 1.
b) To generate a random float number between 0 and 1 and print it 5 times.
c) To generate an array of 5 random float numbers between 0 and 1.
d) To generate a random integer between 0 and 1 and print it 5 times.
Correct answer is: c) To generate an array of 5 random float numbers between 0 and 1.
Explanation: The code imports the `random` module from the NumPy library, which provides various functions for generating random numbers. The `random.rand()` function is used to generate random float numbers in the half-open interval [0, 1), meaning that 0 is included, but 1 is excluded. The code then enters a loop that iterates 5 times. In each iteration, the `random.rand()` function generates a new random float number and assigns it to the variable `rand_num`. The `print()` function is used to display this random float number. As a result, the code prints 5 random float numbers between 0 and 1, each on a separate line.
18). What is the purpose of the following code?
import numpy as np
x = np.random.normal(size=5)
print(x)
a) It generates a random array of size 5 with normally distributed values.
b) It calculates the mean of a 5-element array using the normal distribution.
c) It generates an array of 5 random integers from a normal distribution.
d) It calculates the standard deviation of a 5-element array using the normal distribution.
Correct answer is: a) It generates a random array of size 5 with normally distributed values.
Explanation: The given code utilizes NumPy, a Python library for numerical computations, to achieve its purpose. The `np.random.normal()` function generates a random array of size 5 with values drawn from a normal distribution. The `size` parameter specifies the size of the array to be generated. In the context of this code, the variable `x` is assigned the randomly generated array of size 5. Each element in `x` is a random number sampled from a normal distribution with mean 0 and standard deviation 1, as that is the default behavior of `np.random.normal()`.
19). What is the output of the following code?
import numpy as np
x = np.arange(1, 11)
for val in x:
if val != 5:
print(val, end=" ")
a) 1 2 3 4 5 6 7 8 9 10
b) 1 2 3 4 6 7 8 9 10
c) 1 2 3 4 6 7 8 9
d) 1 2 3 4 6 7 8 9 10 11
Correct answer is: b) 1 2 3 4 6 7 8 9 10
Explanation: The code snippet uses NumPy’s arange() function to create an array `x` with elements from 1 to 10 (inclusive). It then iterates over each element in the array using a for loop. Inside the loop, the condition `if val != 5:` checks if the current value is not equal to 5. If the condition is true, the value is printed using `print(val, end=” “)`.
20). What is the output of the following code?
import numpy as np
x = np.arange(1, 13)
x.reshape(3, 4)
a) [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
b) [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
c) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
d) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Correct answer is: a) [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Explanation: The code snippet imports the NumPy library and creates an array ‘x’ using np.arange(1, 13), which generates an array of integers from 1 to 12 (inclusive of 1, exclusive of 13). The next line, x.reshape(3, 4), reshapes the ‘x’ array into a 2-dimensional array with 3 rows and 4 columns. The elements of the original ‘x’ array are distributed in row-major order to fill the new 3×4 array.
21). What is the output of the following code?
import numpy as np
x = np.arange(1, 16)
x[(x >= 6) & (x <= 10)] *= -1
print(x)
a) [ 1 2 3 4 5 -6 -7 -8 -9-10 11 12 13 14 15]
b) [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
c) [ 1 2 3 4 5 6 -7 -8 -9-10 11 12 13 14 15]
d) [ 1 2 3 4 5 -6 7 8 9 10 11 12 13 14 15]
Correct answer is: a) [ 1 2 3 4 5 -6 -7 -8 -9-10 11 12 13 14 15]
Explanation: Let’s break down the code step by step: `import numpy as np`: Imports the NumPy library and assigns it the alias ‘np’. `x = np.arange(1, 16)`: Creates a NumPy array ‘x’ containing integers from 1 to 15 (inclusive), excluding 16. The array ‘x’ looks like this: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]. `x[(x >= 6) & (x <= 10)] *= -1`: This line uses Boolean indexing to select the elements of ‘x’ that satisfy the condition (x >= 6) & (x <= 10), i.e., elements greater than or equal to 6 and less than or equal to 10. The selected elements are [6, 7, 8, 9, 10]. Then, it multiplies these selected elements by -1, effectively negating them. `print(x)`: Prints the modified ‘x’ array.
22). What is the output of the following code?
import numpy as np
x = np.array([4, 2, 8])
y = np.array([7, 3, 5])
print(x * y)
a) [28, 6, 40]
b) [11, 5, 13]
c) [28, 6, 40, 11, 5, 13]
d) [ 28, 6, 40, 7, 3, 5]
Correct answer is: a) [28, 6, 40]
Explanation: The code snippet imports the NumPy library, creates two NumPy arrays x and y, and performs element-wise multiplication between the arrays using the * operator. When two NumPy arrays of the same shape are multiplied element-wise, each element of one array is multiplied by the corresponding element of the other array.
23). What is the output of the following code?
import numpy as np
x = np.array([[5, 6, 7],
[8, 9, 10]])
print("Shape of the matrix: ", x.shape)
a) Shape of the matrix: (3, 2)
b) Shape of the matrix: (2, 2)
c) Shape of the matrix: (2, 3)
d) Shape of the matrix: (3, 3)
Correct answer is: b) Shape of the matrix: (2, 3)
Explanation: The code defines a NumPy array `x` with two rows and three columns. The `x.shape` attribute returns a tuple representing the dimensions of the array, where the first element is the number of rows and the second element is the number of columns. In this case, the output will be “Shape of the matrix: (2, 3)” since `x` has 2 rows and 3 columns. The correct answer is option b).
24). What is the output of the following code?
import numpy as np
x = np.diag([7, 8])
print(x)
a) [[7, 0], [0, 8]]
b) [7, 8]
c) [7, 8, 0, 0]
d) [[7], [8]]
Correct answer is: a) [[7, 0], [0, 8]]
Explanation: The code snippet imports the NumPy library and uses the np.diag() function to create a 2-dimensional diagonal array with the elements [7, 8]. The resulting array will have zeros in off-diagonal elements and the given elements on the diagonal. The np.diag() function creates a diagonal matrix from the given input array, placing the elements on the diagonal of the resulting matrix and filling other elements with zeros.
25). What is the output of the following code?
import numpy as np
x = np.array([[5, 3],
[7, 4]])
print("Sum of all the elements : ", np.sum(x))
a) 9
b) 14
c) 19
d) 22
Correct answer is: c) 19
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 all elements in the array `x`.