ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (& is bitwise AND on two's-complement integers)
What will be the output of the following pseudocode? (& is bitwise AND on two's-complement integers)
Integer n = 360
while (n > 0)
Print n & (-n)
n = n - (n & (-n))
end while- A. 256 64 32 8
- B. 8 16 32 64
- C. 8 32 64 256
- D. 2 4 8 16
Correct answer: C. 8 32 64 256
In two's complement, n & (−n) keeps only the lowest set bit. 360 = 256 + 64 + 32 + 8, and the loop peels these off from the lowest: 8 32 64 256.
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? (substring(s, i, k) is the k characters starting at index i; indices start at 0)…
- What will be the output of the following pseudocode? Function F(Integer n) if (n == 0) return 1 end if return n - M(F(n - 1)) End Function…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {1, 2, 3, -2, 2, 1}, c = 0, s for (each i…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? (C-style for loop: the update i = i + 1 runs after each pass of the body) Integer c =…
- What will be the output of the following pseudocode? (Strings are indexed from 0) String s = "110100" Integer v = 0, p = 1 for (each i…