ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (^ is XOR, & is AND; x & (−x) keeps the lowest 1-bit)
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, 4}
Integer x = 0, low, p = 0, q = 0
for (each i from 0 to 5)
x = x ^ a[i]
end for
low = x & (-x)
for (each i from 0 to 5)
if ((a[i] & low) != 0)
p = p ^ a[i]
else
q = q ^ a[i]
end if
end for
Print x, p, q- A. 0 7 9
- B. 14 7 9
- C. 14 9 7
- D. 16 7 9
Correct answer: B. 14 7 9
Pairs cancel, so x = 7 ^ 9 = 14 (1110). Its lowest set bit is 2, which splits the numbers: those with bit 2 (2, 7, 2) XOR to 7, the rest (4, 9, 4) XOR to 9. Output 14 7 9.
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? 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…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[7] = {-7, 1, 5, 2, -4, 3, 0} Integer total = 0…