Array bounds starting at zero or one

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

Array bounds starting at zero or one

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. DECLARE Scores : ARRAY[0:29] OF INTEGER. What is the index of the first element?
A 0
B 1
2. DECLARE Names : ARRAY[1:30] OF STRING. Which element is the first one?
A Names[1]
B Names[0]
3. How many elements do ARRAY[0:29] and ARRAY[1:30] each hold?
A ARRAY[0:29] holds 29 and ARRAY[1:30] holds 30
B Both hold 30 elements
4. Which is the better practice when declaring an array in pseudocode?
A State the lower bound explicitly, such as ARRAY[1:30]
B Leave the lower bound out and let the computer assume it
5. Which lower bound does the Cambridge IGCSE syllabus generally use in its pseudocode?
A 0
B 1

Match the Pairs

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

Left columnRight column
1DECLARE Scores : ARRAY[0:29] OF INTEGER AThe first index is 1 and the last is 30, so the array holds 30 elements
2DECLARE Scores : ARRAY[1:30] OF INTEGER BDECLARE Names : ARRAY[1:20] OF STRING
3Stating the lower bound explicitly in the declaration shows whether the first element is at index 0 or 1 CARRAY[0:30] holds 31 elements (0 to 30). For 30 elements starting at 0, use ARRAY[0:29]. Or use ARRAY[1:30] to start at 1.
4In an array with lower bound 0, the first element is read using index 0 DOUTPUT Scores[0]
5DECLARE Marks : ARRAY[0:30] OF INTEGER // the student wants exactly 30 elements EIndex 30 is out of range because the upper bound is 29. The last element is Marks[29], and the first is Marks[0].
6DECLARE Marks : ARRAY[0:29] OF INTEGER OUTPUT Marks[30] FThe first index is 0 and the last is 29, so the array holds 30 elements

Definitions

ARRAY[1:30]

Fill in the Blanks

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

1.
DECLARE Names : ARRAY[_:30] OF STRING
2.
DECLARE Scores : ARRAY[0:__] OF INTEGER
3.
DECLARE Marks : ARRAY[0:29] OF INTEGER OUTPUT Marks[_]
4.
DECLARE Marks : ARRAY[0:29] OF INTEGER FOR Index ← _ TO 29 OUTPUT Marks[Index]
5.
DECLARE Names : ARRAY[1:30] OF STRING OUTPUT Names[__]

Spot the Error

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

Question 1
A DECLARE S : ARRAY[5:1] OF REAL
B S[1] ← 7.5
C OUTPUT S[1]
Question 2
A DECLARE M : ARRAY[0:4] OF CHAR
B M[5] ← 'A'
C M[4] ← 'E'
D OUTPUT M[0]
Question 3
A DECLARE P : ARRAY[9] OF CHAR
B FOR I ← 0 TO 9
C P[I] ← '*'
D NEXT I
Question 4
A DECLARE T : ARRAY[1:9] OF CHAR
B FOR I ← 0 TO 9
C T[I] ← 'x'
D NEXT I
Question 5
A DECLARE W : ARRAY[1:3] OF CHAR
B W[1] ← 'a'
C W[0] ← 'c'
D OUTPUT W[1]

Answer Key

Array bounds starting at zero or one
Section A: Quick Questions
Q1
A — 0
Q2
A — Names[1]
Q3
B — Both hold 30 elements
Q4
A — State the lower bound explicitly, such as ARRAY[1:30]
Q5
B — 1
Section B: Match the Pairs
Q1
DECLARE Scores : ARRAY[0:29] OF INTEGER → The first index is 0 and the last is 29, so the array holds 30 elements
Q2
DECLARE Scores : ARRAY[1:30] OF INTEGER → The first index is 1 and the last is 30, so the array holds 30 elements
Q3
Stating the lower bound explicitly in the declaration shows whether the first element is at index 0 or 1 → DECLARE Names : ARRAY[1:20] OF STRING
Q4
In an array with lower bound 0, the first element is read using index 0 → OUTPUT Scores[0]
Q5
DECLARE Marks : ARRAY[0:30] OF INTEGER // the student wants exactly 30 elements → ARRAY[0:30] holds 31 elements (0 to 30). For 30 elements starting at 0, use ARRAY[0:29]. Or use ARRAY[1:30] to start at 1.
Q6
DECLARE Marks : ARRAY[0:29] OF INTEGER OUTPUT Marks[30] → Index 30 is out of range because the upper bound is 29. The last element is Marks[29], and the first is Marks[0].
Section C: Definitions
Q1
ARRAY[1:30] — The number before the colon is the lower bound, the first index. ARRAY[1:30] starts at 1, ARRAY[0:29] starts at 0. Both hold 30 elements, so always state the lower bound.
Section D: Fill in the Blanks
Q1
DECLARE Names : ARRAY[1:30] OF STRING
Q2
DECLARE Scores : ARRAY[0:29] OF INTEGER
Q3
DECLARE Marks : ARRAY[0:29] OF INTEGER OUTPUT Marks[0]
Q4
DECLARE Marks : ARRAY[0:29] OF INTEGER FOR Index ← 0 TO 29 OUTPUT Marks[Index]
Q5
DECLARE Names : ARRAY[1:30] OF STRING OUTPUT Names[30]
Section E: Spot the Error
Q1
A — The lower bound must come first, before the colon, and the upper bound after it. It should be ARRAY[1:5], which holds 5 elements.
Q2
B — The bounds are 0 to 4, so index 5 is out of range. The first element is M[0], so the line should be M[0] ← 'A'.
Q3
A — The declaration must state both the lower and the upper bound, like [0:9]. Writing only one number does not say where the indexes start.
Q4
B — The lower bound is 1, so there is no T[0]. The loop would start at an index that does not exist. It should be FOR I ← 1 TO 9.
Q5
C — The lower bound is 1, so index 0 does not exist. The last element of ARRAY[1:3] is W[3], so the line should be W[3] ← 'c'.