ProgrammingModerateCapgemini · Infosys
What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings)
Function f(Integer n)
if (n < 3)
return toString(n)
end if
return f(n / 3) + toString(n mod 3)
End Function
Print f(100)- A. 20201
- B. 1201
- C. 10201
- D. 10202
Correct answer: C. 10201
This writes n in base 3, most significant digit first: 100 = 81 + 2 × 9 + 1 = 1·3⁴ + 0·3³ + 2·3² + 0·3 + 1, so the output is 10201.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What is the time complexity of the following pseudocode, as a function of n? i = n while (i > 1) j = i while (j < n) j = j 2 end while i =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer c = 0, i = 1, j while…
- What will be the output of the following pseudocode? (stop ends the program; code(c) gives a letter's character code) String s =…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[8] = {1, 1, 2, 2, 2, 3, 5, 5} Integer k = 1 for…
- What will be the output of the following pseudocode? (strings are indexed from 0) String x = "AGGTAB", y = "GXTXAYB" Integer L[0..6][0..7]…
- What will be the output of the following pseudocode? Integer wt[3] = {2, 3, 4}, val[3] = {3, 5, 6} Integer best[0..6] = all 0 for (each i…