Bird
0
0

The code below throws an error. What is the mistake?

medium📝 Debug Q14 of 15
NumPy - Creating Arrays
The code below throws an error. What is the mistake?
import numpy as np
matrix = np.eye(4, 3, k=4)
print(matrix)
Anp.eye() does not accept three arguments
BThe matrix must be square for np.eye()
CThe value of k is too large for the matrix shape
DThe print statement is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check the meaning of k relative to matrix size

    The parameter k=4 means the diagonal of 1s is shifted 4 above the main diagonal.
  2. Step 2: Compare k with matrix dimensions

    For a 4x3 matrix, the maximum upper diagonal shift is 2 (columns - 1). k=4 is invalid and causes an error.
  3. Final Answer:

    The value of k is too large for the matrix shape -> Option C
  4. Quick Check:

    k must be within valid diagonal range [OK]
Quick Trick: k must fit within matrix diagonal limits [OK]
Common Mistakes:
  • Assuming any k value works
  • Thinking np.eye() only creates square matrices
  • Blaming print instead of parameter error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes