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:

{"ITPEC includes members from 6 countries",
"many students prepare for the exam",
"The ITPEC exam is essential for IT professionals"}

The table shows the description of the functions used in the program.

Table Functions

FunctionReturn valueDescription
split(string: str)string []Returns words separated by a space in the text str.
log(real: x)realReturns natural logarithm (base e) of the value x.

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("ITPEC", corpus) 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.

[Program]
○ real: calcIDF(string: term, string []: corpus)
integer: i, j, numDocs, numWords, termCount
boolean: isContainsTerm
real: idf
string []: words
termCount ← 0
numDocs ← the number of elements in corpus
for (increase i from 1 to numDocs by 1)
isContainsTerm ← false
words ← split(corpus[i])
numWords ← the number of elements in words
for (increase j from 1 to numWords by 1) // α
if (A)
isContainsTerm ← true
exit the for block marked α
endif
endfor
if (B)
termCount ← termCount + 1
endif
endfor
idf ← log(numDocs ÷ (1 + termCount)) /* Division is performed
in data type real */
return idf

Answer group

OptionAB

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[numWords], 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, and j runs up to the word count (6 to 8 in the example) while corpus has 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] ≠ term sets 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.