ProgrammingHardWipro · TCS NQT pattern
What will be the output of the following pseudocode? (One pass = one full sweep of the outer loop; indexed from 0.)
What will be the output of the following pseudocode? (One pass = one full sweep of the outer loop; indexed from 0.)
Integer arr[6] = {49, 58, 65, 24, 88, 83}
for (each p from 0 to 0)
for (each i from 0 to 4 - p)
if (arr[i] > arr[i + 1])
Integer t = arr[i]
arr[i] = arr[i + 1]
arr[i + 1] = t
end if
end for
end for
for (each i from 0 to 5)
Print arr[i]
end for- A. 49, 58, 65, 24, 88, 83
- B. 24, 49, 58, 65, 83, 88
- C. 49, 58, 24, 65, 83, 88
- D. 83, 88, 24, 65, 58, 49
Correct answer: C. 49, 58, 24, 65, 83, 88
This is 1 pass of bubble sort (each pass bubbles the next-largest unsorted element toward the end, with the inner bound shrinking by 1 per pass). After 1 pass starting from [49, 58, 65, 24, 88, 83], the array is [49, 58, 24, 65, 83, 88] — not fully sorted like [24, 49, 58, 65, 83, 88], since only 1 sweep ran.
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? (All variables are integers, so / is integer division. Array is sorted and indexed…
- What is the final content of the stack (bottom to top, left to right) after this sequence of operations on an initially empty stack? push…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division. Array is sorted and indexed…
- What is the final content of the stack (bottom to top, left to right) after this sequence of operations on an initially empty stack? push…
- What is the final content of the queue (front to back, left to right) after this sequence of operations on an initially empty queue?…
- Consider this naive recursive Fibonacci. How many TOTAL calls to fib (including the initial call) happen when computing fib(5)? Function…