ITPEC FE Morning April 2019 Question 8

The flowchart below shows the algorithm of concatenating two character strings in X and Y, with the resulting character string in Z. Which of the following is the appropriate pair of operations to insert in the blanks, A and B? Here, X and Y are character arrays of dimensions m and n, respectively, where the i-th character of each string is stored as the i-th element of the character array.

StartLoop 1k:1, 1, mALoop 1Loop 2k:1, 1, nBLoop 2End

Note:

The description in Loop 1 indicates that variable k varies from 1 to m by 1. So does Loop 2.

OptionAB

Explanation

A: X(k) → Z(k), B: Y(k) → Z(m+k) — copy X to the front of Z, then place Y directly after it.

  • •Loop 1 runs k from 1 to m, the length of X, so it must copy X: X(k) goes to Z(k), filling Z(1) to Z(m)
  • •Loop 2 runs k from 1 to n, the length of Y, so it copies Y, starting right after the last character of X: Y(k) goes to Z(m + k), filling Z(m + 1) to Z(m + n)

Answer (a)

Why not others:

  • •(b) — offsets Y by n, its own length; unless m = n this overwrites the end of X or leaves a gap after it
  • •(c) and (d) — copy Y in Loop 1, which runs to m, the length of X; they also put Y before X, which reverses the concatenation

Key rule: When appending one array to another, the second array's index in the result is offset by the length of the first; each loop's bound tells you which array it walks.

Algorithm and Programming · 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.