Exam practice
Every question from the end of every unit, with hints and answers to open. After you check an answer, mark how you did, and come back to the ones you didn’t get.
10 questions
Unit 3 · Algorithms: Step by Step
What is an algorithm? Write three things that every good algorithm has, and give two examples of algorithms from daily life.
Answer
An algorithm is a list of exact steps, in order, that does a job or solves a problem.
A good algorithm: (1) has steps that are clear and exact, each with only one meaning; (2) has its steps in the right order; (3) comes to an end, with the job done.
Examples (any two): a recipe for making chiya; the steps for washing your hands; directions from school to a temple; the steps for doing a long division.
Rewrite these instructions so that a robot could follow them exactly: “Put some rice in a pot with enough water and cook it till it’s done.”
Answer
For example:
- Put 1 cup of rice in the pot.
- Wash the rice in water 2 times, pouring the water out each time.
- Pour 2 cups of water into the pot.
- Turn the stove on.
- When the water boils, turn the flame down low and put the lid on.
- Cook for 15 minutes.
- Turn the stove off.
Any answer is right if every amount and time is a number with its unit, each step is one action, and nothing is left to guess. (Cooking times differ with the rice and the pot; what matters is that the algorithm says one exact time.)
Explain sequence, repetition and choice, with an everyday example of each.
Answer
- Sequence: doing steps one after another, in order. Example: wake up, wash your face, put on your uniform, eat breakfast.
- Repetition: doing the same steps again, a number of times or until something happens. Example: fold 40 momos; stir until the sugar dissolves.
- Choice: doing some steps only if a condition is true. Example: if it is raining, take an umbrella; otherwise, leave it at home.
You are facing east. You follow these directions: turn left, forward 2 blocks, turn left, forward 1 block, turn left. Which way are you facing at the end? Draw your path.
Hint
Draw an arrow for the way you face after each turn.
Answer
Facing east, turn left → north; forward 2 blocks north; turn left → west; forward 1 block west; turn left → south. The path goes 2 blocks up, then 1 block to the left, and you end up facing down (south).
What is the difference between “repeat a number of times” and “repeat until”? Give an example where each is the better choice.
Answer
“Repeat a number of times” is used when you know before you start how many times the steps must be done: “Repeat 4 times: put a plate on the table” for 4 people.
“Repeat until” is used when you don’t know how many times, and must check after each time whether to stop: “Keep pumping air into the bicycle tyre until it is hard.”
What is a variable? Copy and complete the table with the data type of each variable (number, text or yes/no).
| Variable | Value | Data type |
|---|---|---|
| marks | 78 | |
| city | Pokhara | |
| passed | yes | |
| students | 32 | |
| school | Bhaktapur Secondary School |
Answer
A variable is a named place that holds one piece of information, which can change while an algorithm runs.
| Variable | Value | Data type |
|---|---|---|
| marks | 78 | number |
| city | Pokhara | text |
| passed | yes | yes/no |
| students | 32 | number |
| school | Bhaktapur Secondary School | text |
A school gives a prize to students whose marks are 80 or more. Raju writes this algorithm:
- Ask the student’s marks. Keep the number in marks.
- If marks is more than 80, say “Prize!”. Otherwise, say “Well done.”
Test it with marks of 92, 80 and 45. Which test finds a bug? Fix the algorithm.
Answer
- 92: more than 80, so it says “Prize!”. Right.
- 80: 80 is not more than 80, so it says “Well done.” Wrong: the rule says 80 or more get a prize.
- 45: not more than 80, so “Well done.” Right.
The test with 80, right at the edge of the rule, finds the bug. Fixed step 2: “If marks is 80 or more, say ‘Prize!’. Otherwise, say ‘Well done.’”
Make a trace table for the tea shop’s algorithm for Gita, who wants 3 cups of black tea. What does the algorithm say at the end?
- Ask the customer’s name. Keep it in name.
- Ask how many cups. Keep the number in cups.
- Ask “Milk tea?” Keep the answer in milk.
- If milk is yes, price = 30. Otherwise, price = 20.
- bill = cups × price.
- Say: “name, please pay Rs bill.”
Answer
| Step | name | cups | milk | price | bill |
|---|---|---|---|---|---|
| 1 | Gita | ||||
| 2 | 3 | ||||
| 3 | no | ||||
| 4 | 20 | ||||
| 5 | 60 |
At step 6 it says: “Gita, please pay Rs 60.” (Black tea, so the price is Rs 20, and 3 × 20 = 60.)
What is debugging? Write the four steps of debugging an algorithm, and say why you should test again with all the old tests after fixing a bug.
Answer
Debugging is finding the mistakes (bugs) in an algorithm and fixing them.
- Find a test that fails.
- Trace the algorithm for that test and find the first step that goes wrong.
- Fix that one thing.
- Test again, with the failing test and all the others.
You test again with the old tests because a fix in one place can break something that used to work. If all the tests still pass, you know the fix didn’t cause a new bug for those inputs.
Design an algorithm for a bus conductor that works out one passenger’s fare. A ride costs Rs 30, students pay half, and children under 5 years old ride free. Use variables (with their data types) and choices, and test your algorithm with three passengers of your own.
Hint
Ask the age first. A child under 5 pays nothing, whether a student or not.
Answer
- Ask the passenger’s age. Keep it in age (number).
- Ask “Are you a student?” Keep the answer in student (yes/no).
- If age is less than 5, fare = 0.
- Otherwise, if student is yes, fare = 15.
- Otherwise, fare = 30.
- Say: “Please pay Rs fare.”
Tests (the fares are made up for this question):
- age 3, student no: under 5, so Rs 0. Right.
- age 12, student yes: not under 5, a student, so Rs 15. Right.
- age 40, student no: Rs 30. Right.
A good set of tests also tries age 5 exactly (it should pay, because 5 is not under 5).
