arrow
arrow
arrow
What is the output of the following? import numpy as np A = np.array([[2, 3], [10, 20]]) print(A + 1)
Question

What is the output of the following?
import numpy as np
A = np.array([[2, 3], [10, 20]])
print(A + 1)

A.

[ [3 4]
[10 20] ]

B.

[ [3 4]
[11 21] ]

C.

[ [2 3]
[11 21] ]

D.

[ [3  3]
[11 20] ]

Correct option is B


The code creates a NumPy array A with the following elements:
A = np.array([[2, 3], [10, 20]])
Then, the statement A + 1 adds 1 to each element of the array. This operation is element-wise, meaning that 1 is added to each individual element of the array:
[[2, 3], -> [[2+1, 3+1], = [[3, 4],
[10, 20]] [10+1, 20+1]] [11, 21]]
So the output will be:
[[3 4]
[11 21]]
Important Key Points:
1. Element-wise Operation: In NumPy, adding a scalar to an array performs the operation on each element of the array individually.
2. Array Shape Preservation: The shape of the array is preserved. It remains a 2x2 matrix after the addition.
3. Broadcasting: NumPy automatically broadcasts the scalar value 1 to each element of the array.
Knowledge Booster:
· Option (a): This is incorrect because 1 is added to every element of the array, so the correct result should be [[3, 4], [11, 21]], not [[3, 4], [10, 20]].
· Option (c): This is incorrect because the first row should become [3, 4], not [2, 3]. The scalar 1 is added to all elements, not just the second row.
· Option (d): This is incorrect because the first row should become [3, 4], not [3, 3]. Each element in the array is incremented by 1, so the second element in the first row will become 4.

Free Tests

Free
Must Attempt

UPTET Paper 1: PYP Held on 23rd Jan 2022 (Shift 1)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Social Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Maths & Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
test-prime-package

Access ‘EMRS PGT’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
354k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow