Meaningful identifiers for arrays

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

Meaningful identifiers for arrays

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which array name is better for storing 30 students' names?
A A
B StudentNames
2. Which statement is correct?
A An array identifier should describe what its elements hold
B An array identifier should be as short as possible
3. Which declaration uses a valid identifier for the array?
A DECLARE Scores : ARRAY[1:10] OF INTEGER
B DECLARE INPUT : ARRAY[1:10] OF INTEGER
4. In pseudocode, are StudentNames and studentnames the same identifier?
A Yes, identifiers are case insensitive
B No, they are two different arrays
5. Which identifier must NOT be used for an array?
A ForecastTemps
B FOR

Match the Pairs

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

Left columnRight column
1DECLARE StudentNames : ARRAY[1:30] OF STRING AINPUT is a keyword, so it cannot be an array identifier. Use a name such as Scores instead.
2DECLARE FOR : ARRAY[1:10] OF INTEGER BA meaningful array identifier: the name tells you the elements are student names
3Identifiers are case insensitive, so StudentNames and STUDENTNAMES refer to the same array CStudentNames[1] ← "Ava" OUTPUT STUDENTNAMES[1]
4A plural name suits an array that holds many values of the same kind, such as several temperatures DNot allowed: FOR is a keyword so it can never be used as an array identifier
5DECLARE A : ARRAY[1:30] OF STRING EThe name A says nothing about the contents. It should be a meaningful name such as StudentNames.
6DECLARE INPUT : ARRAY[1:5] OF INTEGER INPUT[1] ← 20 FDECLARE Temperatures : ARRAY[1:7] OF REAL

Definitions

meaningful identifier

Fill in the Blanks

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

1.
DECLARE StudentNames : ARRAY[1:30] OF STRING ____________[1] ← "Ava"
2.
DECLARE Marks : ARRAY[1:5] OF INTEGER _____[2] ← 75
3.
FOR Index ← 1 TO 3 _____ TeamNames[Index] NEXT Index
4.
DECLARE StudentAges : ARRAY[1:30] OF _______
5.
DECLARE ExamMarks : _____[1:20] OF INTEGER

Spot the Error

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

Question 1
A Scores[1] ← 75
B A[2] ← 82
C OUTPUT Scores[2]
Question 2
A FOR Pos ← 1 TO 3
B INPUT Prices[Pos]
C NEXT Pos
D OUTPUT Price[3]
Question 3
A Colours[1] ← "red"
B OUTPUT[2] ← "blue"
C OUTPUT Colours[2]
Question 4
A FOR Day ← 1 TO 7
B INPUT Names[Day]
C NEXT Day
D OUTPUT Temperatures[1]
Question 5
A StudentNames[1] ← "Ava"
B StudentNames[2] ← "Ben"
C Student Names[3] ← "Cy"

Answer Key

Meaningful identifiers for arrays
Section A: Quick Questions
Q1
B — StudentNames
Q2
A — An array identifier should describe what its elements hold
Q3
A — DECLARE Scores : ARRAY[1:10] OF INTEGER
Q4
A — Yes, identifiers are case insensitive
Q5
B — FOR
Section B: Match the Pairs
Q1
DECLARE StudentNames : ARRAY[1:30] OF STRING → A meaningful array identifier: the name tells you the elements are student names
Q2
DECLARE FOR : ARRAY[1:10] OF INTEGER → Not allowed: FOR is a keyword so it can never be used as an array identifier
Q3
Identifiers are case insensitive, so StudentNames and STUDENTNAMES refer to the same array → StudentNames[1] ← "Ava" OUTPUT STUDENTNAMES[1]
Q4
A plural name suits an array that holds many values of the same kind, such as several temperatures → DECLARE Temperatures : ARRAY[1:7] OF REAL
Q5
DECLARE A : ARRAY[1:30] OF STRING → The name A says nothing about the contents. It should be a meaningful name such as StudentNames.
Q6
DECLARE INPUT : ARRAY[1:5] OF INTEGER INPUT[1] ← 20 → INPUT is a keyword, so it cannot be an array identifier. Use a name such as Scores instead.
Section C: Definitions
Q1
meaningful identifier — An array name that says what its elements hold, like StudentNames not A. It must never be a keyword such as INPUT or FOR, and upper or lower case makes no difference.
Section D: Fill in the Blanks
Q1
DECLARE StudentNames : ARRAY[1:30] OF STRING StudentNames[1] ← "Ava"
Q2
DECLARE Marks : ARRAY[1:5] OF INTEGER MARKS[2] ← 75
Q3
FOR Index ← 1 TO 3 INPUT TeamNames[Index] NEXT Index
Q4
DECLARE StudentAges : ARRAY[1:30] OF INTEGER
Q5
DECLARE ExamMarks : ARRAY[1:20] OF INTEGER
Section E: Spot the Error
Q1
B — A is a meaningless name and it is not the same array as Scores. Use the descriptive identifier Scores: Scores[2] ← 82.
Q2
D — The array is called Prices. Price is a different identifier, so it would refer to an array that does not exist. Use the same plural name: OUTPUT Prices[3].
Q3
B — OUTPUT is a keyword, so it can never be used as an identifier. The array name should be Colours: Colours[2] ← "blue".
Q4
B — The array holds temperatures, so calling it Names is misleading and does not match the rest of the program. Use INPUT Temperatures[Day].
Q5
C — An identifier cannot contain a space. Write the name as one word: StudentNames[3] ← "Cy".