ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode? (Arrays are indexed from 0.)

What will be the output of the following pseudocode? (Arrays are indexed from 0.)
Integer arr[6] = {4, 8, 15, 16, 23, 42}
Integer target = 23, steps = 0, found = -1
for (each i from 0 to length(arr) - 1)
    steps = steps + 1
    if (arr[i] == target)
        found = i
        break
    end if
end for
Print found, steps
  1. A. 4 4
  2. B. 4 5
  3. C. 3 4
  4. D. 4 6

Correct answer: B. 4 5

Linear search scans from index 0; it checks indices 0,1,2,3 (values 4,8,15,16, none matching) before finding 23 at index 4 on the 5th check, so found=4 and steps=5.

More Pseudocode and Code Tracing questions

Exams that ask this topic