Writing a value into an array element

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

Writing a value into an array element

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line stores the string "Ali" in the first element of StudentNames?
A StudentNames(1) = "Ali"
B StudentNames[1] ← "Ali"
2. StudentNames is an ARRAY OF STRING. Which assignment is correct?
A StudentNames[2] ← "Bea"
B StudentNames[2] ← Bea
3. Scores is an ARRAY[1:5] OF INTEGER. Which assignment matches the data type?
A Scores[3] ← 85
B Scores[3] ← "eighty-five"
4. StudentNames[1] holds "Ali". After StudentNames[1] ← "Sam", what does element 1 hold?
A "Sam"
B "Ali" and "Sam"
5. Which statement is true about writing to an array element?
A Every element in the array changes
B Only the element at the given index changes

Match the Pairs

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

Left columnRight column
1StudentNames[1] ← "Ali" AColours[2] ← "Blue"
2Scores[3] ← 50 Scores[3] ← 72 BMarks[4] ← 85
3A string value written into a STRING array element must be inside double quotes CThe string Ali has no double quotes, so it is read as a variable name instead of text. It should be "Ali".
4The value must match the array data type, so an INTEGER array element is given a whole number DElement 3 ends up holding 72, because the second assignment replaces the old value 50
5StudentNames[1] ← Ali EMarks is an INTEGER array, so a string cannot be stored in it. The value must be a whole number such as 90.
6Marks[2] ← "high" FStores the string "Ali" in element 1 of the StudentNames array

Definitions

StudentNames[1] ← "Ali"

Fill in the Blanks

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

1.
StudentNames[1] _ "Ali"
2.
DECLARE StudentNames : ARRAY[1:3] OF STRING ____________[2] ← "Mia"
3.
StudentNames[1] ← "Ali" StudentNames[_] ← "Ben"
4.
DECLARE Marks : ARRAY[1:5] OF _______ Marks[1] ← 85
5.
DECLARE StudentNames : ARRAY[1:3] OF ______ StudentNames[3] ← "Zoe"

Spot the Error

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

Question 1
A StudentNames[1] ← "Ali"
B StudentNames[2] ← "Bea"
C StudentNames[3] = "Cy"
D OUTPUT StudentNames[3]
Question 2
A Fruit[1] ← "Apple"
B Fruit[2] ← "Pear"
C Fruit[3] ← Plum
D OUTPUT Fruit[3]
Question 3
A // Scores holds INTEGER values
B Scores[1] ← 45
C Scores[2] ← "60"
D OUTPUT Scores[2]
Question 4
A Prices[1] ← 2.5
B Prices(2) ← 3.75
C Prices[3] ← 1.2
Question 5
A 21 ← Temp[1]
B Temp[2] ← 19
C OUTPUT Temp[1]

Answer Key

Writing a value into an array element
Section A: Quick Questions
Q1
B — StudentNames[1] ← "Ali"
Q2
A — StudentNames[2] ← "Bea"
Q3
A — Scores[3] ← 85
Q4
A — "Sam"
Q5
B — Only the element at the given index changes
Section B: Match the Pairs
Q1
StudentNames[1] ← "Ali" → Stores the string "Ali" in element 1 of the StudentNames array
Q2
Scores[3] ← 50 Scores[3] ← 72 → Element 3 ends up holding 72, because the second assignment replaces the old value 50
Q3
A string value written into a STRING array element must be inside double quotes → Colours[2] ← "Blue"
Q4
The value must match the array data type, so an INTEGER array element is given a whole number → Marks[4] ← 85
Q5
StudentNames[1] ← Ali → The string Ali has no double quotes, so it is read as a variable name instead of text. It should be "Ali".
Q6
Marks[2] ← "high" → Marks is an INTEGER array, so a string cannot be stored in it. The value must be a whole number such as 90.
Section C: Definitions
Q1
StudentNames[1] ← "Ali" — Stores a value in one array element using the assignment operator ←. The value must match the array's data type, and it replaces any old value already in that element.
Section D: Fill in the Blanks
Q1
StudentNames[1] ← "Ali"
Q2
DECLARE StudentNames : ARRAY[1:3] OF STRING StudentNames[2] ← "Mia"
Q3
StudentNames[1] ← "Ali" StudentNames[1] ← "Ben"
Q4
DECLARE Marks : ARRAY[1:5] OF INTEGER Marks[1] ← 85
Q5
DECLARE StudentNames : ARRAY[1:3] OF STRING StudentNames[3] ← "Zoe"
Section E: Spot the Error
Q1
C — Assigning a value to an array element uses the assignment operator ←, not =. It should be StudentNames[3] ← "Cy".
Q2
C — A string value must be written in double quotes. Without them, Plum looks like a variable name. It should be Fruit[3] ← "Plum".
Q3
C — The array holds INTEGER values, so the value must match that data type. "60" is a string. It should be Scores[2] ← 60.
Q4
B — An array index goes in square brackets, not round brackets. It should be Prices[2] ← 3.75.
Q5
A — The array element goes on the left of ← and the value goes on the right. The value is stored in the element. It should be Temp[1] ← 21.