Practice a Create-Performance-Task-style written response about a procedure, a list, iteration, and testing. Write your response, then reveal the model answer to see exactly what earns each point.
Note: The AP CSP exam is all multiple-choice; extended writing appears in the Create Performance Task. This question gives you practice with that style of written response.
A student builds a to-do list app. The tasks are stored in a list called tasks, and each task has a status of either “done” or “not done”. The program includes a procedure countDone(tasks) that returns how many tasks are marked “done”.
The procedure's parameter is tasks, the list of tasks, and it returns a number — the count of tasks whose status is “done”. To compute this it must use iteration to traverse every task in the list, and selection (a conditional) to check whether each task's status equals “done” and, if so, add 1 to a running count.
Start by setting a counter variable to 0. Then traverse the list tasks, visiting each task one at a time (for example, with a loop over each index). For each task, use a conditional to check whether its status is “done”; if it is, add 1 to the counter. After the loop has visited every task, return the counter as the total number of completed tasks.
Putting the counting logic in a procedure improves the program through abstraction and reuse: the rest of the program can simply call countDone(tasks) whenever it needs the total, without repeating the code, which makes the program shorter and easier to maintain. To test it, run it with lists whose answers are known — for example, a list where 3 of 5 tasks are “done” should return 3, a list with none done should return 0, and an empty list should return 0. Comparing the results to these expected values (including edge cases) confirms the procedure works.