ProgrammingHardWipro · TCS NQT 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(13323, 0)
  1. A. 35
  2. B. 32
  3. C. 12
  4. D. 30

Correct answer: B. 32

Each call strips the last digit and adds its SQUARE to the accumulator: digits of 13323 are 1, 3, 3, 2, 3, squares 1, 9, 9, 4, 9, sum = 32. Adding the digits instead of their squares gives the trap 12.

More Pseudocode and Code Tracing questions

Exams that ask this topic