Programming Basics (Pseudocode Tracing)

The following for loop increases i by 2. What is the value of s after it runs?

integer: s ← 0
for (i from 1 to 10 step 2)
  s ← s + i
endfor
  1. A25
  2. B30
  3. C36
Show answer and explanation

Answer: A. 25

i takes the values 1, 3, 5, 7 and 9; the next value 11 exceeds the final value 10, so the loop ends. s = 1 + 3 + 5 + 7 + 9 = 25. A for loop exits as soon as the control variable exceeds the final value, so the body never runs with a value beyond it.

  • A. Correct. 1 + 3 + 5 + 7 + 9 = 25.
  • B. This starts at 2: 2 + 4 + 6 + 8 + 10. The initial value is 1.
  • C. This adds 11 as well. 11 exceeds the final value 10, so it is not used.
Practice this topic Back to topic

Related calculators

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

More Programming Basics (Pseudocode Tracing) questions

  1. In the following nested loop, the inner loop starts from the outer variable i. What is the value of c after it runs?
  2. The following program has a condition inside a nested loop. What is the value of c after it runs?
  3. The following program was meant to compute the sum of all elements of array a. Array indices start at 1. What is the value of s after it runs?
  4. Array indices start at 1. The following program looks for the largest element. What is the value of m after it runs?
  5. The following program was meant to reverse the elements of array a. Array indices start at 1. What does a contain after it runs?