What a variable is

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

What a variable is

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement describes a variable?
A A named storage location whose value can change while the program runs
B A named storage location whose value can never change once set
2. A program sets Score ← 0 and later Score ← 10. What is true about Score?
A It now holds both 0 and 10 at the same time
B Its value changed from 0 to 10, so it is a variable
3. What does the name of a variable, such as Score, do?
A It is the value that is stored in memory
B It labels a place in memory where a value is stored
4. Why do programs use variables?
A To remember values such as a player's score while the program runs
B To make the program run without using any memory
5. Lives ← 3 is followed by Lives ← 2. What is the value of Lives afterwards?
A 2
B 3

Match the Pairs

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

Left columnRight column
1Score ← 0 AScore ← Score + 5
2Score ← 10 BReplaces the old value of Score with 10, showing that a variable's value can change
3A variable is a name that labels a place in memory where a value is kept CWrong: after Score ← 10 the old value is gone. Score now holds 10, because a variable's value can be replaced.
4A program can change the value held in a variable while it runs DDECLARE Score : INTEGER
5Score ← 0 Score ← 10 // Score is still 0 now EStores the value 0 in the variable Score, so the program remembers the starting score
6// A variable's value can never change once it is set Score ← 0 FWrong: a variable's value can change while the program runs. It is a constant whose value cannot change.

Definitions

variable

Fill in the Blanks

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

1.
Score ← 0 _____ ← 10 OUTPUT Score
2.
Lives _ 3 Lives ← 2 OUTPUT Lives
3.
Score ← 0 Score ← __ OUTPUT Score
4.
Level ← 1 Level ← 2 ______ Level
5.
Lives ← 3 Lives ← 2 OUTPUT _____

Spot the Error

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

Question 1
A DECLARE Score : INTEGER
B Score ← 0
C OUTPUT Score
D 10 ← Score
E OUTPUT Score
Question 2
A DECLARE Lives : INTEGER
B Lives ← 3
C Lives = Lives - 1
D OUTPUT Lives
Question 3
A DECLARE Score : INTEGER
B Score 0
C Score ← 10
D OUTPUT Score
Question 4
A DECLARE Age : INTEGER
B Age ← 14
C OUTPUT Age
D Agee ← 15
E OUTPUT Age
Question 5
A DECLARE Name : STRING
B Name ← "Sam"
C Name ← Alex
D OUTPUT Name

Answer Key

What a variable is
Section A: Quick Questions
Q1
A — A named storage location whose value can change while the program runs
Q2
B — Its value changed from 0 to 10, so it is a variable
Q3
B — It labels a place in memory where a value is stored
Q4
A — To remember values such as a player's score while the program runs
Q5
A — 2
Section B: Match the Pairs
Q1
Score ← 0 → Stores the value 0 in the variable Score, so the program remembers the starting score
Q2
Score ← 10 → Replaces the old value of Score with 10, showing that a variable's value can change
Q3
A variable is a name that labels a place in memory where a value is kept → DECLARE Score : INTEGER
Q4
A program can change the value held in a variable while it runs → Score ← Score + 5
Q5
Score ← 0 Score ← 10 // Score is still 0 now → Wrong: after Score ← 10 the old value is gone. Score now holds 10, because a variable's value can be replaced.
Q6
// A variable's value can never change once it is set Score ← 0 → Wrong: a variable's value can change while the program runs. It is a constant whose value cannot change.
Section C: Definitions
Q1
variable — A named storage location in memory whose value can change while a program runs, such as Score going from 0 to 10. Programs use it to remember values.
Section D: Fill in the Blanks
Q1
Score ← 0 Score ← 10 OUTPUT Score
Q2
Lives ← 3 Lives ← 2 OUTPUT Lives
Q3
Score ← 0 Score ← 10 OUTPUT Score
Q4
Level ← 1 Level ← 2 OUTPUT Level
Q5
Lives ← 3 Lives ← 2 OUTPUT Lives
Section E: Spot the Error
Q1
D — The variable name must be on the left of ←, because the value is stored into the variable. A number cannot be a storage location. It should be Score ← 10.
Q2
C — A new value is stored in a variable with the assignment arrow ←, not =. It should be Lives ← Lives - 1, which replaces 3 with 2.
Q3
B — A value is stored in a variable using the assignment operator ←. Without it, the variable is never given its starting value. It should be Score ← 0.
Q4
D — Agee is a different name from Age and was never declared. The value of the variable Age would not change. It should be Age ← 15.
Q5
C — Text values must be in double quotes. Without quotes, Alex looks like another variable name. It should be Name ← "Alex".