Index out of range

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

Index out of range

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Marks is declared as ARRAY[1:30] OF INTEGER. What happens when the program runs Marks[31] ← 50?
A Index out of range error: 31 is above the upper bound
B The array grows to 31 elements so the value is stored
2. Marks is declared as ARRAY[1:30] OF INTEGER. Which statement uses an index that is out of range?
A OUTPUT Marks[0]
B OUTPUT Marks[30]
3. Names is declared as ARRAY[0:9] OF STRING. Which index is out of range?
A Names[10]
B Names[9]
4. Scores is declared as ARRAY[1:10] OF INTEGER. Which loop causes an off-by-one index error?
A FOR i ← 1 TO 10
B FOR i ← 1 TO 11
5. Which check tests that an index is valid for ARRAY[1:30]?
A IF Index >= 1 OR Index <= 30
B IF Index >= 1 AND Index <= 30

Match the Pairs

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

Left columnRight column
1Marks[31] when Marks is ARRAY[1:30] AIF Index >= 1 AND Index <= 30 THEN
2Marks[0] when Marks is ARRAY[1:30] BThe index 11 is above the upper bound of 10, so the element does not exist. Use an index from 1 to 10.
3Check an index is at least the lower bound and at most the upper bound before using it CIndex above the upper bound, so the element does not exist and the program gives an error
4An off-by-one mistake: the loop goes one step past the upper bound of ARRAY[1:30] DIndex below the lower bound, so the element does not exist and the program gives an error
5DECLARE Scores : ARRAY[1:10] OF INTEGER Scores[10 + 1] <- 50 EFOR Count <- 1 TO 31
6DECLARE Names : ARRAY[1:5] OF STRING OUTPUT Names[0] FThe index 0 is below the lower bound of 1, so the element does not exist. Use an index from 1 to 5.

Definitions

index out of range

Fill in the Blanks

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

1.
DECLARE Marks : ARRAY[1:30] OF INTEGER Marks[__] ← 50
2.
DECLARE Marks : ARRAY[_:30] OF INTEGER Marks[0] ← 50
3.
IF Index >= 1 ___ Index <= 30 THEN OUTPUT Marks[Index] ENDIF
4.
DECLARE Marks : ARRAY[1:30] OF INTEGER Marks[__] ← 100
5.
IF Index < 1 __ Index > 30 THEN OUTPUT "Index out of range" ENDIF

Spot the Error

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

Question 1
A // Marks is ARRAY[1:30]
B Total ← 0
C FOR i ← 1 TO 30
D Total ← Total + Marks[i + 1]
E NEXT i
Question 2
A // Scores is ARRAY[0:9]
B Total ← 0
C Count ← 10
D FOR n ← 0 TO Count
E Total ← Total + Scores[n]
F NEXT n
Question 3
A // Names is ARRAY[1:20]
B INPUT Pos
C IF Pos >= 1 AND Pos <= 21 THEN
D OUTPUT Names[Pos]
E ENDIF
Question 4
A // Marks is ARRAY[1:30]
B Marks[0] ← 55
C Marks[30] ← 62
D OUTPUT Marks[1]
Question 5
A // Temp is ARRAY[1:7]
B Last ← 7
C OUTPUT Temp[Last + 1]
D OUTPUT Temp[1]

Answer Key

Index out of range
Section A: Quick Questions
Q1
A — Index out of range error: 31 is above the upper bound
Q2
A — OUTPUT Marks[0]
Q3
A — Names[10]
Q4
B — FOR i ← 1 TO 11
Q5
B — IF Index >= 1 AND Index <= 30
Section B: Match the Pairs
Q1
Marks[31] when Marks is ARRAY[1:30] → Index above the upper bound, so the element does not exist and the program gives an error
Q2
Marks[0] when Marks is ARRAY[1:30] → Index below the lower bound, so the element does not exist and the program gives an error
Q3
Check an index is at least the lower bound and at most the upper bound before using it → IF Index >= 1 AND Index <= 30 THEN
Q4
An off-by-one mistake: the loop goes one step past the upper bound of ARRAY[1:30] → FOR Count <- 1 TO 31
Q5
DECLARE Scores : ARRAY[1:10] OF INTEGER Scores[10 + 1] <- 50 → The index 11 is above the upper bound of 10, so the element does not exist. Use an index from 1 to 10.
Q6
DECLARE Names : ARRAY[1:5] OF STRING OUTPUT Names[0] → The index 0 is below the lower bound of 1, so the element does not exist. Use an index from 1 to 5.
Section C: Definitions
Q1
index out of range — An error caused by using an index below the lower bound or above the upper bound, so the element does not exist. Example: Marks[31] in ARRAY[1:30] or Marks[0] when the lower bound is 1.
Section D: Fill in the Blanks
Q1
DECLARE Marks : ARRAY[1:30] OF INTEGER Marks[31] ← 50
Q2
DECLARE Marks : ARRAY[1:30] OF INTEGER Marks[0] ← 50
Q3
IF Index >= 1 AND Index <= 30 THEN OUTPUT Marks[Index] ENDIF
Q4
DECLARE Marks : ARRAY[1:30] OF INTEGER Marks[30] ← 100
Q5
IF Index < 1 OR Index > 30 THEN OUTPUT "Index out of range" ENDIF
Section E: Spot the Error
Q1
D — When i is 30, Marks[i + 1] is Marks[31]. The upper bound is 30, so this index is out of range. It should be Marks[i].
Q2
D — Count is 10, so n reaches 10. Scores only goes up to index 9, so Scores[10] is out of range. The loop must end at Count - 1.
Q3
C — The upper bound is 20, so the check must use <= 20. With <= 21, the index 21 passes the check and Names[21] is out of range.
Q4
B — The lower bound is 1, so Marks[0] does not exist and the index is out of range. The first element is Marks[1].
Q5
C — Last is 7, the upper bound. Temp[Last + 1] is Temp[8], which does not exist. This off-by-one mistake should be Temp[Last].