ITPEC FE Subject B April 2025 Question 12
ITPEC FE Subject B April 2025 — Question 12 of 20
Hamming distance counts positions where two equal-length arrays differ
The function hammingDistance takes two character arrays s1 and s2. If their lengths differ, it returns -1. Otherwise, it iterates through each index and counts how many positions have different values.
The missing condition is s1[i] ≠ s2[i] — the loop variable is i, and we need to compare elements at the same index and increment cnt when they differ.
Why not others:
- s1[i] = s2[i] (g) — would count matching positions, not differences
- Options using cnt as index (a–d) — cnt is the counter, not the loop variable; using it as an index would compare wrong positions
- s1[i] = s2[cnt] (e) or s1[i] ≠ s2[cnt] (f) — mixes loop index with counter, comparing mismatched positions
Key rule: In Hamming distance, compare elements at the same index using the loop variable, and count where they differ (≠).
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
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.