ProgrammingHardTCS NQT · Infosys · Accenture
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer A[5] = {5, 1, 4, 2, 8}, t
for (each j from 0 to 3)
if (A[j] > A[j + 1])
swap A[j] and A[j + 1]
end if
end for
Print A[0], A[1], A[2], A[3], A[4]- A. 1 2 4 5 8
- B. 1 4 2 5 8
- C. 1 4 2 8 5
- D. 5 1 4 2 8
Correct answer: B. 1 4 2 5 8
One bubble pass: 5 moves right past 1, 4, 2 and stops before 8, giving 1 4 2 5 8. A single pass does not fully sort the array.
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] = {64, 25, 12, 22, 11} repeat 2 times, with p = 0, then 1 m = index of…
- What will be the output of the following pseudocode? function change(Integer x) x = x + 10 return x 2 end function Integer a = 4, b b =…
- What will be the output of the following pseudocode? function triple(ref Integer x) // pass by reference x = x 3 end function Integer a =…
- What will be the output of the following pseudocode? Integer M[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, s = 0 for (each i from 0 to 2) s…
- What will be the output of the following pseudocode? Integer i = 10, c = 0 do i = i - 4 c = c + 1 while (i > 0) Print i, c
- What will be the output of the following pseudocode? function g(Integer n) if (n > 0) g(n - 1) Print n g(n - 2) end if end function g(3)