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.
Answer group
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.
- •
istarts atkand the loop runs whilei ≤ m, so the index must move up by one on every pass to reach pastmand stop: B isi ← 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 - 1moves the index down fromk; sincek < m, the conditioni ≤ mstays true forever andisoon falls below index 1 - •(c) —
array[i] mod 2 ≠ 0selects 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.