Using an array element

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

Using an array element

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line stores the name Ali in the first element of the array StudentNames?
A StudentNames(1) ← "Ali"
B StudentNames[1] ← "Ali"
2. The array Scores is declared as ARRAY[1:5] OF INTEGER. Which index is valid?
A Scores[5]
B Scores[6]
3. What does this line do? StudentNames[n + 1] ← StudentNames[n]
A Copies the value at position n into the next position
B Adds 1 to every name in the array
4. Which of these correctly reads one element of the array StudentNames into a variable called First?
A First ← StudentNames[1]
B First ← StudentNames
5. StudentNames is declared as ARRAY[1:30] OF STRING. Which statement is out of range?
A StudentNames[30] ← "Zoe"
B StudentNames[0] ← "Zoe"

Match the Pairs

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

Left columnRight column
1StudentNames[1] ← "Ali" AStores the value "Ali" in the element at index 1 of the StudentNames array
2StudentNames[n + 1] ← StudentNames[n] BIndex 0 is outside the array's bounds. The lower bound is 1, so the index must be between 1 and 30.
3To change one element of an array, write the array name, one index in square brackets, then ← and the new value CStudentNames[3] ← "Mia"
4To read an element, use the array name and one index in square brackets, for example to output it DOUTPUT StudentNames[2]
5StudentNames[0] ← "Ali" (array declared as ARRAY[1:30]) ECopies the value at index n into the next element, whose index is n + 1
6StudentNames(1) ← "Ali" FRound brackets are used instead of square brackets. The index must be written in square brackets.

Definitions

StudentNames[1]

Fill in the Blanks

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

1.
____________[1] ← "Ali"
2.
DECLARE StudentNames : ARRAY[1:5] OF STRING StudentNames[_] ← "Zoe"
3.
StudentNames[n + 1] ← StudentNames[_]
4.
StudentNames[1] _ "Ali"
5.
______ StudentNames[2]

Spot the Error

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

Question 1
A // Names: ARRAY[1:5]
B Names(1) ← "Ali"
C OUTPUT Names[1]
Question 2
A [2]Scores ← 15
B Scores[3] ← Scores[2] + 5
C OUTPUT Scores[3]
Question 3
A // Marks: ARRAY[1:10]
B Marks[11] ← 75
C OUTPUT Marks[10]
Question 4
A // Temp: ARRAY[0:6]
B Temp[0] ← 12
C Temp[7] ← 18
D OUTPUT Temp[6]
Question 5
A // Names: ARRAY[1:5]
B n ← 2
C Names[n + 1] ← Names[n]
D OUTPUT Names[n + 1

Answer Key

Using an array element
Section A: Quick Questions
Q1
B — StudentNames[1] ← "Ali"
Q2
A — Scores[5]
Q3
A — Copies the value at position n into the next position
Q4
A — First ← StudentNames[1]
Q5
B — StudentNames[0] ← "Zoe"
Section B: Match the Pairs
Q1
StudentNames[1] ← "Ali" → Stores the value "Ali" in the element at index 1 of the StudentNames array
Q2
StudentNames[n + 1] ← StudentNames[n] → Copies the value at index n into the next element, whose index is n + 1
Q3
To change one element of an array, write the array name, one index in square brackets, then ← and the new value → StudentNames[3] ← "Mia"
Q4
To read an element, use the array name and one index in square brackets, for example to output it → OUTPUT StudentNames[2]
Q5
StudentNames[0] ← "Ali" (array declared as ARRAY[1:30]) → Index 0 is outside the array's bounds. The lower bound is 1, so the index must be between 1 and 30.
Q6
StudentNames(1) ← "Ali" → Round brackets are used instead of square brackets. The index must be written in square brackets.
Section C: Definitions
Q1
StudentNames[1] — Reads or changes one array element: the array name, then one index in square brackets. The index must be between the array's lower and upper bound. StudentNames[1] ← "Ali" stores a value.
Section D: Fill in the Blanks
Q1
StudentNames[1] ← "Ali"
Q2
DECLARE StudentNames : ARRAY[1:5] OF STRING StudentNames[5] ← "Zoe"
Q3
StudentNames[n + 1] ← StudentNames[n]
Q4
StudentNames[1] ← "Ali"
Q5
OUTPUT StudentNames[2]
Section E: Spot the Error
Q1
B — An array index goes in square brackets, not round brackets. It should be Names[1] ← "Ali".
Q2
A — The array name comes first, then the index in square brackets. It should be Scores[2] ← 15.
Q3
B — The array only goes up to index 10, so index 11 is above the upper bound. It should be Marks[10] ← 75.
Q4
C — The upper bound of Temp is 6, so index 7 does not exist. It should be Temp[6] ← 18.
Q5
D — The square bracket is not closed. Every index needs both brackets: OUTPUT Names[n + 1].