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- A. 2
- B. 3
- C. 4
- D. 5
Correct answer: B. 3
mid = 7 (45), then mid = 3 (12), mid = 5 (23): 3 probes.
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[4][4], i, j, s = 0 for (i = 0; i < 4; i = i + 1) for (j = 0; j < 4; j = j +…
- What will be the output of the following pseudocode? Integer calls = 0 Function F(Integer n) calls = calls + 1 if (n < 2) return n end if…
- What will be the output of the following pseudocode? List L = [1, 2, 3, 4, 5, 6, 7] Integer idx = 0, k = 3 while (size(L) > 1) idx = (idx…
- What will be the output of the following pseudocode? Function gcd(Integer a, Integer b) while (b != 0) t = a mod b a = b b = t end while…
- What will be the output of the following pseudocode? String s = "{[()]}(()" Stack st Integer pops = 0, i Boolean ok = true for (i = 0; i <…
- What will be the output of the following pseudocode? Integer a[5] = {2, 5, 9, 14, 20}, i for (i = 1; i < 5; i = i + 1) a[i] = a[i] - a[i…