ProgrammingVery hardAccenture · Cognizant 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 f(Integer n, Integer acc)
if (n == 0)
return acc
end if
return f(n / 10, acc + (n mod 10) * (n mod 10))
end Function
Print f(64387, 0)- A. 28
- B. 181
- C. 174
- D. 168
Correct answer: C. 174
Each call strips the last digit and adds its SQUARE to the accumulator: digits of 64387 are 6, 4, 3, 8, 7, squares 36, 16, 9, 64, 49, sum = 174. Adding the digits instead of their squares gives the trap 28.
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? (& is bitwise AND, >> is right shift.) Integer n = 3887, c = 0 while (n > 0) c = c +…
- What will be the output of the following pseudocode? (One pass = one full sweep of the outer loop; indexed from 0.) Integer arr[6] = {49…
- What will be the output of the following pseudocode? (One pass = one full sweep of the outer loop; indexed from 0.) Integer arr[8] = {61…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division. Array is sorted and indexed…
- What is the final content of the queue (front to back, left to right) after this sequence of operations on an initially empty queue?…
- What is the tightest time complexity of the following pseudocode, in terms of n? (Treat any constant like 3 as a fixed constant, not…