NumPy Questions with Answers

18 practice questions on NumPy (Data Analytics) with answers and step-by-step solutions. Try 10 samples below, then practise all 18 free in the AptiNest app.

Sample questions

Question 1Easy

What does this print?

import numpy as np
a = np.array([[1, 2], [3, 4], [5, 6]])
print(a.shape)
  1. (3,)
  2. (3, 2)
  3. (2, 3)
  4. 6

Answer: B. (3, 2)

Explanation

Three rows of two columns.

Step-by-step solution

  1. The array has 3 inner lists (rows).
  2. Each row has 2 elements (columns).
  3. shape = (3, 2).

Why the other options are wrong

  • A. (3,) is the shape of a 1-D array.
  • C. (2, 3) swaps rows and columns.
  • D. 6 is a.size, the total number of elements.

Tip: shape = (rows, columns).

Question 2Easy

What does this print?

import numpy as np
print(np.arange(12).reshape(3, -1).shape)
  1. (3, 4)
  2. (4, 3)
  3. (3, 12)
  4. (3, 1)

Answer: A. (3, 4)

Explanation

-1 lets NumPy infer the size: 12 / 3 = 4.

Step-by-step solution

  1. np.arange(12) has 12 elements.
  2. reshape(3, -1): -1 means 'work it out'.
  3. 12 / 3 = 4, so shape = (3, 4).

Why the other options are wrong

  • B. The first dimension is fixed at 3.
  • C. 3 x 12 = 36 elements, more than 12.
  • D. 3 x 1 = 3 elements, fewer than 12.

Tip: -1 in reshape = total / product of the others.

Question 3Easy

What does this print?

import numpy as np
a = np.array([2, 7, 4, 7])
print(np.unique(a))
  1. [2 7 4 7]
  2. [2 4 7]
  3. 3
  4. [2 7 4]

Answer: B. [2 4 7]

Explanation

unique returns the sorted distinct values.

Step-by-step solution

  1. np.unique removes duplicates.
  2. It also sorts the result.
  3. Output: [2 4 7].

Why the other options are wrong

  • A. Duplicates are removed.
  • C. 3 is the count of unique values, not the output.
  • D. The result is sorted, so 4 comes before 7.

Tip: np.unique = distinct + sorted.

Question 4Easy

What does this print?

import numpy as np
a = np.array([1, 2, 3])
print(a * 2)
  1. [2 4 6]
  2. [1, 2, 3, 1, 2, 3]
  3. [1 2 3 1 2 3]
  4. [2, 4, 6]

Answer: A. [2 4 6]

Explanation

NumPy applies arithmetic element-wise; arrays print without commas.

Step-by-step solution

  1. a * 2 multiplies each element (broadcasting a scalar).
  2. Result: array([2, 4, 6]).
  3. NumPy prints arrays with spaces and no commas: [2 4 6].

Why the other options are wrong

  • B. Repetition happens with Python lists, not NumPy arrays.
  • C. NumPy does arithmetic, not repetition, and prints without commas.
  • D. The values are right, but NumPy arrays print without commas.

Tip: List * 2 repeats; array * 2 doubles.

Question 5Medium

What does this print?

import numpy as np
print(np.linspace(0, 1, 5).tolist())
  1. [0.0, 0.2, 0.4, 0.6, 0.8]
  2. [0, 1, 2, 3, 4]
  3. [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
  4. [0.0, 0.25, 0.5, 0.75, 1.0]

Answer: D. [0.0, 0.25, 0.5, 0.75, 1.0]

Explanation

linspace includes both endpoints and spaces 5 points evenly.

Step-by-step solution

  1. linspace(0, 1, 5) gives 5 evenly spaced points including both ends.
  2. Step = (1 - 0) / (5 - 1) = 0.25.
  3. Points: 0.0, 0.25, 0.5, 0.75, 1.0.

Why the other options are wrong

  • A. That uses step 0.2 and excludes the end point.
  • B. linspace does not count by integers.
  • C. That is 6 points, not 5.

Tip: linspace step = (stop - start) / (num - 1).

Question 6Medium

What does this print?

import numpy as np
a = np.array([3, 1, 2])
print(np.argmax(a))
  1. 1
  2. 0
  3. 2
  4. 3

Answer: B. 0

Explanation

argmax returns the index of the largest value (3 at index 0).

Step-by-step solution

  1. argmax returns the index of the largest value, not the value.
  2. The largest value 3 is at index 0.
  3. Output: 0 (option B).

Why the other options are wrong

  • A. 1 is the index of the smallest value.
  • C. 2 is the index of the value 2.
  • D. 3 is the largest value, not its index.

Tip: arg- functions return indices.

Question 7Medium

What does this print?

import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.sum(axis=1))
  1. 10
  2. [1 2 3 4]
  3. [4 6]
  4. [3 7]

Answer: D. [3 7]

Explanation

axis=1 sums across each row:
1 + 2 = 3,
3 + 4 = 7.

Step-by-step solution

  1. axis=1 sums across columns, within each row.
  2. Row 0: 1 + 2 = 3; row 1: 3 + 4 = 7.
  3. Result: [3 7].

Why the other options are wrong

  • A. 10 is the sum with no axis.
  • B. That is the flattened array, not a sum.
  • C. [4 6] is axis=0 (column sums).

Tip: axis=1 gives one result per row.

Question 8Medium

What does this print?

import numpy as np
a = np.array([4, 9, 16])
print(np.sqrt(a).mean())
  1. 3.0
  2. 9.666666666666666
  3. 3
  4. 29.0

Answer: A. 3.0

Explanation

sqrt gives [2, 3, 4], whose mean is 3.0.

Step-by-step solution

  1. np.sqrt([4, 9, 16]) = [2.0, 3.0, 4.0].
  2. Mean = 9 / 3 = 3.0.
  3. The result is a float, printed as 3.0.

Why the other options are wrong

  • B. 9.67 is the mean before taking square roots.
  • C. sqrt returns floats, so the mean prints as 3.0.
  • D. 29.0 is the sum of the original values.

Tip: sqrt and mean always produce floats.

Question 9Hard

What does this print?

import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.T[0].tolist())
  1. [1, 3]
  2. [2, 4]
  3. [1, 2]
  4. [3, 4]

Answer: A. [1, 3]

Explanation

The transpose's first row is the original first column.

Step-by-step solution

  1. a.T swaps rows and columns: [[1, 3], [2, 4]].
  2. a.T[0] is the first row of the transpose.
  3. That is the first column of a: [1, 3].

Why the other options are wrong

  • B. [2, 4] is the second column of a.
  • C. [1, 2] is the first row of a, before transposing.
  • D. [3, 4] is the second row of a.

Tip: Rows of a.T are columns of a.

Question 10Hard

What does this print?

import numpy as np
a = np.arange(6)
b = a[1:4]
b[0] = 50
print(a.tolist())
  1. [0, 1, 2, 3, 4, 5]
  2. [0, 50, 2, 3, 4, 5]
  3. [50, 1, 2, 3, 4, 5]
  4. [0, 50, 50, 50, 4, 5]

Answer: B. [0, 50, 2, 3, 4, 5]

Explanation

Basic slicing returns a view sharing memory with the original array.

Step-by-step solution

  1. a = [0, 1, 2, 3, 4, 5].
  2. b = a[1:4] is a view of elements 1, 2, 3.
  3. b[0] = 50 changes a[1].
  4. a becomes [0, 50, 2, 3, 4, 5].

Why the other options are wrong

  • A. NumPy slices are views, so the change reaches a.
  • C. b[0] maps to a[1], not a[0].
  • D. Only one element is assigned, not the whole slice.

Tip: NumPy slice = view; list slice = copy.

Practise all 18 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Analytics topics · All topics