ITPEC FE Subject B October 2025 Question 16
From the answer group below, select the correct combination of answers to be inserted into blank in the program. Here, the array index starts at 0.
The Jaccard similarity index, J, measures the similarity between two sets by computing the ratio of the number of elements in their intersection to the number of elements in their union, i.e., J(A, B) = |A∩B| / |A∪B|, where A and B represent sets, ∩ and ∪ represent the intersection and the union set operations, respectively, and |…| is the operator to calculate the number of elements in a set. The intersection operation builds a new set taking members that are common to both sets; while the union operation builds a set taking all members of both sets, where each member will appear only once. For instance, A = {3, 4, 6, 7} and B = {2, 4, 5}, then A∩B = {4}, A ∪ B = {2, 3, 4, 5, 6, 7}, |A| = 4, | B| = 3, |A∩B| = 1, and |A∪B| = 6.
The function jaccardSimilarity calculates the Jaccard similarity stated above and returns it. The definitions of variables used in the function are stated in the below table.
Table Variable definitions
Answer group
Explanation
nB — start the union count at the size of B, then add each element of A that has no match in B.
The outer loop visits every element of A. An element found in B adds 1 to iCount; an element not found in B adds 1 to uCount. After the loop uCount = start + (nA - iCount), and it must equal |A∪B| = |A| + |B| - |A∩B| = nA + nB - iCount. So the start value is nB: all of B, plus the elements of A that B lacks.
Check with the example A = {3, 4, 6, 7}, B = {2, 4, 5}: only 4 is found, so iCount = 1, while 3, 6 and 7 each add 1, so uCount = 3 + 3 = 6. The result is 1 ÷ 6, matching |A∩B| = 1 and |A∪B| = 6.
Answer (c)
Why not others:
- •(a) 0 — counts only the elements of A missing from B, 3 here, giving 1 ÷ 3; the elements of B never enter the union
- •(b) nA — counts A where B belongs: 4 + 3 = 7, giving 1 ÷ 7
- •(d) nA + nB — already counts the intersection twice before the loop adds A's unmatched elements again: 7 + 3 = 10
- •(e) nA - nB — a difference of set sizes with no meaning as a count: 1 + 3 = 4
- •(f) nA ÷ nB and (g) nA × nB — a ratio or product of set sizes is not an element count: 4 ÷ 3 is not even an integer, and 12 + 3 = 15 is far above the 6 elements of the union
Key rule: When a loop builds a union count element by element, use inclusion–exclusion, |A∪B| = |A| + |B| - |A∩B|, to work out what the counter must start at.
Math & Data Science · 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.