ProgrammingVery hardTCS NQT · Infosys · Capgemini 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(56785, 0)- A. 31
- B. 195
- C. 199
- D. 204
Correct answer: C. 199
Each call strips the last digit and adds its SQUARE to the accumulator: digits of 56785 are 5, 6, 7, 8, 5, squares 25, 36, 49, 64, 25, sum = 199. Adding the digits instead of their squares gives the trap 31.
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 c = 0 for (each i from 1 to 14) for (each j from i + 1 to 14) if ((i + j) mod…
- What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift.) Integer n = 845, c = 0 while (n > 0) c = c +…
- What is the final content of the stack (bottom to top, left to right) after this sequence of operations on an initially empty stack? push…
- 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 6 as a fixed constant, not…
- What will be the output of the following pseudocode? (Arrays are passed and assigned BY REFERENCE: b = a makes b point to the same array…