Bird
0
0

Which of the following is the correct syntax to select the last two columns of all rows in a NumPy array named arr?

easy📝 Syntax Q3 of 15
NumPy - Indexing and Slicing
Which of the following is the correct syntax to select the last two columns of all rows in a NumPy array named arr?
Aarr[-2:, :]
Barr[:, -2:]
Carr[:, 2:]
Darr[-1:, -2:]
Step-by-Step Solution
Solution:
  1. Step 1: Understand negative indexing

    -2: means start from the second last column to the end.
  2. Step 2: Apply slicing to rows and columns

    : for rows means all rows, -2: for columns means last two columns.
  3. Final Answer:

    arr[:, -2:] selects all rows and last two columns -> Option B
  4. Quick Check:

    Negative index for columns, all rows = arr[:, -2:] [OK]
Quick Trick: Negative indices count from the end [OK]
Common Mistakes:
  • Confusing row and column negative indices
  • Using negative index on rows instead of columns
  • Wrong slice direction

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes