ITPEC FE Subject B April 2026 Question 12

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array indexes start at 1.

The function isIsomorphic compares the two character arrays s1 and s2 that are given as arguments. Both s1 and s2 have one or more elements.
Two character arrays are considered isomorphic if the characters in s1 can be consistently replaced to obtain s2 without changing the order of characters. Each character in s1 must map to exactly one character in s2, and no two characters in s1 may map to the same character in s2. A character may map to itself.

If s1 and s2 do not have the same number of elements, the function returns false. Otherwise, it returns true if s1 and s2 are isomorphic and false if they are not.

The table lists examples of s1 and s2 given to the function isIsomorphic and the return values.

Table Examples of s1 and s2 given to the function isIsomorphic and the return values

s1s2Return Value
{"e", "g", "g"}{"a", "d", "d"}true
{"f", "o", "o"}{"b", "a", "r"}false
{"p", "a", "p", "e", "r"}{"t", "i", "t", "l", "e"}true
{"a", "b"}{"c", "c"}false
[Program]
○ boolean: isIsomorphic(character []: s1, character []: s2)
integer: len, i, j
len ← number of elements in s1
if (number of elements in s2 ≠ len)
return false
endif
for (increase i from 1 to (len - 1) by 1)
for (increase j from (i + 1) to len by 1)
if ((s1[i] = s1[j] and A)
or (s1[i] ≠ s1[j] and B))
return false
endif
endfor
endfor
return true

Answer group

OptionAB

Explanation

A: s2[i] ≠ s2[j], B: s2[i] = s2[j] — every pair of positions must agree in both arrays: equal in s1 exactly when equal in s2.

The double loop visits every pair of positions i < j and returns false on either way a one-to-one mapping can break:

  • •Blank A: s1[i] = s1[j] but s2[i] ≠ s2[j] — one source character maps to two different targets. {"f", "o", "o"} against {"b", "a", "r"} fails here at positions 2 and 3.
  • •Blank B: s1[i] ≠ s1[j] but s2[i] = s2[j] — two source characters map to one target. {"a", "b"} against {"c", "c"} fails here.

For {"e", "g", "g"} and {"a", "d", "d"} no pair triggers either condition, so the function returns true.

Answer (d)

Why not others:

  • •(a) and (b) — compare s1[i] with s2[j], a character of one array against a character of the other; isomorphism never requires the two alphabets to share letters. With (a), egg/add fails at i = 1, j = 2 because "e" ≠ "d"; with (b) it fails at i = 2, j = 3 because "g" ≠ "d", though the expected result is true
  • •(c) — inverts both tests, so it rejects the consistent pairs: for egg/add, positions 2 and 3 have s1 equal and s2 equal, and (c) returns false

Key rule: A one-to-one mapping must be checked in both directions: equal sources give equal targets, and different sources give different targets.

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.