ITPEC FE Subject B October 2025 Question 15
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.
Inverse document frequency (IDF) is a metric used to evaluate the importance of a word in a document relative to a document collection. The function calcIDF receives a string term and an array of strings corpus as arguments, and calculates and returns the IDF score for the term based on the given corpus. The corpus of documents corpus is given as a string array. An example of corpus that indicates 3 documents is as follows:
The table shows the description of the functions used in the program.
Table Functions
Traditionally, IDF is computed using the following formula:
IDF(t) = log ( N1 + df(t) )
Where N is the total number of documents, t refers to a specific term or word in a document, and df(t) is the number of documents containing the term t.
For example, the return value of calcIDF( is log(3 ÷ (1 + 2)) = 0 when array corpus is assigned as shown in the above example. Because the total number of documents is 3, and the total number of documents that include the word “ITPEC” is 2.
Answer group
Explanation
A: words[j] = term, B: isContainsTerm = true — flag a document as soon as one of its words equals the term, then count each flagged document once.
df(t) counts documents, not occurrences. For document i, words holds its words and loop α walks words[1] to words[, so the comparison inside it is words[j] = term. On a match the flag is set and the loop is left, so a document that repeats the term is still counted once.
After α, termCount must grow only for documents that contain the term, that is when isContainsTerm = true. The flag is reset to false at the start of every document, so it cannot carry over from one document to the next.
Check with the example: "ITPEC" is a word of documents 1 and 3 but not 2, so termCount = 2 and idf = log(3 ÷ (1 + 2)) = log 1 = 0, as the question states.
Answer (f)
Why not others:
- •(a), (b), (c) and (d) —
corpus[j]is a whole document, not a word, andjruns up to the word count (6 to 8 in the example) whilecorpushas only 3 elements, so it compares the wrong thing and reads past the end - •(e) — counts the documents that do not contain the term: 1 for "ITPEC", giving log(3 ÷ 2) instead of 0
- •(g) —
words[j] ≠ termsets the flag at the first word that differs, which is almost every document; B then counts only documents made entirely of the term, 0 here, giving log 3 - •(h) — the same inverted test counted the other way: every document has a word other than "ITPEC", so
termCount= 3 and the result is log(3 ÷ 4)
Key rule: To count items that contain something, set a per-item flag inside the inner loop, leave the loop on the first hit, and count the flag once after the loop.
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.