ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? ((mask >> i) & 1 is evaluated before ==; ^ is XOR)
What will be the output of the following pseudocode? ((mask >> i) & 1 is evaluated before ==; ^ is XOR)
Integer a[3] = {5, 1, 6}, total = 0
for (each mask from 0 to 7)
Integer x = 0
for (each i from 0 to 2)
if ((mask >> i) & 1 == 1)
x = x ^ a[i]
end if
end for
total = total + x
end for
Print total- A. 24
- B. 28
- C. 12
- D. 48
Correct answer: B. 28
total adds the XOR of every subset. A bit contributes 2^(bit) × 2ⁿ⁻¹ whenever at least one element has it: the OR is 5 | 1 | 6 = 7, so total = 7 × 2² = 28.
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? (+ joins strings) String s = "PAYPALISHIRING" Integer rows = 3, r = 0, step = 1…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[5] = {1, 3, 5, 4, 2}, n = 5 Integer i = n - 2…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[9] = {3, 2, 3, 1, 2, 4, 5, 5, 6}, k = 4 sort a…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function canSplit(Integer a[]…
- What will be the output of the following pseudocode? Integer g[3][3] = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? Function rob(Integer a[], Integer lo, Integer hi) Integer inc = 0, exc = 0, t for…