ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode (Hamming distance: number of differing bits between two integers)? (^ is bitwise XOR, & is bitwise AND.)
What will be the output of the following pseudocode (Hamming distance: number of differing bits between two integers)? (^ is bitwise XOR, & is bitwise AND.)
Function hamming(Integer a, Integer b)
Integer x = a xor b
Integer c = 0
while (x != 0)
c = c + (x & 1)
x = x >> 1
end while
return c
End Function
Print hamming(7, 10)- A. 2
- B. 3
- C. 4
- D. 1
Correct answer: B. 3
7=0111, 10=1010. XOR gives 1101, which has three 1-bits, so the Hamming distance is 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 (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…
- What will be the output of the following pseudocode (length of the longest substring without repeating characters, sliding window)?…
- What will be the output of the following pseudocode (number of bits needed to represent n in binary)? Function bitsNeeded(Integer n)…
- 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)…