ProgrammingModerateCapgemini · Infosys pattern
What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
Function fun(Integer n)
if (n == 0)
return 0
end if
return n mod 10 + fun(n / 10)
End Function
Print fun(9875)- A. 28
- B. 5789
- C. 2
- D. 29
Correct answer: D. 29
fun adds up the digits recursively: 5 + 7 + 8 + 9 = 29. It does not repeat until one digit is left, so 2 (the digital root) is wrong.
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? Integer a = 48, b = 18, t while (b != 0) t = b b = a mod b a = t end while Print a
- What will be the output of the following pseudocode? Integer p = 2, q = 3 for (each i from 1 to 3) p = p q q = q - 1 end for Print p + q
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[6] = {1, 2, 3, 4, 5, 6} for (each i from 0…
- What will be the output of the following pseudocode? Integer x = 1 while (x < 100) x = x 3 end while Print x
- What will be the output of the following pseudocode? Integer s = 0 for (i = 10; i >= 1; i = i - 3) s = s + i end for Print s
- What will be the output of the following pseudocode? Integer c = 0 for (each d from 1 to 36) if (36 mod d == 0) c = c + 1 end if end for…