Skip to content

平成22年度 春期 ITパスポート試験 問53

This question's explanation and your answers so far.

問53
頻出度★★☆

変数AとBに格納されているデータを入れ替えたい。データを一時的に格納するための変数をTMPとすると, データが正しく入れ替わる手順はどれか。ここで“x←y”は, yのデータでxの内容を置き換えることを表す。

出典 平成22年度 春期 ITパスポート試験 問53
解説

問 53

  • Incorrectア

    TMP←A → A←B → B←A. Steps (1) and (2) are the same as in イ, giving A=b, B=b, TMP=a, but step (3) is B←A instead of B←TMP. At that point A has already been overwritten with b in step (2), so B←A merely assigns b to B and B stays b, unchanged. The final state is A=b, B=b: the value a that was carefully saved in TMP is never used even once, and the original value of A is lost. This is the typical mistake of omitting the last step of writing the saved value back.

  • イ

    正解 The iron rule for swapping variables is to save the value of the side that is about to be overwritten first, and then write it back; choice イ follows that order correctly. Let a be the original value of A and b the original value of B. (1) TMP←A saves a into TMP (at this point A=a, B=b, TMP=a). (2) A←B replaces A with b (A=b, B=b, TMP=a — the original value a of A has been overwritten, but it remains in TMP). (3) B←TMP writes the saved a back into B (A=b, B=a). As a result, A holds the original value of B and B holds the original value of A, so the swap succeeds. The key point is that the value of A, which is lost in step (2), is first moved to TMP in step (1), and in step (3) it is written back using that TMP.

  • Incorrectウ

    TMP←B → A←B → B←TMP. The error is that step (1) saves B instead of A. (1) TMP←B makes TMP=b. (2) A←B makes A=b, so the original value a of A is overwritten without ever being saved and disappears completely. (3) B←TMP only writes b back into B, so the final state is A=b, B=b. Because the variable that is saved and the variable that is overwritten next do not match, the result is not a swap but a copy of the value of B.

  • Incorrectエ

    TMP←B → A←B → B←A. As in ウ, what is saved is the value of B, and in addition step (3) does not use TMP either. (1) TMP←B makes TMP=b. (2) A←B makes A=b, so a disappears. (3) B←A merely assigns the value of A at that point (=b) to B, so B stays b. The final state is A=b, B=b, and TMP is only assigned at the very beginning and is never referenced even once. This choice combines two errors: saving the wrong variable and omitting the write-back.

ここがポイント
  • To swap the values of two variables, you must be aware that assignment is destructive (x←y overwrites x with the value of y, and the original value of x is lost). You first save the value of the side that is about to be overwritten into the temporary variable TMP, and finally write that TMP back into the other variable — these three steps (TMP←A → A←B → B←TMP) are required.
  • The variable that is saved must be the same as the variable that is overwritten first (if you overwrite A, you must also save A), and the saved TMP must be referenced at the end. If either of these two conditions is missing, both variables end up with the same value and the data of one of them is lost — every incorrect choice violates one of these two.
用語

Your answers