🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Prefix Sums and Difference Arrays: all 13 questions

Quiz: Prefix Sums and Difference Arrays · Question 1 of 13

What does this print? (code)

What does this print?

a = [2, 4, 6, 8]
P = [0]
for x in a:
    P.append(P[-1] + x)
print(P[3] - P[1])
  1. A.6
  2. B.10
  3. C.12
  4. D.18
Answer & explanation

Correct answer: 10

P = [0, 2, 6, 12, 20]; P[3] − P[1] = 12 − 2 = 10, the sum of a[1..2] = 4 + 6.

Can you get the rest right without peeking?

Take the timed Quiz: Prefix Sums and Difference Arrays quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →