Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q13 of 15
NumPy - Aggregation Functions
What is the output of the following code?
import numpy as np
arr = np.array([2, 4, 6, 8])
cum_sum = np.cumsum(arr)
print(cum_sum)
A[2 8 14 22]
B[2 4 6 8]
C[2 6 12 20]
D[20 18 14 8]
Step-by-Step Solution
Solution:
  1. Step 1: Calculate cumulative sums stepwise

    Start with 2, then 2+4=6, then 6+6=12, then 12+8=20.
  2. Step 2: Form the cumulative sum array

    The resulting array is [2, 6, 12, 20].
  3. Final Answer:

    [2 6 12 20] -> Option C
  4. Quick Check:

    Running total sums = [2, 6, 12, 20] [OK]
Quick Trick: Add each element to sum of all before it [OK]
Common Mistakes:
  • Confusing cumulative sum with original array
  • Adding elements incorrectly
  • Reversing the order of sums

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes