Reading a value from an array element

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

Reading a value from an array element

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Marks[3] ← 70 and then OUTPUT Marks[3]. What is displayed?
A Marks[3]
B 70
2. Which line READS a value from an array element?
A OUTPUT StudentNames[1]
B StudentNames[1] ← "Ava"
3. Marks[1] is 40 and Marks[2] is 55. What is Total after Total ← Marks[1] + Marks[2]?
A 95
B 3
4. Score ← Marks[2] has just run. Which statement is true?
A Score now holds a copy of the value in Marks[2]
B Score now holds the number 2
5. Marks[4] is 60. After OUTPUT Marks[4], what is stored in Marks[4]?
A 0
B 60

Match the Pairs

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

Left columnRight column
1OUTPUT StudentNames[1] ABest ← Scores[3]
2Total ← Marks[1] + Marks[2] BCopy ← Marks[2] OUTPUT Marks[2]
3Copying one element into a variable: the value is read from the array and stored in the variable CReads the values in elements 1 and 2 of Marks, adds them and stores the result in Total
4Reading an element does not change it: the element keeps its value after being used DThe assignment is the wrong way round. This writes Total into the array element instead of reading the element. To read it, write Total ← Marks[1].
5OUTPUT StudentNames(1) ERound brackets are used instead of square brackets. An array element must be read as StudentNames[1].
6Marks[1] ← Total FDisplays the value stored in element 1 of the StudentNames array

Definitions

OUTPUT StudentNames[1]

Fill in the Blanks

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

1.
OUTPUT ____________[1]
2.
OUTPUT StudentNames[_]
3.
Total ← Marks[1] _ Marks[2]
4.
Score ← _____[3]
5.
Best ← Marks[2] OUTPUT Best OUTPUT Marks[_]

Spot the Error

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

Question 1
A Marks[1] ← 40
B Marks[2] ← 55
C Total ← Marks[1] + Marks[2]
D OUTPUT "Total"
Question 2
A StudentNames[1] ← "Ava"
B OUTPUT StudentNames(1)
C StudentNames[2] ← "Ben"
D OUTPUT StudentNames[2]
Question 3
A Score[1] ← 8
B Score[2] ← 6
C Copy ← Score[1]
D OUTPUT Copy[1]
Question 4
A Quiz[1] = 7
B Quiz[2] ← 9
C Total ← Quiz[1] + Quiz[2]
D OUTPUT Total
Question 5
A StudentNames[1] ← Zara
B StudentNames[2] ← "Omar"
C First ← StudentNames[1]
D OUTPUT First

Answer Key

Reading a value from an array element
Section A: Quick Questions
Q1
B — 70
Q2
A — OUTPUT StudentNames[1]
Q3
A — 95
Q4
A — Score now holds a copy of the value in Marks[2]
Q5
B — 60
Section B: Match the Pairs
Q1
OUTPUT StudentNames[1] → Displays the value stored in element 1 of the StudentNames array
Q2
Total ← Marks[1] + Marks[2] → Reads the values in elements 1 and 2 of Marks, adds them and stores the result in Total
Q3
Copying one element into a variable: the value is read from the array and stored in the variable → Best ← Scores[3]
Q4
Reading an element does not change it: the element keeps its value after being used → Copy ← Marks[2] OUTPUT Marks[2]
Q5
OUTPUT StudentNames(1) → Round brackets are used instead of square brackets. An array element must be read as StudentNames[1].
Q6
Marks[1] ← Total → The assignment is the wrong way round. This writes Total into the array element instead of reading the element. To read it, write Total ← Marks[1].
Section C: Definitions
Q1
OUTPUT StudentNames[1] — Reading an element: Name[index] in OUTPUT or an expression gives the value stored in that element. Reading only uses the value, so the element stays unchanged.
Section D: Fill in the Blanks
Q1
OUTPUT StudentNames[1]
Q2
OUTPUT StudentNames[1]
Q3
Total ← Marks[1] + Marks[2]
Q4
Score ← Marks[3]
Q5
Best ← Marks[2] OUTPUT Best OUTPUT Marks[2]
Section E: Spot the Error
Q1
D — Quotes make OUTPUT print the word Total instead of the value stored in the variable. It should be OUTPUT Total, which displays 95.
Q2
B — An element is read using square brackets around the index, not round brackets. It should be OUTPUT StudentNames[1].
Q3
D — Copy is an ordinary variable that holds one copied value, not an array, so it has no index. It should be OUTPUT Copy.
Q4
A — A value is stored with the assignment arrow ←, not =. Without it, Quiz[1] never holds 7, so the later reads give the wrong total. It should be Quiz[1] ← 7.
Q5
A — Text must be in quotes. Without them Zara is treated as a variable, so the element would not hold the string read later. It should be StudentNames[1] ← "Zara".