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 · Programming: Variables and Choices
Explain each term with an example: variable, input, condition, selection.
Answer
- Variable: a named place in memory that holds one value, which can change while the program runs. Example: score, which starts at 0 and goes up by 1 for each right answer.
- Input: information that comes into a program from outside while it runs. Example: the age a passenger types when the bus fare program asks.
- Condition: a question whose answer is true or false. Example: age < 5.
- Selection: choosing which blocks to run, depending on a condition. Example: if age < 5 then set fare to 0 else set fare to 30.
What is the difference between set and change? A variable coins holds 8. What does it hold after set coins to 3, and what after change coins by 3 (each starting from 8)?
Answer
set puts a new value in the variable, throwing away the old one; change adds a number to the value already there.
After set coins to 3, coins holds 3. After change coins by 3, it holds 8 + 3 = 11.
Name the data type (number, text or true/false) best suited to each variable, and give a reason for one of them: a student’s name; the number of students in a class; whether the school bus has left; the price of a notebook; a district’s name; whether a player has a ticket.
Answer
| Variable | Type |
|---|---|
| a student’s name | text |
| number of students | number |
| whether the bus has left | true/false |
| price of a notebook | number |
| a district’s name | text |
| whether a player has a ticket | true/false |
For example: whether the bus has left has only two answers, so true/false fits it, and it can go straight into a condition: if left = true then …. The price is a number because a bill will add and multiply it.
x holds 6 and y holds 9. Is each condition true or false? i. x < y ii. x > 6 iii. y = 9 iv. x + 3 = y v. y < x
Answer
- i.true: 6 is less than 9.
- ii.false: 6 is not more than 6.
- iii.true.
- iv.true: 6 + 3 = 9.
- v.false: 9 is not less than 6.
Write a trace table for this program when the user types 7. What does it say?
ask “Pick a number” and keep the answer in nset total to n × 2if total > 10 then change total by -10else change total by 5say totalAnswer
| Step | Block | n | total |
|---|---|---|---|
| 1 | ask → n | 7 | |
| 2 | set total to n × 2 | 7 | 14 |
| 3 | if total > 10? true | 7 | 14 |
| 4 | change total by −10 | 7 | 4 |
| 5 | say total | 7 | 4 |
It says 4. (With 3 typed instead: total = 6, the condition is false, so total becomes 11.)
A canteen gives a free banana with any order of Rs 100 or more. Rojan writes: if bill > 100 then say “Free banana!”. Find the bug, give an input that shows it, and fix it.
Answer
The bug is at the edge: a bill of exactly Rs 100 should get the banana, but 100 is not more than 100, so the program says nothing. The input 100 shows it.
Fix: if bill > 99 then say “Free banana!” (for whole rupees). Test it with 99 (no banana), 100 (banana) and 150 (banana).
Design a program that asks for a student’s marks out of 100 and says “Distinction” for 80 or more, “Pass” for 40 to 79, and “Not yet” below 40. List its variables with their types, write its blocks in plain words, and give five tests with the answers they should give.
Answer
Variable: marks, a number (what the student types).
Blocks:
ask “Marks out of 100?” and keep the answer in marksif marks > 79 then say “Distinction”else if marks > 39 then say “Pass” else say “Not yet”Tests: 95 → Distinction; 80 → Distinction; 79 → Pass; 40 → Pass; 39 → Not yet. (Each edge, 79/80 and 39/40, is tested on both sides.) Other correct designs are possible, for example checking “< 40” first.
Look at this quiz program. What is wrong with it, and what will a player see if they play two games in a row and answer right both times? How would you fix it?
ask “What is 9 × 7?” and keep the answer in answerif answer = 63 then change score by 1say join “Score: ” scoreAnswer
score is never set to 0. The first time it runs, score has no value, so in our blocks the program stops at change score by 1 with an error. (In block languages where a new variable starts at 0, the first game shows Score: 1, but the second shows Score: 2, the third 3: the score carries on from the game before.)
Fix: add set score to 0 as the first block, so every game starts from zero.
Plan a game of your own that uses at least three variables of at least two different types. Write: what the game does, a table of its variables (name, type, what it holds), its steps, and three tests.
Answer
Many answers are right. An example, a times-table game:
What it does: picks two numbers from 2 to 9, asks for their product, says whether the answer is right, and keeps a score over 3 rounds.
| Variable | Type | What it holds |
|---|---|---|
| a, b | number | the two numbers picked |
| answer | number | what the player types |
| score | number | right answers so far |
| right | true/false | whether this answer was right |
Steps: set score to 0; repeat 3 times: set a and b to random numbers from 2 to 9; ask “What is a × b?” and keep it in answer; set right to (answer = a × b); if right = true then say “Correct!” and change score by 1, else say the right answer. Finally say the score.
Tests (with the random numbers fixed for testing, say a = 6, b = 7): answer 42 → “Correct!”, score 1; answer 41 → “Sorry, it is 42”, score 0; three right answers → score 3.
Why do programs need input and if/else? Explain with an example of an app you have used.
Answer
Without input, a program would do exactly the same thing every time it ran, whoever used it. Without if/else, it couldn’t react differently to different inputs.
Example: a mobile wallet app asks how much to send (input) and keeps it in a variable. If the amount is more than the balance, it says there isn’t enough money; else it sends the money and takes the amount away from the balance (another variable). The same program works for every user and every amount because of input, variables and choices.
