ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[] = {4, 7, 1, 9, 3}
for (i = 0 to 3)
if (a[i] > a[i + 1])
swap(a[i], a[i + 1])
end if
end for
Print a- A. 1 3 4 7 9
- B. 4 1 7 3 9
- C. 4 1 7 9 3
- D. 1 4 7 3 9
Correct answer: B. 4 1 7 3 9
This is a single bubble-sort pass: it moves the largest value (9) to the end but does not sort the rest. 7 > 1 swaps, 7 < 9 no swap, 9 > 3 swaps → 4 1 7 3 9.
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 n = 5, s = 0 for (i = 1 to n) for (j = 1 to i) if ((i + j) mod 2 == 0) s = s…
- What will be the output of the following pseudocode? Integer v = 1 for (k = 0 to 4) v = v 2 + k end for Print v
- What will be the output of the following pseudocode? Integer calls = 0 Integer Function f(Integer n) calls = calls + 1 if (n <= 2) return…
- What will be the output of the following pseudocode? Integer a = 5, b, c, d b = a++ // post-increment c = ++a // pre-increment d = a + b +…
- What will be the output of the following pseudocode? Integer a[5] = {2, 4, 6, 8, 10} for (i = 1 to 4) a[i] = a[i] + a[i - 1] end for Print a
- What will be the output of the following pseudocode? Integer x = 1 for (i = 1 to 4) if (i mod 2 == 1) x = x i else x = x + i end if end…