ProgrammingModerateInfosys · Wipro pattern
What will be the output of the following pseudocode? (/ is integer division.)
What will be the output of the following pseudocode? (/ is integer division.)
Function f(Integer n)
if (n == 0)
return 0
end if
return (n mod 2) + 10 * f(n / 2)
End Function
Print f(13)- A. 1011
- B. 13
- C. 1101
- D. 111
Correct answer: C. 1101
The last binary digit (n mod 2) becomes the units digit and the rest is shifted one decimal place left: f(13) = 1 + 10·f(6), f(6) = 0 + 10·f(3), f(3) = 1 + 10·f(1), f(1) = 1. So f(13) = 1101, the binary form of 13 written as a decimal number. 1011 is the reversed digit order.
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 integer division.) Integer n = 25, z = 0 while (n > 0) n = n / 5 z = z + n end…
- What will be the output of the following pseudocode? Integer s = 0 for (i = 1 to 30) if (i mod 4 == 0) continue end if if (i mod 7 == 0)…
- What will be the output of the following pseudocode? (Byte variables are 8-bit unsigned: they hold 0–255 and arithmetic wraps modulo 256.)…
- What will be the output of the following pseudocode? (Strings are indexed from 0; swap exchanges two characters.) String s = "ABCDEF"…
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1 to 5) for (j = i to 5) if ((i + j) mod 3 == 0) c = c + 1 end…
- What will be the output of the following pseudocode? (/ is integer division; calls counts every call of f.) Integer calls = 0 Function…