ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[] = {1, 3, 5, 7, 9, 11, 13}
Integer lo = 0, hi = 6, t = 11, steps = 0, mid
while (lo <= hi)
steps = steps + 1
mid = (lo + hi) / 2
if (a[mid] == t) break
if (a[mid] < t) lo = mid + 1
else hi = mid - 1
end while
Print mid, steps- A. 5 3
- B. 4 2
- C. 5 2
- D. 6 2
Correct answer: C. 5 2
mid = 3 (7 < 11) → lo = 4; mid = (4 + 6)/2 = 5 → a[5] = 11 found. 2 iterations; a sorted array of 7 needs at most 3.
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 x = 15, y = 4, r = 0 while (x >= y) x = x - y r = r + 1 end while Print r, x
- What will be the output of the following pseudocode? Integer a = 1, b = 2, c = 3 a = (a < b) ? b : c b = (b < c AND a == 2) ? c : a c = (a…
- What will be the output of the following pseudocode? Integer Function h(Integer n) if (n <= 0) return 1 if (n mod 2 == 1) return h(n - 1)…
- What will be the output of the following pseudocode? Integer v = 0 for (i = 1 to 5) v = (v << 1) | (i & 1) end for Print v
- What will be the output of the following pseudocode? Integer a[] = {10, 20, 30, 40} Integer i = 0, s = 0 while (i < 4) s = s + a[i] i = i…
- What will be the output of the following pseudocode? (memo starts with every entry equal to −1; calls counts every call, including the…