Bird
0
0

Find the mistake in this code snippet:

medium📝 Debug Q7 of 15
NumPy - Creating Arrays
Find the mistake in this code snippet:
import numpy as np
arr = np.ones((2, 2), dtype='integer')
print(arr)
AThe dtype 'integer' is invalid; use 'int' or 'int32'
BShape must be a list, not a tuple
Cnp.ones() does not accept dtype argument
DMissing parentheses in print statement
Step-by-Step Solution
Solution:
  1. Step 1: Check dtype argument

    NumPy expects dtype names like 'int', 'float', or specific types like 'int32'. 'integer' is not valid.
  2. Step 2: Verify other parts

    Shape as tuple is valid, np.ones() accepts dtype, and print syntax is correct in Python 3.
  3. Final Answer:

    The dtype 'integer' is invalid; use 'int' or 'int32' -> Option A
  4. Quick Check:

    Use valid dtype names like 'int' or 'float' [OK]
Quick Trick: Use correct dtype names like 'int', not 'integer' [OK]
Common Mistakes:
  • Using invalid dtype strings
  • Confusing list and tuple for shape
  • Syntax errors in print statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes