Clean Code Points 0 / 19
Code Rank Spaghetti Coder

2.3

Maintainability

Question 1 [3 Marks]
A software development company has a team of 20 programmers working on a single large project.

(a) Explain why the use of comments is essential when multiple programmers are working on the same file.
[2]

(b) Apart from comments, state one other technique that makes code easier to read for other programmers.
[1]
✅ Mark Scheme

1a: Allows other programmers to understand the logic/purpose of the code (1) so they can update/fix it without breaking it (1).

1b: Indentation / Meaningful Variable Names / Use of Subprograms / Constants

Score:
Question 2 [3 Marks]
The following algorithm calculates the pay for a worker. However, the programmer has used poor variable names, making it difficult to maintain.
x = float(input("Enter number: ")) y = float(input("Enter number: ")) z = x * y print("Total is: " + str(z))
Rewrite the algorithm above.
You must change the variable names to be meaningful and follow standard conventions. The logic must remain exactly the same.
[3]
✅ Mark Scheme

2: hours = float(input(...)) (1)
rate = float(input(...)) (1)
pay = hours * rate (1)

Score:
Question 3 [4 Marks]
In Python, indentation defines the structure (blocks) of the code.
Look at the two code snippets below. They contain the exact same text, but the indentation of Line 04 is different.

Snippet A:
01 score = 10 02 if score > 5: 03 print("Start") 04 print("End")
Snippet B:
01 score = 10 02 if score > 5: 03 print("Start") 04 print("End")
(a) State the output of Snippet A.
[1]

(b) State the output of Snippet B if score is changed to 2.
[1]

(c) Explain how the indentation of Line 04 changes the logic of the program between Snippet A and Snippet B.
[2]
✅ Mark Scheme

3a: Start
End

3b: End

3c: In A, Line 04 is part of the IF block (dependent on condition) (1).
In B, Line 04 is outside the IF block (runs sequentially/always) (1).

Score:
Question 4 [5 Marks]
A student has written a program that repeats the same code three times to draw three squares.
# Square 1 print("Moving Forward") print("Turning Right") print("Moving Forward") print("Turning Right") # ... (lines repeated for remaining sides and squares)
(a) This approach is inefficient and hard to update. Refine this solution by defining a subprogram (function) named draw_square().
[3]

(b) Explain how using this subprogram makes the code more maintainable if the student decides to change the shape from a Square to a Triangle later.
[2]
✅ Mark Scheme

4a: def draw_square(): (1)
  # Code for one square indented inside (1)
draw_square() (1)

4b: You only need to update the code in one place (the function definition) (1). The change automatically applies to every time the function is called (1).

Score:
Question 5 [4 Marks]
A program calculates the area of circles. The value 3.14 is used in 50 different places throughout the 1000-line code.
area1 = 3.14 * (radius1 * radius1) # ... 500 lines later ... area2 = 3.14 * (radius2 * radius2)
(a) Identify the maintenance issue with typing 3.14 manually in every calculation.
[2]

(b) Write a single line of code to declare 3.14 as a constant named PI, and write one example line showing how to use it in a calculation.
[2]
✅ Mark Scheme

5a: If the value of Pi needs to change, the programmer has to find/replace it 50 times (1). High risk of missing one or making a typo (1).

5b: PI = 3.14 (1)
area = PI * (radius * radius) (1)

Score: