ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (finding the 1-based position of the rightmost set bit)? (All variables are integers, so / is integer division.)
What will be the output of the following pseudocode (finding the 1-based position of the rightmost set bit)? (All variables are integers, so / is integer division.)
Function rightmostSetBit(Integer n)
Integer pos = 0
while (n mod 2 == 0)
n = n / 2
pos = pos + 1
end while
return pos + 1
End Function
Print rightmostSetBit(18), rightmostSetBit(12)- A. 2 3
- B. 1 2
- C. 3 2
- D. 2 2
Correct answer: A. 2 3
18 = 10010 in binary; its rightmost set bit is at position 2 (counting from 1, LSB side). 12 = 1100 in binary; its rightmost set bit is at position 3.
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 (checking if s3 is an interleaving of s1 and s2, via dynamic programming)? Function…
- What will be the output of the following pseudocode (counting pairs (i,j) with i<=j<=n whose product is divisible by n)? Integer n = 12, c…
- What will be the output of the following pseudocode (checking whether all characters in a string are unique)? Function…
- What will be the output of the following pseudocode (Hamming distance: number of differing bits between two integers)? (^ is bitwise XOR…
- What will be the output of the following pseudocode (finding the minimum in a rotated sorted array via binary search)? (Arrays are indexed…
- What will be the output of the following pseudocode (moving all zeros to the end while preserving the relative order of non-zero elements…