ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode (summing a range with a step)?

What will be the output of the following pseudocode (summing a range with a step)?
Function sumRange(Integer a, Integer b, Integer step)
    if (a > b)
        return 0
    end if
    return a + sumRange(a + step, b, step)
End Function

Print sumRange(1, 10, 2)
  1. A. 30
  2. B. 25
  3. C. 20
  4. D. 55

Correct answer: B. 25

The recursion visits a=1,3,5,7,9 (each step of 2, stopping once a exceeds 10 at a=11), summing 1+3+5+7+9=25.

More Pseudocode and Code Tracing questions

Exams that ask this topic