Term 1 in review
Answers are for parents and teachers.
1
Follow the algorithm when the user types 5. What is output? Then change it so that the loop outputs the even numbers 2, 4, 6, 8 and 10.
Pseudocode
1INPUT limit 2FOR n ← 1 TO limit 3 OUTPUT n * 5 4NEXT n
The counter takes 1 to 5, so the output is 5, 10, 15, 20 and 25. To output 2, 4, 6, 8 and 10, change the output line to OUTPUT n * 2. The loop and the typed limit stay the same.
2
Write Python. The user types a word. Print it in capital letters, then print how many characters it has. What is printed for the word tiger?
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): word = input("Word? ") / print(word.upper()) / print(len(word)). Check: For tiger it prints TIGER and then 5.
3
The list is scores = [8, 15, 11, 20]. Write the value of scores[1], of scores[3] and of len(scores). What happens with scores[4]?
scores[1] is 15, scores[3] is 20 and len(scores) is 4. scores[4] causes IndexError: list index out of range, because the last index is 3.
4
Two programs both output the total of two typed numbers. Program A uses four variables. Program B uses two. Program B misses the requirement to output the total. Which program is better? Explain.
A is better. B does not meet the requirement, so it is not a solution. Efficiency is compared only for programs that meet the requirements.
5
The sorted list is 2, 9, 14, 20, 27, 35, 41. Use a binary search to find 35 and write the centre item at each check. How many comparisons does it take?
Check 1: centre 20; 35 is larger, leaving 27 35 41. Check 2: centre 35; found. It takes 2 comparisons.