Lower bound and upper bound

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

Lower bound and upper bound

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In ARRAY[1:30], what is the lower bound?
A 30
B 1
2. In ARRAY[1:30], what is the upper bound?
A 30
B 1
3. Which shows the bounds written in the correct order?
A ARRAY[30:1]
B ARRAY[1:30]
4. The lower bound of an array is...
A the index of the first element
B the number of elements in the array
5. Which is the correct way to separate the bounds in an array declaration?
A ARRAY[1:30]
B ARRAY[1-30]

Match the Pairs

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

Left columnRight column
1ARRAY[1:30] ADECLARE Marks : ARRAY[1:25] OF INTEGER
2ARRAY[0:9] BThe bounds are written the wrong way round. The lower bound must come first and the upper bound second, as l:u, so it should be ARRAY[1:30].
3The lower bound is the index of the first element, written before the colon CThe two bounds are separated by a comma instead of a colon. A 1D array needs l:u, so it should be ARRAY[1:20].
4The upper bound is the index of the last element, written after the colon DAn array whose first element has index 1 (lower bound) and whose last element has index 30 (upper bound)
5DECLARE Scores : ARRAY[30:1] OF INTEGER EThe lower bound is 0 and the upper bound is 9, so the indexes run 0, 1, 2 ... 9
6DECLARE Ages : ARRAY[1,20] OF INTEGER FDECLARE Names : ARRAY[1:40] OF STRING

Definitions

ARRAY[1:30]

Fill in the Blanks

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

1.
DECLARE Scores : ARRAY[_:30] OF INTEGER // 30 elements, first index is 1
2.
DECLARE Names : ARRAY[1:__] OF STRING // the last element has index 30
3.
DECLARE Temp : ARRAY[_:6] OF REAL // 7 elements, first index is zero
4.
DECLARE Marks : ARRAY[1_30] OF INTEGER
5.
DECLARE Scores : ARRAY[1:30] OF INTEGER FOR Index ← 1 TO __ NEXT Index

Spot the Error

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

Question 1
A // Bounds of ARRAY[1:30]
B lower ← 30
C upper ← 30
Question 2
A // Temp is ARRAY[1:7]
B Temp[1] ← 18
C Temp[7] ← 25
D OUTPUT Temp[8]
Question 3
A // Names is ARRAY[0:9]
B first ← 0
C last ← 10
Question 4
A // Prices holds 12 values
B // Declared as ARRAY[12:1]
C OUTPUT Prices[1]
Question 5
A // Pupils is ARRAY[1,30]
B FOR n ← 1 TO 30
C OUTPUT Pupils[n]
D NEXT n

Answer Key

Lower bound and upper bound
Section A: Quick Questions
Q1
B — 1
Q2
A — 30
Q3
B — ARRAY[1:30]
Q4
A — the index of the first element
Q5
A — ARRAY[1:30]
Section B: Match the Pairs
Q1
ARRAY[1:30] → An array whose first element has index 1 (lower bound) and whose last element has index 30 (upper bound)
Q2
ARRAY[0:9] → The lower bound is 0 and the upper bound is 9, so the indexes run 0, 1, 2 ... 9
Q3
The lower bound is the index of the first element, written before the colon → DECLARE Marks : ARRAY[1:25] OF INTEGER
Q4
The upper bound is the index of the last element, written after the colon → DECLARE Names : ARRAY[1:40] OF STRING
Q5
DECLARE Scores : ARRAY[30:1] OF INTEGER → The bounds are written the wrong way round. The lower bound must come first and the upper bound second, as l:u, so it should be ARRAY[1:30].
Q6
DECLARE Ages : ARRAY[1,20] OF INTEGER → The two bounds are separated by a comma instead of a colon. A 1D array needs l:u, so it should be ARRAY[1:20].
Section C: Definitions
Q1
ARRAY[1:30] — In ARRAY[l:u], l is the lower bound (index of the first element) and u is the upper bound (index of the last). Here the lower bound is 1 and the upper bound is 30.
Section D: Fill in the Blanks
Q1
DECLARE Scores : ARRAY[1:30] OF INTEGER // 30 elements, first index is 1
Q2
DECLARE Names : ARRAY[1:30] OF STRING // the last element has index 30
Q3
DECLARE Temp : ARRAY[0:6] OF REAL // 7 elements, first index is zero
Q4
DECLARE Marks : ARRAY[1:30] OF INTEGER
Q5
DECLARE Scores : ARRAY[1:30] OF INTEGER FOR Index ← 1 TO 30 NEXT Index
Section E: Spot the Error
Q1
B — In ARRAY[1:30] the lower bound is the index of the first element, which is 1. It should be lower ← 1. The value 30 is the upper bound.
Q2
D — The upper bound of ARRAY[1:7] is 7, so the last valid index is 7. Temp[8] is outside the bounds. It should be OUTPUT Temp[7].
Q3
C — In ARRAY[0:9] the upper bound is 9, which is the index of the last element. It should be last ← 9. Index 10 does not exist.
Q4
B — Bounds are written lower:upper, so the lower bound comes first. It should be ARRAY[1:12]. The lower bound cannot be larger than the upper bound.
Q5
A — The lower and upper bounds are separated by a colon, written l:u. It should be ARRAY[1:30]. A comma is used to separate the dimensions of a 2D array.