ProgrammingHardLTIMindtree · Cognizant GenC

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[15] = {2, 5, 8, 12, 16, 23, 38, 45, 56, 72, 91, 99, 101, 110, 120}
Integer lo = 0, hi = 14, mid, c = 0, key = 23
while (lo <= hi)
    mid = (lo + hi) / 2
    c = c + 1
    if (a[mid] == key)
        break
    end if
    if (a[mid] < key)
        lo = mid + 1
    else
        hi = mid - 1
    end if
end while
Print c
  1. A. 2
  2. B. 3
  3. C. 4
  4. D. 5

Correct answer: B. 3

mid = 7 (45), then mid = 3 (12), mid = 5 (23): 3 probes.

More Pseudocode and Code Tracing questions

Exams that ask this topic