Declaring a 1D array

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

Declaring a 1D array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly declares an array to store 30 student names, numbered from 1?
A DECLARE StudentNames : ARRAY[1:30] OF STRING
B DECLARE StudentNames : ARRAY[1,30] OF STRING
2. In DECLARE Scores : ARRAY[1:10] OF INTEGER, what is the lower bound?
A 10
B 1
3. How many elements does ARRAY[1:30] hold?
A 30
B 29
4. Which statement about the data type in an array declaration is true?
A Every element has the same data type
B Each element can have a different data type
5. Which declaration is written correctly for an array of 5 whole-number marks?
A DECLARE Marks : ARRAY[1:5] INTEGER
B DECLARE Marks : ARRAY[1:5] OF INTEGER

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 AA comma is used between the bounds. It must be a colon, so write [1:30].
2DECLARE Marks : ARRAY[1:25] OF INTEGER BCreates an array of 25 INTEGER elements, numbered from 1 to 25
3An array declaration must state the lower bound and upper bound inside square brackets, separated by a colon CThe data type is missing. OF STRING must be added after the bounds.
4Every element in an array has the same data type, which is stated after OF DDECLARE Scores : ARRAY[1:10] OF INTEGER
5DECLARE StudentNames : ARRAY[1,30] OF STRING ECreates an array of 30 STRING elements, numbered from 1 to 30
6DECLARE StudentNames : ARRAY[1:30] FDECLARE Names : ARRAY[1:20] OF STRING

Definitions

DECLARE StudentNames : ARRAY[1:30] OF STRING

Fill in the Blanks

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

1.
_______ StudentNames : ARRAY[1:30] OF STRING
2.
DECLARE StudentNames : _____[1:30] OF STRING
3.
DECLARE StudentNames : ARRAY[_:30] OF STRING
4.
DECLARE StudentNames : ARRAY[1:30] __ STRING
5.
// 30 whole-number marks DECLARE Marks : ARRAY[1:30] OF _______

Spot the Error

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

Question 1
A DECLARE T : ARRAY[1:7] OF REAL
B T[1] ← 18.5
C T[2] ← 21.0
D T[8] ← 19.5
Question 2
A // Seven daily temperatures
B DECLARE T : ARRAY(1:7) OF REAL
C T[1] ← 18.5
Question 3
A DECLARE G : ARRAY[1:5] OF CHAR
B G[1] ← 'A'
C G[2] ← 7
D OUTPUT G[2]
Question 4
A DECLARE T : ARRAY[1:7] OF REAL
B FOR I ← 0 TO 7
C INPUT T[I]
D NEXT I
Question 5
A DECLARE G : ARRAY[1:5] OF CHAR
B G(1) ← 'A'
C OUTPUT G[1]

Answer Key

Declaring a 1D array
Section A: Quick Questions
Q1
A — DECLARE StudentNames : ARRAY[1:30] OF STRING
Q2
B — 1
Q3
A — 30
Q4
A — Every element has the same data type
Q5
B — DECLARE Marks : ARRAY[1:5] OF INTEGER
Section B: Match the Pairs
Q1
DECLARE StudentNames : ARRAY[1:30] OF STRING → Creates an array of 30 STRING elements, numbered from 1 to 30
Q2
DECLARE Marks : ARRAY[1:25] OF INTEGER → Creates an array of 25 INTEGER elements, numbered from 1 to 25
Q3
An array declaration must state the lower bound and upper bound inside square brackets, separated by a colon → DECLARE Scores : ARRAY[1:10] OF INTEGER
Q4
Every element in an array has the same data type, which is stated after OF → DECLARE Names : ARRAY[1:20] OF STRING
Q5
DECLARE StudentNames : ARRAY[1,30] OF STRING → A comma is used between the bounds. It must be a colon, so write [1:30].
Q6
DECLARE StudentNames : ARRAY[1:30] → The data type is missing. OF STRING must be added after the bounds.
Section C: Definitions
Q1
DECLARE StudentNames : ARRAY[1:30] OF STRING — Creates a fixed-length array. Its 30 elements are numbered from lower bound 1 to upper bound 30, and every element holds the same data type, here STRING.
Section D: Fill in the Blanks
Q1
DECLARE StudentNames : ARRAY[1:30] OF STRING
Q2
DECLARE StudentNames : ARRAY[1:30] OF STRING
Q3
DECLARE StudentNames : ARRAY[1:30] OF STRING
Q4
DECLARE StudentNames : ARRAY[1:30] OF STRING
Q5
// 30 whole-number marks DECLARE Marks : ARRAY[1:30] OF INTEGER
Section E: Spot the Error
Q1
D — The array is declared with bounds 1:7, so the upper bound is 7. Index 8 does not exist. It should be an index from 1 to 7, such as T[3] ← 19.5.
Q2
B — The bounds of an array must be written in square brackets, not round brackets. It should be ARRAY[1:7].
Q3
C — Every element of an array holds the same data type. G is an array of CHAR, so it cannot store the number 7. It should be G[2] ← 'B'.
Q4
B — The lower bound is 1, so the first element is T[1]. Starting the loop at 0 would use T[0], which does not exist. It should be FOR I ← 1 TO 7.
Q5
B — An element is chosen using square brackets around its index. G(1) uses round brackets. It should be G[1] ← 'A'.