ProgrammingVery hardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift)
What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift)
Integer a = 13, b = 11, carry, rounds = 0
while (b != 0)
carry = (a & b) << 1
a = a ^ b
b = carry
rounds = rounds + 1
end while
Print a, rounds- A. 24 2
- B. 24 4
- C. 6 4
- D. 24 3
Correct answer: B. 24 4
XOR adds without carries and (a & b) << 1 is the carry. (13, 11) → (6, 18) → (20, 4) → (16, 8) → (24, 0): the sum 24 appears after 4 rounds.
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? ((b & 1) is evaluated before == ; & is AND, << and >> are shifts) Integer a = 13, b =…
- What will be the output of the following pseudocode? (^ is XOR, >> is right shift) Integer s = 0, g for (each i from 0 to 7) g = i ^ (i >>…
- What will be the output of the following pseudocode? Function possible(Integer out[5]) Stack st = empty Integer next = 1 for (each i from…
- What will be the output of the following pseudocode? Integer coins[4] = {9, 6, 5, 1} // sorted largest first Integer amt = 11, used = 0…
- What will be the output of the following pseudocode? Integer a[8] = {10, 9, 2, 5, 3, 7, 101, 18} Integer L[8], best = 0 for (each i from 0…
- What will be the output of the following pseudocode? (strings are indexed from 0) String x = "SUNDAY", y = "SATURDAY" Integer…