ProgrammingHardCapgemini · Infosys pattern
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 = 98, y = 21 x = x ^ y y = x ^ y x = x ^ y Print x, y
- A. 98 21
- B. 21 98
- C. 21 99
- D. 21 21
Correct answer: B. 21 98
This is the classic XOR swap: after the three XOR steps, x and y end up swapped without a temp variable, so x = 21 (originally y) and y = 98 (originally x). Thinking XOR leaves them unchanged gives the trap "98 21".
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? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…
- What will be the output of the following pseudocode? Function isEven(Integer n) if (n == 0) return true end if return isOdd(n - 1) end…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer x, y Set x = 17, y =…
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 10) if (i mod 2 == 0) s = s + i end if end for…
- What will be the output of the following pseudocode? Integer a = 6, b = 4 a = a + b b = a - b a = a - b Print a, b
- What will be the output of the following pseudocode? Integer n = 5, f = 1 while (n > 1) f = f n n = n - 1 end while Print f