Skip to content

平成23年度 特別試験 ITパスポート試験 問95

This question's explanation and your answers so far.

問95
頻出度★★★

Q社のRさんは,チケット販売システムの開発で使用する関数のプログラミングとテストを実施しようとしている。この関数は,大人と子供のチケットの購入希望枚数を基に,合計金額の計算を行う。大人のチケットは3,000円,子供のチケットは1,000円である。一度に購入できる大人と子供のチケットの最高枚数は,それぞれ20枚までであり,関数はこの購入条件を満たしているかどうかをチェックする。また,合計で20枚以上購入する場合,割引処理を行う。関数の仕様と処理内容は次のとおりである。 〔関数の仕様〕 関数に渡される引数  J:大人のチケットの購入希望枚数,K:子供のチケットの購入希望枚数  (J,Kは0以上の整数) 関数の戻り値  L:合計金額 (Lは整数) 〔処理内容〕 (1) 購入希望枚数を基に,購入条件を満たしているかどうかを判定する。購入条件を満たしていない場合,合計金額に−1を代入する。 (2) 購入条件を満たしている場合,引数を基に次のとおりに合計金額の計算を行う。   合計金額 = 大人のチケットの購入希望枚数×3,000+子供のチケットの購入希望枚数×1,000 (3) チケットを合計で20枚以上購入した場合は,合計金額から1割を割り引く。 関数をテストするために用意したテストデータと予想結果の一部を表2に示す。表2のd,eに入れる値の組合せはどれか。 表2 テストデータと予想結果の一部

テスト番号JKL
13211,000
21515d
3200(網掛け)
41030e
⋮⋮⋮⋮

注記 網掛けの部分は,表示していない。

出典 平成23年度 特別試験 ITパスポート試験 問95
解説

問 95

※ Our check while writing this explanation could not fully confirm the published answer; scoring follows IPA's official key.

  • Incorrectア

    d=54,000 is correct, but e=54,000 is wrong. In Test 4, K=30 violates the purchase condition that at most 20 child tickets, too, can be bought at one time. The function gets No at the first condition check (0≦J≦20 and 0≦K≦20), never enters the amount-calculation branch at all, and returns L=−1. The value 54,000 appears only if you skip the condition check, compute 3,000×10+1,000×30=60,000, and then multiply by 0.9 because J+K=40≧20; it is a mistake that ignores the purchase-condition test.

  • イ

    正解 The correct answer is イ (d:54,000, e:−1). Following the [Processing details] of question group B, this function works in this order: (1) first check the purchase conditions, (2) calculate the total amount only when the conditions are met, and (3) take 10% off when 20 or more tickets are bought in total. The purchase condition is that at most 20 adult tickets and at most 20 child tickets can be bought at one time, that is, 0≦J≦20 and 0≦K≦20; if this is not satisfied, the function does not calculate any amount but assigns −1 to L and ends. When the condition is satisfied, it computes L=3,000×J+1,000×K, and then takes 10% off, that is, multiplies by 0.9, only when J+K≧20. First, check this calculation model against the known Test 1: J=3 and K=2 are both 20 or less, so the condition is met; J+K=5 is less than 20, so there is no discount, and L=3,000×3+1,000×2=9,000+2,000=11,000, which matches the value in Table 2. Next, find d. In Test 2, J=15 and K=15 are both 20 or less, so the purchase condition holds; J+K=30 is 20 or more, so the discount applies, and L=0.9×(3,000×15+1,000×15)=0.9×(45,000+15,000)=0.9×60,000=54,000. Therefore d=54,000. Then find e. In Test 4, J=10 is 20 or less, but K=30 exceeds the upper limit of 20 tickets, so it does not meet the purchase condition of at most 20 tickets each for adults and children. In this case the flow never enters the amount-calculation branch; under [Processing details] (1) it assigns −1 to L and ends, so e=−1. Only イ has the combination d=54,000 and e=−1, so イ is the correct answer. Confirming that −1 is returned in an abnormal case that fails the conditions, as in Test 4, is also an important role of test data.

  • Incorrectウ

    Both d and e are wrong. d=60,000 is the value obtained by stopping at the pre-discount amount 3,000×15+1,000×15=60,000 for Test 2 (J=15, K=15). Since J+K=30 is 20 or more, the 10% reduction in [Processing details] (3) is certainly applied, so the correct expected result must be 0.9×60,000=54,000. e=54,000 is wrong for the same reason as ア: in Test 4, K=30 exceeds the upper limit, so the flow does not reach the amount calculation and L=−1.

  • Incorrectエ

    e=−1 is correct, but d=60,000 is wrong. In Test 2, J=15 and K=15 are both 20 or less, so the purchase condition is met and the total amount comes to 3,000×15+1,000×15=60,000. But that is not the end: there is a rule that when 20 or more tickets are bought in total, 10% is taken off the total amount, and J+K=30 is 20 or more, so the discount applies. The expected result is therefore not 60,000 but 0.9×60,000=54,000. 60,000 is merely the value obtained by forgetting to apply the discount step.

ここがポイント
  • The expected result (expected value) for test data must be worked out by yourself by tracing the processing according to the specification, not according to the implementation. For the function in this question, the decisive point is the order of the branches: check the purchase conditions (0≦J≦20 and 0≦K≦20) → if not met, L=−1; if met, compute the total amount L=3,000×J+1,000×K → then, if J+K≧20, take 10% off (×0.9). Keep to that order and d=54,000 and e=−1 are determined mechanically.
  • How to cover boundary values and abnormal cases — going over the limit of 20 tickets by even one (for example K=30) means the amount calculation is skipped and the error value −1 is returned, while exactly 20 tickets in total still gets the discount because J+K≧20. Deliberately preparing test data aimed at such boundaries and exceptions is a basic of test design. Verifying the calculation model with the known Test 1 (J=3, K=2 → 11,000) before filling in the unknown cells prevents mistakes such as forgetting to multiply by the discount.
用語

Your answers