ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
import itertools
print(list(itertools.accumulate([1, 2, 3, 4])))
print(list(itertools.accumulate([1, 2, 3, 4], func=lambda a, b: a * b)))
  1. A. [1, 2, 3, 4] [1, 2, 6, 24]
  2. B. [1, 3, 6, 10] [1, 2, 6, 24]
  3. C. [1, 3, 6, 10] [1, 2, 3, 4]
  4. D. [10, 10, 10, 10] [24, 24, 24, 24]

Correct answer: B. [1, 3, 6, 10] [1, 2, 6, 24]

Default accumulate computes running sums: 1, 1+2=3, 3+3=6, 6+4=10. With func=multiply, it computes a running product instead: 1, 1*2=2, 2*3=6, 6*4=24.

More Python Programming questions

Exams that ask this topic