ITPEC FE Subject B April 2026 Question 7

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

The function linearSearch linearly searches the global array D using the recursive function recursive, and if an element with the same value as the argument searchKey exists, it returns that element index, otherwise -1 is returned. Assume that the array D has no duplicate elements.

[Program]
global: integer []: D ← {3, 4, 2, 5, 9}
global: integer: N ← the number of elements of D
○ integer: linearSearch(integer: searchKey)
return recursive(searchKey, N)
○ integer: recursive(integer: searchKey, integer: index)
if (index < 1)
return -1
elseif (D[index] = searchKey)
return A
else
return recursive(searchKey, B)
endif

Answer group

OptionAB

Explanation

A: index, B: index - 1 — the recursion scans D from the last element down to the first, and on a match the current position is the answer.

  • •linearSearch starts the recursion with index = N = 5
  • •index < 1 means every element was checked without a hit, so −1 is returned
  • •a match at D[index] returns the position itself: A is index (printed Index in the answer group)
  • •otherwise the search moves one place left: B is index - 1

Trace for linearSearch(2) with D = {3, 4, 2, 5, 9}: D[5] is 9, D[4] is 5, D[3] is 2 and matches, so 3 is returned. For a missing key the calls reach index = 0 and return −1.

Answer (d)

Why not others:

  • •(a), (b) and (c) — return the constant 1 on a match, reporting position 1 wherever the value was found
  • •(g), (h) and (i) — return index - 1, one position before the element that matched
  • •(b), (e) and (h) — recurse with N - 1 every time, so only positions 5 and 4 are ever checked, and any other key recurses forever without reaching the base case
  • •(c), (f) and (i) — recurse with N - index: from 5 that is 0, so only D[5] is checked and every other key returns −1

Key rule: A recursive scan must pass a value derived from its own parameter, so that each call moves one step closer to the base case; a value built from a constant alone never moves.

Array Manipulation · AI-written, may contain errors

This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.