ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

Array a = [3, 8, 5, 6, 7] has its odd values summed by the procedure below, and the result is printed. What is printed?

Array a = [3, 8, 5, 6, 7] has its odd values summed by the procedure below, and the result is printed. What is printed?
total = 0
for each x in a
    if x mod 2 == 1 then total = total + x
end for
Print total
  1. A. 14
  2. B. 15
  3. C. 3
  4. D. 29

Correct answer: B. 15

The odd values are 3, 5 and 7, summing to 15. Summing the even values gives 14, counting the odd values gives 3, and the total of the array is 29.

More Pseudocode and Code Tracing questions

Exams that ask this topic