ProgrammingVery hardProduct company OA
What will be the output of the following pseudocode? ('ref' parameters are passed by reference; ^ is bitwise XOR; arrays are indexed from 0.)
What will be the output of the following pseudocode? ('ref' parameters are passed by reference; ^ is bitwise XOR; arrays are indexed from 0.)
Procedure swap(ref Integer a, ref Integer b)
a = a ^ b
b = a ^ b
a = a ^ b
End Procedure
Integer arr[3] = {5, 7, 9}
swap(arr[0], arr[2])
swap(arr[1], arr[1])
Print arr[0], arr[1], arr[2]- A. 9 0 5
- B. 9 7 5
- C. 5 0 9
- D. 9 7 9
Correct answer: A. 9 0 5
The first call swaps 5 and 9 normally. In the second call a and b refer to the SAME element: a = a ^ b sets it to 7 ^ 7 = 0, and every later XOR keeps it 0. So the middle element is destroyed: 9 0 5. That is why an XOR swap must check that the two references differ.
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? (Arrays are passed by reference and indexed from 0.) Procedure p(Integer arr[]…
- What will be the output of the following pseudocode? (/ is integer division.) Integer c = 0, i = 1, j while (i < 1000) j = i while (j > 0)…
- What will be the output of the following pseudocode? (/ is integer division.) Integer c = 0 for (n = 1 to 200) Integer s = 0, t = n while…
- What will be the output of the following pseudocode? (isComposite[] starts all false; ops counts every assignment inside the inner loop…
- What will be the output of the following pseudocode? (&& stops as soon as the left side is false) Integer c = 0, k = 0 Function f(Integer…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Procedure show(Integer n) if…