ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (counting trailing zero bits in the binary representation of n)? (All variables are integers, so / is integer division.)
What will be the output of the following pseudocode (counting trailing zero bits in the binary representation of n)? (All variables are integers, so / is integer division.)
Function trailingZeroBits(Integer n)
Integer c = 0
while (n mod 2 == 0 and n != 0)
n = n / 2
c = c + 1
end while
return c
End Function
Print trailingZeroBits(40)- A. 2
- B. 3
- C. 4
- D. 5
Correct answer: B. 3
40 in binary is 101000, which has 3 trailing zero bits (dividing by 2 three times: 40->20->10->5, where 5 is odd).
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 (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…
- What will be the output of the following pseudocode (counting distinct ordered character pairs (s[i], s[j]) with i<j)? (Strings are…