ProgrammingVery hardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode? (^ is bitwise XOR)
What will be the output of the following pseudocode? (^ is bitwise XOR)
Integer x = 0
for (each i from 1 to 10)
x = x ^ i
end for
Integer y = 0
for (each i from 1 to 12)
y = y ^ i
end for
Print x, y- A. 1 0
- B. 11 13
- C. 11 12
- D. 10 12
Correct answer: C. 11 12
XOR of 1..n follows a cycle of 4: it equals n, 1, n + 1, 0 for n ≡ 0, 1, 2, 3 (mod 4). n = 10 ≡ 2 gives 11 and n = 12 ≡ 0 gives 12. Output 11 12.
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, >> is right shift) Integer x = 21, z = 22 Integer y = x ^ (x >>…
- What will be the output of the following pseudocode? (code(c) is the ASCII code: 'A' = 65, 'a' = 97) Function cmp(String a, String b)…
- What will be the output of the following pseudocode? Integer a[8] = {3, 1, 4, 1, 5, 9, 2, 6} Integer best = a[0], c = 1 for (each i from 1…
- What will be the output of the following pseudocode? Integer a[6] = {100, 4, 200, 1, 3, 2} Set s = all values of a Integer best = 0, len…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer m[4][4] = {{1, 4, 7, 11}, {2, 5, 8, 12}, {3, 6…
- What will be the output of the following pseudocode? Integer a[7] = {4, 5, 2, 10, 8, 6, 9} Stack st = empty for (each i from 0 to 6) while…