Assignment with ← (7c)

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

Assignment with ← (7c)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly stores 0 in the variable Counter?
A 0 ← Counter
B Counter ← 0
2. Which line adds 1 to Counter and stores the new value back in Counter?
A Counter ← Counter + 1
B Counter + 1 ← Counter
3. Which line correctly calculates the amount to pay?
A NumberOfHours * HourlyRate ← TotalToPay
B TotalToPay ← NumberOfHours * HourlyRate
4. True or false: in Name ← "Sam", the value "Sam" is stored in Name.
A True
B False
5. Name is declared as a STRING. Which assignment matches its data type?
A Name ← "Sam"
B Name ← 25

Match the Pairs

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

Left columnRight column
1Counter ← Counter + 1 AMultiplies the hours by the hourly rate and stores the result in TotalToPay.
2TotalToPay ← NumberOfHours * HourlyRate BWorks out Counter + 1 using the old value, then stores the result back in Counter, so it goes up by one.
3Setting a counter to start at zero before a loop begins CThe left side of ← must be a single variable, not an expression. The value to store must be on the right. Correct: Counter ← Counter + 1
4The variable being assigned to goes on the left of the arrow only, and the value or expression goes on the right DScore ← Score + 5
5Counter + 1 ← Counter EAge is an INTEGER variable, so it cannot store a string. The value must match the data type. Correct: Age ← 16
6Age ← "sixteen" FCounter ← 0

Definitions

<identifier> ← <value>

Fill in the Blanks

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

1.
Counter _ 0
2.
Counter ← _______ + 1
3.
TotalToPay ← NumberOfHours _ HourlyRate
4.
__________ ← NumberOfHours * HourlyRate
5.
DECLARE IsValid : _______ IsValid ← TRUE

Spot the Error

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

Question 1
A DECLARE Counter : INTEGER
B 0 ← Counter
C Counter ← Counter + 1
D OUTPUT Counter
Question 2
A Hours ← 8
B Rate ← 12
C TotalToPay ← 0
D Hours * Rate ← TotalToPay
Question 3
A Counter = 0
B Counter ← Counter + 1
C OUTPUT Counter
Question 4
A DECLARE Name : STRING
B DECLARE Age : INTEGER
C Age ← "fifteen"
D Name ← "Sam"
Question 5
A Price ← "4.50"
B Quantity ← 3
C Cost ← Price * Quantity
D OUTPUT Cost

Answer Key

Assignment with ← (7c)
Section A: Quick Questions
Q1
B — Counter ← 0
Q2
A — Counter ← Counter + 1
Q3
B — TotalToPay ← NumberOfHours * HourlyRate
Q4
A — True
Q5
A — Name ← "Sam"
Section B: Match the Pairs
Q1
Counter ← Counter + 1 → Works out Counter + 1 using the old value, then stores the result back in Counter, so it goes up by one.
Q2
TotalToPay ← NumberOfHours * HourlyRate → Multiplies the hours by the hourly rate and stores the result in TotalToPay.
Q3
Setting a counter to start at zero before a loop begins → Counter ← 0
Q4
The variable being assigned to goes on the left of the arrow only, and the value or expression goes on the right → Score ← Score + 5
Q5
Counter + 1 ← Counter → The left side of ← must be a single variable, not an expression. The value to store must be on the right. Correct: Counter ← Counter + 1
Q6
Age ← "sixteen" → Age is an INTEGER variable, so it cannot store a string. The value must match the data type. Correct: Age ← 16
Section C: Definitions
Q1
<identifier> ← <value> — Assignment stores the value on the right of ← in the variable on the left. The value must match the variable's data type, e.g. Counter ← Counter + 1.
Section D: Fill in the Blanks
Q1
Counter ← 0
Q2
Counter ← Counter + 1
Q3
TotalToPay ← NumberOfHours * HourlyRate
Q4
TotalToPay ← NumberOfHours * HourlyRate
Q5
DECLARE IsValid : BOOLEAN IsValid ← TRUE
Section E: Spot the Error
Q1
B — The variable must be on the left of ←. Here the value 0 is on the left, which is not allowed. It should be Counter ← 0.
Q2
D — Only a single variable can go on the left of ←. The expression Hours * Rate must be on the right: TotalToPay ← Hours * Rate.
Q3
A — In Cambridge pseudocode, = is used for comparison. Storing a value in a variable needs the assignment operator: Counter ← 0.
Q4
C — Age is an INTEGER, so the value assigned must be a whole number. Text in quotes is a STRING. It should be Age ← 15.
Q5
A — The quotes make 4.50 a STRING, so it cannot be multiplied. Price holds a REAL number, so it should be Price ← 4.50.