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)- A. 30
- B. 25
- C. 20
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode (tail-recursive factorial with an accumulator)? Function factAcc(Integer n, Integer…
- What will be the output of the following pseudocode (GCD of an entire list, folding pairwise)? Function gcdList(Array arr) Integer r =…
- What will be the output of the following pseudocode (counting permutations of a string via recursion)? Function countPerms(String s) if…
- What will be the output of the following pseudocode (basic run-length string compression, kept only if actually shorter)? Function…
- What will be the output of the following pseudocode (counting distinct substrings of length 2)? String s = "abab" Set subs = empty set for…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 20, steps = 0…