🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Semaphores and Classic Problems: all 9 questions

Quiz: Semaphores and Classic Problems · Question 8 of 9

What does this print? (code)

What does this print?

s = 7
for op in ["P"] * 20 + ["V"] * 15:
    s = s - 1 if op == "P" else s + 1
print(s)
  1. A.2
  2. B.−13
  3. C.22
  4. D.7
Answer & explanation

Correct answer: 2

7 − 20 + 15 = 2; the order of operations does not change the final value.

Can you get the rest right without peeking?

Take the timed Quiz: Semaphores and Classic Problems quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →