ProgrammingVery hardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode? (Arrays are 0-indexed.)
What will be the output of the following pseudocode? (Arrays are 0-indexed.)
Integer a[7] = {5, 1, 4, 2, 8, 0, 3}
Integer swaps = 0, passes = 0
Boolean swapped
Repeat
swapped = false
For i = 0 to 5 - passes
If a[i] > a[i + 1] Then
Swap a[i], a[i + 1]
swaps = swaps + 1
swapped = true
End If
End For
passes = passes + 1
Until swapped = false
Print passes, swaps- A. 5 12
- B. 6 13
- C. 6 12
- D. 7 12
Correct answer: C. 6 12
Each adjacent swap removes exactly one inversion, and the array has 12 inversions, so swaps = 12. The array becomes sorted during pass 5, and pass 6 finds nothing to swap, which ends the loop: passes = 6.
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? (Static scoping: a name used inside a procedure refers to the variable visible where…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[7] = {1, 2, 4, 5, 7, 9, 10} Integer i = 0, j = 6, c…
- What will be the output of the following pseudocode? (Division is integer division; arrays are 0-indexed.) Integer a[9] = {2, 3, 3, 3, 5…
- What will be the output of the following pseudocode? (& is bitwise AND; arrays are 0-indexed.) Integer m[4][4] For i = 0 to 3 For j = 0 to…
- What will be the output of the following pseudocode? Function J(n, k) If n = 1 Then Return 0 Return (J(n - 1, k) + k) mod n End Function…
- What will be the output of the following pseudocode? Integer n = 1, s = 0 Repeat s = s + n n = n 2 + 1 Until s >= 100 Print n, s