Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1def calc(a, b): -> def calc_area(width, height): | AThe tests were not re-run, so there is no proof the refactored code still works. Always re-test after refactoring. | |
| 2def show_total(price): print("Total: $", price) | Bx = w * h -> area = width * height | |
| 3After refactoring, re-run the same test cases to check the program still gives the same results | Cassert calc_area(3, 4) == 12 | |
| 4Refactoring changes how the code is written but not what it does | DRenaming a function and its variables so the names show their purpose, without changing the result | |
| 5area = width * height -> area = width + height | EThe calculation was changed, so the program now behaves differently. That is not refactoring because refactoring must keep the same behaviour. | |
| 6Refactor the code, then hand it in without running the tests | FA repeated print line moved into one function that can be called many times |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
refactoring — Rewriting working code to make it clearer or simpler (e.g. renaming variables, moving repeated lines into a function) without changing what it does, then re-running tests.totalPrice = price * qty
print(totalPrice)def showTotal(price, qty):
return price * qtydef showTotal(price, qty):
return price * qty
print(showTotal(4, 3))def showTotal(price, qty):
return price * qty
assert showTotal(2, 3) == 6before = 12
after = showTotal(4, 3)
print(before == after)