ProgrammingHardAmazon OA · Infosys SP
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[6] = {1, 2, 3, 4, 5, 6}
Integer i = 0, j = 5
while (i < j)
if (a[i] mod 2 == 0)
i = i + 1
else if (a[j] mod 2 == 1)
j = j - 1
else
swap(a[i], a[j])
i = i + 1
j = j - 1
end if
end while
Print a[0], a[1], a[2], a[3], a[4], a[5]- A. 2 4 6 1 3 5
- B. 6 4 2 3 5 1
- C. 2 4 6 5 3 1
- D. 6 2 4 3 5 1
Correct answer: D. 6 2 4 3 5 1
The two-pointer partition moves even numbers to the front by swapping, without keeping order. 1 ↔ 6, then 3 ↔ 4 (2 is already even): {6, 2, 4, 3, 5, 1}. The stable answer 2 4 6 1 3 5 would need a different method.
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? Integer a[5] = {0, 1, 0, 3, 12}, k = 0 for (each i from 0 to 4) if (a[i] != 0)…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {-1, 0, 1, 2, -1, -4} sort a // {-4, -1…
- What will be the output of the following pseudocode? Integer a[3] = {5, -3, 5} Integer total = 0, curMax = 0, bestMax = a[0], curMin = 0…
- What will be the output of the following pseudocode? Integer n = 5, spaces = 0, stars = 0 for (each i from 1 to n) for (each j from 1 to n…
- What will be the output of the following pseudocode? (~ is bitwise NOT, & is AND, | is OR, >> is right shift) Integer x = 11 Integer a =…
- What will be the output of the following pseudocode? (^ is XOR, & is AND) Integer a = 10, b = 20, x, flips = 0 x = a ^ b while (x > 0) x =…