When two defects hide behind a passing test

Mark Yourself Question

  1. Grab a piece of paper and try this problem yourself.
  2. When you're done, check the "I have answered this question" box below.
  3. View the solution and report whether you got it right or wrong.

A function is specified to calculate energy in joules from constant power in kilowatts and duration in minutes. Its intermediate variables must represent power in watts and duration in seconds because those values are also used in diagnostic reports.


Python
def energy_j(power_kw, duration_min):
    power_w = power_kw * 60.0
    duration_s = duration_min * 1000.0
    return power_w * duration_s


For power_kw = 1.8 and duration_min = 2.5, an existing test expects 270000 J270000\ \text{J} and passes. Assume finite positive inputs, exact arithmetic for the algebraic comparison, and no overflow.


a) Trace both intermediate values and the returned energy. Explain why the energy test passes despite incorrect conversions.


b) Correct both conversions and calculate the repaired intermediate values and returned energy.


c) Determine whether testing only the returned energy with additional inputs can distinguish these two implementations under the stated arithmetic assumptions. Propose checks that would detect the intermediate defects.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Software Debugging Questions: