Bird
0
0

Which of the following is the correct syntax to create a 2D NumPy array with values 1, 2 in the first row and 3, 4 in the second row?

easy📝 Syntax Q3 of 15
NumPy - Creating Arrays
Which of the following is the correct syntax to create a 2D NumPy array with values 1, 2 in the first row and 3, 4 in the second row?
Anp.array([[1, 2], [3, 4]])
Bnp.array([1, 2, 3, 4])
Cnp.array(1, 2, 3, 4)
Dnp.array([1, 2], [3, 4])
Step-by-Step Solution
Solution:
  1. Step 1: Understand 2D array creation syntax

    NumPy expects a list of lists for 2D arrays, so nested brackets are needed.
  2. Step 2: Identify correct option

    Only np.array([[1, 2], [3, 4]]) uses nested lists correctly: [[1, 2], [3, 4]].
  3. Final Answer:

    np.array([[1, 2], [3, 4]]) -> Option A
  4. Quick Check:

    2D array needs nested lists [OK]
Quick Trick: Use double brackets for 2D arrays in np.array [OK]
Common Mistakes:
  • Missing nested brackets
  • Passing multiple arguments instead of one list
  • Using flat list for 2D array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes