ITPEC FE Subject B October 2025 Question 2

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.

In statistics, the mode is the value that occurs most frequently in a dataset. For example, the mode of the integer array {2, 1, 1, 9, 6, 6, 2, 5, 6} is 6, as it occurs most often. The function findMode receives an integer array arr as a dataset and returns the mode for it.

[Program]
○ integer: findMode(integer []: arr)
integer: n ← the number of elements in arr
integer: m ← arr[1] /* Current mode value */
integer: m_c ← 1 /* Frequency count of mode */
integer: c, i, j
for (increase i from 1 to n - 1 by 1)
c ← 1
for (increase j from i + 1 to n by 1)
if (A)
c ← c + 1
endif
endfor
if (B)
m_c ← c
m ← arr[i]
endif
endfor
return m

Answer group

OptionAB

Explanation

A: arr[i] = arr[j], B: m_c < c — count how often each arr[i] occurs from position i on, and make it the mode when that count beats the best so far.

  • •The inner loop compares arr[i] with every later element, and c starts at 1 for arr[i] itself, so A must be arr[i] = arr[j].
  • •m_c holds the frequency of the current mode m. The body of B stores c in m_c and arr[i] in m, so it must run only when the new count is higher: B is m_c < c.

Trace for {2, 1, 1, 9, 6, 6, 2, 5, 6}:

  • •i = 1 (value 2): c = 2, and 1 < 2, so m = 2, m_c = 2
  • •i = 2 (value 1): c = 2, not greater than 2, no change
  • •i = 5 (value 6): c = 3, and 2 < 3, so m = 6, m_c = 3
  • •no later element reaches 3, so the function returns 6

Answer (c)

Why not others:

  • •(a) — B m < arr[i] compares values, not counts, so m ends as the largest of arr[1] to arr[n-1]: 9 for the example
  • •(b) — B m ≠ arr[i] replaces m whenever the value differs, so m ends as arr[n-1]: 5 for the example
  • •(d) — B m_c > c is never true (m_c stays 1 and c is at least 1), so the function always returns arr[1]: 2 for the example
  • •(e), (f), (g) and (h) — A m = arr[j] counts later copies of the current mode m, not of the candidate arr[i], so c is not the frequency of arr[i]; even (g), with the right B, returns 2 for the example

Key rule: Keep counts and values apart: a test that matches elements compares values, while the test for a new best compares counts.

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.