ProgrammingHardCapgemini · Infosys SP
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 <= 1)
return 1
end if
return f(n / 2) + f(n / 2)
End Function
Print f(10), f(16)- A. 8 16
- B. 10 16
- C. 16 16
- D. 8 32
Correct answer: A. 8 16
f(n) = 2 f(n/2) with f(1) = 1, so f(n) is the largest power of 2 not above n when halving: f(10) = 2 f(5) = 4 f(2) = 8 f(1) = 8, and f(16) = 16. Output 8 16.
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 calls = 0 Procedure f(Integer n) calls = calls + 1 for (each i from 1 to n…
- What will be the output of the following pseudocode? Integer t[0..10] t[0] = 0 t[1] = 1 t[2] = 1 for (each i from 3 to 10) t[i] = t[i - 1]…
- What will be the output of the following pseudocode? (strings are indexed from 0) String s = "{[()]}([)]" Stack st = empty Integer bad =…
- What will be the output of the following pseudocode? Queue q = {1, 2, 3, 4, 5} Integer k = 3 Stack st = empty for (each i from 1 to k)…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) // numbers stored as digit…
- What will be the output of the following pseudocode? // circular list of 6 people: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> back to 1 Node p = node 1…