IPA FE Subject B first sample Question 1
A question IPA Japan published in 基本情報技術者試験 科目Bのサンプル問題 問1, translated into English. ITPEC is based on the same exams.
From the answer group below, select the correct answer to be inserted into blank in the program.
The admission fee of a certain facility is 100 yen for ages 0 to 3, 300 yen for ages 4 to 9, and 500 yen for ages 10 and above. The function fee receives an integer of 0 or more that represents an age as its argument and returns the admission fee.
Answer group
Explanation
age is 9 or less — the elseif is tested only when the if was false, so every age that reaches it is already 4 or more; it has only to separate 4 to 9 from 10 and above.
The branches are tested from the top. Ages 0 to 3 stop at the if and get 100. The elseif must then be true for 4 to 9, which get 300, and false for 10 and above, which fall to else and get 500. A lower bound of 4 would be redundant, and “age is 9 or less” is exactly the upper bound.
Run for ages 0 to 29 with each option, only (f) returned the specified fee for every age: fee( = 100, fee( = 300, fee( = 300, fee( = 500.
Answer (f)
Why not others:
- •(a) and (g) — “less than 9” leaves out 9 itself, so
fee(returns 500 instead of 3009) - •(b) — true only for 4 and 9, so ages 5 to 8 get 500
- •(c) — “greater than 4” leaves out 4, so
fee(returns 5004) - •(d) — no upper bound: every age that reaches it is 4 or more, so ages 10 and above also get 300 and
elseis never reached - •(e) — no upper bound and leaves out 4: age 4 gets 500, ages 10 and above get 300
Key rule: A later branch of an if–elseif chain runs only when every earlier condition was false, so it need not repeat their bounds; test each boundary age (3 and 4, 9 and 10) against “or less”, “less than”, “or more” and “greater than”.
Math & Numbers · AI-written, may contain errors
This question comes from the past questions IPA Japan publishes, translated into English by ITPEC Practice. ITPEC Practice is an independent study tool and is not affiliated with IPA or ITPEC. See the original paper (PDF) or Report an issue.