ProgrammingHardTCS NQT · Accenture 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)
if (n < 10)
return n
end if
return f(f(n / 10) + n mod 10)
End Function
Print f(9875)- A. 11
- B. 5
- C. 29
- D. 2
Correct answer: D. 2
f(n) reduces n to a single digit (its digital root). f(98) = f(9 + 8) = f(17) = 8. f(987) = f(8 + 7) = f(15) = 6. f(9875) = f(6 + 5) = f(11) = 2. The plain digit sum 29 is the trap.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- How many lines does this pseudocode print for hanoi(5, 'A', 'B', 'C')? Function hanoi(Integer n, Char from, Char via, Char to) if (n == 0)…
- How many of the following pseudocode statements print 1? (/ is integer division; & is bitwise AND; ^ is bitwise XOR.) (i) Print (7 / 2) 2…
- What will be the output of the following pseudocode? Integer a = 12, 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 s = 0 for (i = 1 to 5) for (j = i to 5) s = s + 1 end for end for Print s
- What will be the output of the following pseudocode? Integer n = 1234, r = 0 while (n > 0) r = r 10 + (n mod 10) n = n / 10 // integer…
- What will be the output of the following pseudocode? Integer x = 5, y = 9 x = x ^ y // ^ is bitwise XOR y = x ^ y x = x ^ y Print x, y, (5…