ITPEC IP April 2025 Question 6

The procedure calculateElectricBill receives electricity usage that accepts the units (non-negative value) and returns the amount of electricity bills. The pricing structure is given as: For the first 100 units, the cost is $10 per unit. Any units exceeding 100 are charged at $15 per unit. Which of the following is an appropriate piece of code to be inserted into blank in the program?

[Program]
○ integer: calculateElectricBill(integer: units)
integer: payAmount ← 0
if (units ≤ 100)
payAmount ← units × 10
else
payAmount ← blank
endif
return payAmount

Explanation

(units − 100) × 15 + 100 × 10 — above 100 units, the first 100 cost $10 each and only the excess costs $15.

The else branch runs when units is greater than 100. The bill has two parts:

  • •First 100 units at $10: 100 × 10 = 1,000
  • •The remaining units − 100 at $15: (units − 100) × 15

Check with 150 units: 50 × 15 + 100 × 10 = 750 + 1,000 = 1,750 dollars. At exactly 100 units the formula would give 1,000, the same as the if branch, so the two tiers join without a jump.

Answer (b)

Why not others:

  • •(a) — swaps the rates: the excess at $10 and the first 100 at $15; 150 units would cost 2,000
  • •(c) — charges every unit at $15, losing the cheaper first tier; 150 units would cost 2,250
  • •(d) — charges every unit at $15 and then adds the first tier again, counting the first 100 units twice; 150 units would cost 3,250

Key rule: In tiered pricing, charge each tier only for the units that fall in it, and check the formula at the boundary, where both branches must agree.

Algorithm and Programming · 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 IP past-paper collection or Report an issue.