All sample questions / NumPy

What does print(m[1, 2]) print? NumPy Indexing, Slicing and Masks

Block 2, Indexing, Slicing and Masks, question 4 of the NumPy sample

import numpy as np
m = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(m[1, 2])

What does this print?

  1. A. 4
  2. B. np.int64(6)
  3. C. 6
  4. D. 8
Show the answer and why each option is right or wrong

Answer, C. 6

A. 4
Reads the indices as column first and row second, landing on the value at the start of row 1 instead.
B. np.int64(6)
Confuses printing a lone scalar with printing it inside a list, which would show the type wrapper.
C. 6
Correct. The comma form selects row 1 and column 2 in one step, landing on 6.
D. 8
Reads row 2 and column 1 instead of row 1 and column 2.

a[i, j] on a 2D array selects row i and column j directly with one indexing operation. Row 1 here is 4, 5, 6, and column 2 of that row is 6.

The answer above is what NumPy 2.4.4 printed when the snippet was run, not what someone expected it to do.

This question comes from a bank of 279 NumPy interview questions, all built the same way, sold as one PDF with the full answer key.

Get the 279 NumPy questions, 9 euros

Or SQL, Python, pandas and NumPy together, 1,636 questions, 19 euros

Or play the free NumPy sample, twenty questions in the browser, also as a free PDF.

Previous question Next question

Did this save you a lookup? Buy me a coffee on Ko-fi.