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 g(Integer n)
if (n == 0)
return 0
end if
return n mod 3 + 10 * g(n / 3)
End Function
Print g(50)- A. 1221
- B. 2121
- C. 1212
- D. 212
Correct answer: C. 1212
g writes n in base 3, with the digits shown as a decimal number. 50 = 1×27 + 2×9 + 1×3 + 2, which is 1212₃. Reading the remainders in the order they come out (2, 1, 2, 1) gives the trap 2121.
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? (Arrays are indexed from 0.) Integer arr[6] = {5, -2, 3, -1, 4, -6} Integer s = 0, m…
- What will be the output of the following pseudocode? Integer x = 7, y = 3 Print x mod y y + x / y
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[6] = {1, 2, 3, 4, 5, 6} for (each i from 0…
- How many times is fun called in total when fun(6) is evaluated? Function fun(Integer n) if (n <= 2) return 1 end if return fun(n - 1) +…
- What will be the output of the following pseudocode? Integer c = 0, j for (each i from 1 to 10) j = i while (j <= 10) c = c + 1 j = j + i…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…