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.
Answer group
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, andcstarts at 1 forarr[i]itself, so A must bearr[i] = arr[j]. - •
m_cholds the frequency of the current modem. The body of B storescinm_candarr[i]inm, so it must run only when the new count is higher: B ism_c < c.
Trace for {2, 1, 1, 9, 6, 6, 2, 5, 6}:
- •
i = 1(value 2):c = 2, and 1 < 2, som = 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, som = 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, somends as the largest ofarr[1]toarr[n-1]: 9 for the example - •(b) — B
m ≠ arr[i]replacesmwhenever the value differs, somends asarr[n-1]: 5 for the example - •(d) — B
m_c > cis never true (m_cstays 1 andcis at least 1), so the function always returnsarr[1]: 2 for the example - •(e), (f), (g) and (h) — A
m = arr[j]counts later copies of the current modem, not of the candidatearr[i], socis not the frequency ofarr[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.