ProgrammingVery hardGoldman Sachs OA · Infosys SP
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 c = 0
for (each i from 0 to 20)
if ((i & (i - 1)) == 0)
c = c + 1
end if
end for
Print c- A. 5
- B. 6
- C. 4
- D. 21
Correct answer: B. 6
i & (i − 1) clears the lowest 1-bit, so it is 0 for powers of two: 1, 2, 4, 8, 16. But it is also 0 for i = 0 (0 & −1 = 0), so the 'power of two' test wrongly accepts 0: c = 6.
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 XOR, & is AND; x & (−x) keeps the lowest 1-bit) Integer a[6] = {2, 4, 7, 9, 2…
- What will be the output of the following pseudocode? String w[3] = {"flower", "flow", "flight"} String p = w[0] for (each i from 1 to 2)…
- What will be the output of the following pseudocode? (substring(s, i, 2) is the 2 characters from index i; sorted(t) puts t's letters in…
- What will be the output of the following pseudocode? (strings are indexed from 0) String p = "ABABCABAB" Integer f[9], k = 0 f[0] = 0 for…
- What will be the output of the following pseudocode? Integer a[4] = {1, 2, 3, 4}, res[4], left = 1, right = 1 for (each i from 0 to 3)…
- What will be the output of the following pseudocode? Integer a[5] = {2, 3, -2, 4, -1} Integer mx = a[0], mn = a[0], best = a[0], t for…