ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (>> is right shift, & is AND)
What will be the output of the following pseudocode? (>> is right shift, & is AND)
Integer a[3] = {4, 14, 2}, total = 0
for (each b from 0 to 3)
Integer ones = 0
for (each i from 0 to 2)
ones = ones + ((a[i] >> b) & 1)
end for
total = total + ones * (3 - ones)
end for
Print total- A. 6
- B. 2
- C. 4
- D. 8
Correct answer: A. 6
For each bit position, every (1, 0) pair of numbers differs there, giving ones × zeros pairs. 4 = 0100, 14 = 1110, 2 = 0010: bit 1 → 2 × 1, bit 2 → 2 × 1, bit 3 → 1 × 2. Total 6, the sum of Hamming distances over all pairs (2 + 2 + 2).
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? (| is OR, & is AND, << and >> are shifts) Integer x = 1, r = 0 for (each i from 1 to…
- What will be the output of the following pseudocode? Integer m[3][3] = {{2, 0, 1}, {1, 3, 2}, {1, 1, 1}} Integer det = m[0][0] (m[1][1]…
- What will be the output of the following pseudocode? Integer m[4][4] = {{4, 3, 2, -1}, {3, 2, 1, -1}, {1, 1, -1, -2}, {-1, -1, -2, -3}}…
- What will be the output of the following pseudocode? // list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL Integer n = 5, k = 2 Node tail = last node…
- What will be the output of the following pseudocode? Insert into an empty BST, in this order: 50, 30, 70, 20, 40, 60, 80, 35, 45, 65…
- What will be the output of the following pseudocode? // undirected graph; neighbours are visited in increasing order Edges: 1-2, 1-3, 2-4…