ProgrammingHardCapgemini · 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 bin(Integer n)
if (n == 0)
return ""
end if
return bin(n / 2) + toString(n mod 2)
End Function
String b = bin(37)
Print b, length(b), length(bin(0))- A. 100101 6 1
- B. 101001 6 0
- C. 100101 6 0
- D. 100101 5 0
Correct answer: C. 100101 6 0
The recursion writes the binary digits most significant first: 37 = 100101 (6 digits). bin(0) returns the empty string, so its length is 0 — a known gap of this version. Output 100101 6 0.
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? Integer m[3][3] = {{1, 2, 3}, {2, 5, 9}, {4, 6, 0}} Integer bad = 0 for (each i from…
- What will be the output of the following pseudocode? (operands are evaluated strictly left to right) Integer a = 7, b = 3 a = a + b - (b =…
- What will be the output of the following pseudocode? Integer calls = 0 Function C(Integer n, Integer k) calls = calls + 1 if (k == 0 OR k…
- What will be the output of the following pseudocode? // Integer16: 16-bit signed integers in two's complement; results wrap around…
- What will be the output of the following pseudocode? Record r[4] = {(3, 'a'), (1, 'b'), (3, 'c'), (1, 'd')} // (key, letter) for (each i…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[7] = {4, 2, 2, 8, 3, 3, 1}, cnt[0..8] = all 0…