ProgrammingModerateAmazon OA · TCS NQT
What will be the output of the following pseudocode?
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)
swap(a[k], a[i])
k = k + 1
end if
end for
Print a[0], a[1], a[2], a[3], a[4]- A. 1 3 0 12 0
- B. 12 3 1 0 0
- C. 1 3 12 0 0
- D. 0 0 1 3 12
Correct answer: C. 1 3 12 0 0
Each non-zero value is swapped into the next front slot, so the non-zeros keep their order and the zeros drift to the end: 1 3 12 0 0.
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 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 =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function f(Integer n) if (n <=…