ITPEC FE Subject B October 2025 Question 1

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 sum receives an integer array array with at least two elements and two positive integers k and m (k < m ≤ number of elements in array). It calculates the sum of even elements of the array array whose indices are from k to m.

[Program]
○ integer: sum(integer []: array, integer: k, integer: m)
integer: s ← 0
integer: i ← k
while (i ≤ m)
if (A)
s ← s + array[i]
endif
B
endwhile
return s

Answer group

OptionAB

Explanation

A: array[i] mod 2 = 0, B: i ← i + 1 — the loop walks the indices from k up to m and adds only the even elements.

  • •i starts at k and the loop runs while i ≤ m, so the index must move up by one on every pass to reach past m and stop: B is i ← i + 1.
  • •An element is even when dividing it by 2 leaves no remainder, so A is array[i] mod 2 = 0.

For example, with array = {3, 4, 7, 8, 10}, k = 2 and m = 4, the elements 4, 7 and 8 are visited, 4 and 8 are even, and the function returns 4 + 8 = 12.

Answer (a)

Why not others:

  • •(b) — i ← i - 1 moves the index down from k; since k < m, the condition i ≤ m stays true forever and i soon falls below index 1
  • •(c) — array[i] mod 2 ≠ 0 selects the odd elements, the opposite of the specification
  • •(d) — both mistakes at once: odd elements and a backward-moving index

Key rule: A while loop over a range must move its index toward the exit condition, and evenness is tested with mod 2 = 0.

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.