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 f(Integer n)
if (n == 0)
return 0
end if
if (n mod 2 == 1)
return 1 + f(n - 1)
end if
return 1 + f(n / 2)
End Function
Print f(29)- A. 9
- B. 5
- C. 4
- D. 8
Correct answer: D. 8
29 → 28 → 14 → 7 → 6 → 3 → 2 → 1 → 0 takes 8 steps. In general it is (number of bits - 1) + (number of 1-bits) = 4 + 4. Counting only the 1-bits gives 4.
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 a = 3, b b = a a a = b - a b = b + a Print a mod b, b mod a
- How many times is count incremented? Integer n = 4, count = 0 for (each i from 1 to n) for (each j from 1 to i i) if (j mod i == 0) for…
- What will be the output? (&& does not evaluate its right side when the left side is false.) Integer c = 0, s = 0 Function inc() c = c + 1…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- Two sorted arrays a = {1, 4, 7} and b = {2, 3, 8, 9} are merged. How many element comparisons does the pseudocode make? Integer i = 0, j =…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}…