Assigning the result of an expression

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Assigning the result of an expression

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In TotalToPay ← NumberOfHours * HourlyRate, what happens first?
A TotalToPay is given its value before anything is calculated
B NumberOfHours * HourlyRate is worked out
2. Which line stores the result of a calculation in TotalToPay?
A TotalToPay ← NumberOfHours * HourlyRate
B NumberOfHours * HourlyRate ← TotalToPay
3. Counter currently holds 4. What does Counter hold after Counter ← Counter + 1 ?
A 5
B 4
4. Which statement describes Counter ← Counter + 1 correctly?
A It is impossible because Counter appears on both sides
B It uses the old value of Counter, then replaces it with the new result
5. HourlyRate holds 8 and NumberOfHours holds 5. After TotalToPay ← NumberOfHours * HourlyRate, what do the variables hold?
A TotalToPay is 40, and NumberOfHours and HourlyRate are unchanged
B TotalToPay is 40, and NumberOfHours and HourlyRate are now 0

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1TotalToPay ← NumberOfHours * HourlyRate AThe arrow is the wrong way round. The variable that receives the result must be on the left and the expression on the right: TotalToPay ← NumberOfHours * HourlyRate.
2Counter ← Counter + 1 BTakes the current value of Counter, adds 1, then stores the new value back in Counter, replacing the old one
3Copying the value of one variable into another variable CBackupScore ← Score
4The expression on the right is calculated before anything is stored in the variable on the left DThe expression is on the left of the arrow, so the result has nowhere to be stored. It should be Counter ← Counter + 1.
5NumberOfHours * HourlyRate ← TotalToPay EWorks out NumberOfHours multiplied by HourlyRate first, then stores the result in TotalToPay
6Counter + 1 ← Counter FArea ← Length * Width

Definitions

TotalToPay ← NumberOfHours * HourlyRate

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
TotalToPay ← NumberOfHours _ HourlyRate
2.
Counter ← 5 Counter ← _______ + 1 OUTPUT Counter
3.
Lives ← 3 Lives ← Lives _ 1
4.
Score ← 10 Backup ← _____
5.
Total _ Price * Quantity

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Hours ← 8
B 12.5 ← Rate
C Total ← Hours * Rate
D OUTPUT Total
Question 2
A Counter ← 0
B Counter ← Counter + 1
C Counter ← Count + 1
D OUTPUT Counter
Question 3
A Hours = 6
B Rate ← 11.5
C TotalToPay ← Hours * Rate
Question 4
A Length ← 5
B Width ← 3
C Area ← Length * Width
D Perimeter ← 2 * Length + Width
Question 5
A First ← 10
B Second ← 20
C First ← Backup
D OUTPUT Backup

Answer Key

Assigning the result of an expression
Section A: Quick Questions
Q1
B — NumberOfHours * HourlyRate is worked out
Q2
A — TotalToPay ← NumberOfHours * HourlyRate
Q3
A — 5
Q4
B — It uses the old value of Counter, then replaces it with the new result
Q5
A — TotalToPay is 40, and NumberOfHours and HourlyRate are unchanged
Section B: Match the Pairs
Q1
TotalToPay ← NumberOfHours * HourlyRate → Works out NumberOfHours multiplied by HourlyRate first, then stores the result in TotalToPay
Q2
Counter ← Counter + 1 → Takes the current value of Counter, adds 1, then stores the new value back in Counter, replacing the old one
Q3
Copying the value of one variable into another variable → BackupScore ← Score
Q4
The expression on the right is calculated before anything is stored in the variable on the left → Area ← Length * Width
Q5
NumberOfHours * HourlyRate ← TotalToPay → The arrow is the wrong way round. The variable that receives the result must be on the left and the expression on the right: TotalToPay ← NumberOfHours * HourlyRate.
Q6
Counter + 1 ← Counter → The expression is on the left of the arrow, so the result has nowhere to be stored. It should be Counter ← Counter + 1.
Section C: Definitions
Q1
TotalToPay ← NumberOfHours * HourlyRate — The expression on the right is worked out first, then its result is stored in the variable on the left, replacing any old value that variable held.
Section D: Fill in the Blanks
Q1
TotalToPay ← NumberOfHours * HourlyRate
Q2
Counter ← 5 Counter ← Counter + 1 OUTPUT Counter
Q3
Lives ← 3 Lives ← Lives - 1
Q4
Score ← 10 Backup ← Score
Q5
Total ← Price * Quantity
Section E: Spot the Error
Q1
B — The variable being assigned to must be on the left of the arrow and the value on the right. It should be Rate ← 12.5.
Q2
C — The expression must use the variable Counter, whose old value is read and then replaced. Count was never declared or given a value. It should be Counter ← Counter + 1.
Q3
A — Cambridge pseudocode assigns with the ← arrow, not the = sign. It should be Hours ← 6.
Q4
D — Multiplication is worked out before addition, so this only doubles Length. Brackets are needed so the expression is Perimeter ← 2 * (Length + Width).
Q5
C — To copy First into Backup, the variable receiving the value goes on the left. Here Backup has no value yet and First would be overwritten. It should be Backup ← First.