ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (number of bits needed to represent n in binary)?
What will be the output of the following pseudocode (number of bits needed to represent n in binary)?
Function bitsNeeded(Integer n)
Integer c = 0
while (n > 0)
n = n >> 1
c = c + 1
end while
return c
End Function
Print bitsNeeded(37)- A. 5
- B. 6
- C. 7
- D. 37
Correct answer: B. 6
37 in binary is 100101, which is 6 bits long; right-shifting 37 to 0 takes exactly 6 shifts.
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 (counting trailing zero bits in the binary representation of n)? (All variables are…
- What will be the output of the following pseudocode (recursively reversing a list)? Function reverseList(List lst) if (length(lst) <= 1)…
- What will be the output of the following pseudocode (counting set bits for every number from 0 to n using a DP recurrence)? (>> is right…
- What will be the output of the following pseudocode (reversing the order of words in a sentence)? String s = "the sky is blue" List words…
- What will be the output of the following pseudocode (number of ways to make change for an amount using given coin denominations, counting…
- What will be the output of the following pseudocode (length of the longest common SUBSTRING, i.e. contiguous, not subsequence)? Function…