Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q6 of 15
NumPy - Creating Arrays
What is wrong with this code snippet?
import numpy as np
matrix = np.eye(k=2, 3, 3)
print(matrix)
AThere is no error in the code
BThe function np.eye() does not accept keyword arguments
CThe matrix dimensions are invalid
DKeyword argument 'k' is used before positional arguments
Step-by-Step Solution
Solution:
  1. Step 1: Check argument order

    In Python, positional arguments must come before keyword arguments.
  2. Step 2: Analyze code

    Here, k=2 is a keyword argument placed before positional arguments 3, 3.
  3. Step 3: Correct usage

    Either use all positional or place keyword arguments after positional ones.
  4. Final Answer:

    Keyword argument 'k' is used before positional arguments -> Option D
  5. Quick Check:

    Check argument order in function call [OK]
Quick Trick: Positional args must precede keyword args in function calls [OK]
Common Mistakes:
  • Mixing positional and keyword arguments incorrectly
  • Assuming np.eye() disallows keyword arguments
  • Incorrect argument count

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes