ProgrammingHardCapgemini · Infosys pattern
The array holds {1, 3, 5, 7, 9, 11, 13, 15, 17} (indexed from 0). What is printed? (/ is integer division.)
The array holds {1, 3, 5, 7, 9, 11, 13, 15, 17} (indexed from 0). What is printed? (/ is integer division.)
Integer low = 0, high = 8, mid, steps = 0, key = 4
while (low <= high)
mid = (low + high) / 2
steps = steps + 1
if (arr[mid] == key)
break
else if (arr[mid] < key)
low = mid + 1
else
high = mid - 1
end if
end while
Print steps, low- A. 4 2
- B. 3 2
- C. 3 1
- D. 2 2
Correct answer: B. 3 2
mid = 4 (9 > 4) so high = 3. mid = 1 (3 < 4) so low = 2. mid = 2 (5 > 4) so high = 1. Now low > high and the search fails after 3 steps. low = 2 is where 4 would be inserted.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- How many swaps does this pseudocode perform? (Arrays are indexed from 0.) Integer arr[5] = {5, 1, 4, 2, 8} Integer swaps = 0 for (each i…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function f(Integer n) if (n…
- What will be the output of the following pseudocode? Integer a = 3, b b = a a a = b - a b = b + a Print a mod b, b mod a
- How many times is count incremented? Integer n = 4, count = 0 for (each i from 1 to n) for (each j from 1 to i i) if (j mod i == 0) for…
- What will be the output? (&& does not evaluate its right side when the left side is false.) Integer c = 0, s = 0 Function inc() c = c + 1…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…