ITPEC FE Subject B October 2024 Question 2
From the answer group below, select the correct combination of answers to be inserted into A and B in the program.
The function coupon receives the argument prod_id (a positive integer value) of product ID, and pur_prod (a positive integer value) of the number of purchased products by a customer. The function returns the number of coupons.
For every purchase of three products of the product ID of which the last digit is three, the customer receives one coupon. Otherwise, they receive no coupon.
Answer group
Explanation
A: prod_id mod 10 = 3, B: integer part of (pur_prod ÷ 3) — the remainder after dividing by 10 is the last digit, and each complete group of three products earns one coupon.
The last digit of a positive integer is its remainder after division by 10: for ID 143 the remainder is 3, so the condition holds, and for ID 134 it is 4, so no coupon is given.
One coupon for every three products means counting complete groups of three. For 7 products the integer part of 7 ÷ 3 is 2, so 2 coupons; 2 products give 0.
Answer (g)
Why not others:
- •(a), (b) and (c) — integer part of
(prod_id ÷ 10)drops the last digit instead of keeping it; it equals 3 only for IDs 30 to 39, so ID 143 gets nothing while ID 35 qualifies - •(d), (e) and (f) —
10 mod 3is the constant 1, so the test is simplyprod_id = 1and never looks at the last digit - •(h) — has the right condition, but the
+ 1gives one coupon too many: 2 products earn 1 coupon and 3 earn 2 (the same flaw is in (b) and (e)) - •(i) — has the right condition, but
pur_prod mod 3is what is left over after the groups of three: 3 products earn 0 coupons and 5 earn 2 (the same flaw is in (c) and (f))
Key rule: x mod 10 keeps the last digit and the integer part of x ÷ 10 removes it; likewise the integer part of x ÷ k counts complete groups of k, while x mod k counts what is left over.
Math & Numbers · 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.