ProgrammingHardWipro · Capgemini pattern
What will be the output of the following pseudocode? (/ is integer division; calls counts every call of f.)
What will be the output of the following pseudocode? (/ is integer division; calls counts every call of f.)
Integer calls = 0
Function digsum(Integer n)
Integer s = 0
while (n > 0)
s = s + n mod 10
n = n / 10
end while
return s
End Function
Function f(Integer n)
calls = calls + 1
if (n < 10)
return n
end if
return f(digsum(n))
End Function
Print f(98765), calls- A. 8 3
- B. 35 1
- C. 8 2
- D. 17 3
Correct answer: A. 8 3
98765 → digit sum 35 → digit sum 8 (single digit, so it is returned). f is called with 98765, 35 and 8: 3 calls. The result is the digital root, which also equals 98765 mod 9 = 8. '8 2' forgets the final call that returns 8.
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? (Arrays are indexed from 0.) Integer a[5] = {1, 2, 3, 4, 5}, t for (i = 0 to 4) t =…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer a[5] = {5, 1, 8, 2, 3}, c = 0 for (i = 0 to 3)…
- What will be the output of the following pseudocode? (A static variable is initialised once and keeps its value between calls; operands…
- What will be the output of the following pseudocode? (^ is bitwise XOR, & is AND, >> is a right shift.) Integer x = 29, y = 30 Integer gx…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; the matrix has 3 rows and 4 columns.) Integer m[3][4] =…
- What will be the output of the following pseudocode? (calls counts every call of f, including the first.) Integer calls = 0 Function…