ITPEC FE Subject B April 2026 Question 13
From the answer group below, select the correct answer to be inserted into blank in the program. Here, the array indexes start at 1.
The function extractEmails receives text as an argument, extracts email addresses from the text, and returns all collected email addresses. Here, a string in the format “name@domain” is considered to be an email address, where “name” and “domain” comprise only alphanumeric characters and the characters “.” (dot), “_” (underscore), and “-” (hyphen-minus). The table shows the description of the functions used in the function extractEmails.
Table Functions used in the function extractEmails
extractEmails( returns {"john.. In addition, extractEmails( returns {"a@b", "b@c"}.
Answer group
Explanation
decrease i from atIndex - 1 to index — the name part lies left of the "@" and is built with prepend, so it is read right to left.
The loop collects the name before the "@" found at atIndex:
- •it starts one character before the
"@", atatIndex - 1, since"@"itself is not invalidChars - •it moves backwards and stops at
index, one past the previous"@", or earlier at the first invalid character such as a space - •each character is prepended, so reading right to left rebuilds the name in its original order
Trace of "a@b@c": the first "@" is at 2, the loop reads position 1 and gives "a", then the domain loop adds "b", so "a@b" is stored. Now index = 3 and the next "@" is at 4; the loop reads position 3 only, giving "b", then "b@c". This matches the stated result {"a@b", "b@c"}.
Answer (a)
Why not others:
- •(b) — starts at
atIndex, the"@"itself; it is not a valid character, so the loop exits at once,candidatestays empty and no address is ever collected - •(c) and (d) — walk forwards from
indexwhile prepending, which reverses the name; they also start at the beginning of the text instead of at the"@", so in the first example they read"Contact", stop at the space, and store"tcatnoC@example.com"
Key rule: Match the direction of the scan to how the string is built: prepend while moving left, append while moving right. Start the scan next to the anchor character, not on it.
String Processing · 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.