ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function countdownSum(Integer n)
if (n == 0)
return 0
end if
return n + countdownSum(n - 1)
End Function
Print countdownSum(100)- A. 4950
- B. 5050
- C. 10000
- D. 5000
Correct answer: B. 5050
countdownSum(100) recursively unwinds to 100+99+...+1+0, which is the sum of the first 100 positive integers: 100*101/2 = 5050.
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? (All variables are integers, so / is integer division.) Integer n = 16, c = 0, i = n…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 1024, c = 0 while…
- What is the time complexity of the following pseudocode, in terms of n, where the array has n elements? for (each i from 0 to n - 1) for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[6] = {5, 2, 9, 1, 5, 6} for (each i from 1…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[5] = {2, 4, 6, 8, 10} Integer prefix[6]…
- What will be the output of the following pseudocode? Integer moves = 0 Function hanoi(Integer n) if (n == 0) return end if hanoi(n - 1)…