IPA FE Subject B 2024 Question 1
A question IPA Japan published in 令和6年度 基本情報技術者試験 科目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 function maximum receives three different integers as arguments, and returns the maximum value among them.
Answer group
Explanation
x > y and x > z — the first branch returns x, so its condition must hold exactly when x is the largest of the three; the elseif then only has to choose between y and z.
Since the three integers are different, x is the maximum exactly when it is greater than both others: x > y and x > z. If that is false, the maximum is y or z, and elseif (y > z) picks the larger of the two, which is correct whatever x is.
All six orders of the values 1, 2, 3 were run with each option; only x > y and x > z returns 3 every time. For example, maximum( returns 3 in the first branch, and maximum( fails the condition and returns z = 3.
Answer (b)
Why not others:
- •(a)
x > y— returnsxwithout comparing it withz:maximum(returns 22, 1, 3) - •(c)
x > y and y > z— demands the orderx,y,z, so a largestxwithy < zfalls through:maximum(returns 23, 1, 2) - •(d)
x > z— ignoresy:maximum(returns 22, 3, 1) - •(e)
x > z and z > y— demandsz > y, so a largestxwithy > zfalls through:maximum(returns 23, 2, 1) - •(f)
z > y— says nothing aboutx:maximum(returns 11, 2, 3)
Key rule: In an if/elseif chain, the first condition must test the full claim its branch makes (here, "x beats every other value"); the later branches may then assume it was false.
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.