ProgrammingHardTCS NQT · Infosys · Cognizant GenC

Consider the pseudocode. s = 0 for i = 1 to 7: if i mod 3 == 0: continue j = i while j > 0: if j mod 4 == 0: break s = s + j j = j - 2 print s What is printed?

  1. A. 38
  2. B. 28
  3. C. 29
  4. D. 22

Correct answer: B. 28

i = 1 adds 1; i = 2 adds 2; i = 4 breaks at once (0); i = 5 adds 5+3+1 = 9; i = 7 adds 7+5+3+1 = 16; i = 3 and 6 are skipped. Total 28. Ignoring continue gives 38.

More Pseudocode and Code Tracing questions

Exams that ask this topic