Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Subject:
Topics:
Avg
score:
–
Topics
done:
0/32

1D and 2D arrays

Practise 41 exam-style questions on 1D and 2D arrays for Cambridge IGCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with an AI tutor on hand if you get stuck.

  • 41 questions
  • 1–8 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

2 marks

Write a pseudocode statement to declare a one-dimensional array, CarRegs, that can store the registration numbers of 50 cars. The first index is 1.

Model answer

DECLARE CarRegs : ARRAY[1:50] OF STRING

Explanation

An array declaration gives the identifier, the lower and upper bounds of the index in square brackets, and the data type of every element. Registration numbers such as AB12 CDE mix letters and digits, so STRING is needed.

Mark scheme
  1. 1 mark

    MP1 DECLARE CarRegs : ARRAY[1:50]

  2. 1 mark

    MP2 OF STRING

    Registration numbers contain letters, so do not accept INTEGER or CHAR.

Question 2

2 marks

A treasure hunt game stores a map in the two-dimensional array Map[1:5, 1:5] OF CHAR. The first index is the row and the second index is the column.

Write one statement that stores the character 'T' in row 4, column 2, and one statement that outputs the character in row 1, column 5.

Model answer

Map[4, 2] ← 'T'
OUTPUT Map[1, 5]

Explanation

A single element of a 2D array is identified by both indexes, in the order given in the declaration (here row, then column). Writing to an element uses ← with the element on the left; reading it just uses the element in an expression or OUTPUT.

Mark scheme
  1. 1 mark

    Map[4, 2] ← 'T'

    Accept "T". Do not accept Map[2, 4] or = for assignment.

  2. 1 mark

    OUTPUT Map[1, 5]

    Do not accept Map[5, 1].

Question 3

3 marks

The array Data[1:6] holds the values 14, 9, 22, 17, 22, 18 (Data[1] is 14).

Max ← Data[1]
Position ← 1
FOR Index ← 2 TO 6
    IF Data[Index] > Max
      THEN
        Max ← Data[Index]
        Position ← Index
    ENDIF
NEXT Index
OUTPUT Position

The trace table shows a dry run of this pseudocode. State the values of P, Q and R.

Trace table with four columns: Index, Max, Position and OUTPUT. Row 1: Max = 14, Position = 1; Index and OUTPUT blank. Row 2: Index = 2; Max, Position and OUTPUT blank. Row 3: Index = 3, Max is the letter P, Position is the letter Q; OUTPUT blank. Row 4: Index = 4; Max, Position and OUTPUT blank. Row 5: Index = 5; Max, Position and OUTPUT blank. Row 6: Index = 6; Max, Position and OUTPUT blank. Row 7: OUTPUT is the letter R; Index, Max and Position blank.

Model answer

P = 22
Q = 3
R = 3

Explanation

When Index is 3, Data[3] = 22 is greater than 14, so Max becomes 22 and Position becomes 3. When Index is 5, Data[5] is also 22, but 22 > 22 is false, so nothing changes. The algorithm outputs the position of the first occurrence of the largest value, 3.

Mark scheme
  1. 1 mark

    P = 22

  2. 1 mark

    Q = 3

  3. 1 mark

    R = 3

    Do not accept 5: 22 > 22 is false, so the second 22 does not replace the first.

Question 4

5 marks

The two-dimensional array Marks[1:30, 1:5] stores the marks of 30 students (first index) in 5 tests (second index).

Write pseudocode to calculate and output the average mark for each of the 5 tests.

Model answer

FOR Test ← 1 TO 5
    Total ← 0
    FOR Student ← 1 TO 30
        Total ← Total + Marks[Student, Test]
    NEXT Student
    OUTPUT "Test ", Test, " average: ", Total / 30
NEXT Test

Explanation

To total a column, the outer loop chooses the column (the test) and the inner loop runs down all the rows (the students). The order of the loops can be swapped from the usual row-by-row pattern, but the order of the indexes in Marks[Student, Test] must always match the declaration.

Mark scheme
  1. 1 mark

    MP1 outer loop for the tests 1 to 5

  2. 1 mark

    MP2 total set to 0 for each test, inside the outer loop and before the inner loop

  3. 1 mark

    MP3 inner loop for the students 1 to 30

  4. 1 mark

    MP4 adds Marks[Student, Test] to the total, with the indexes in the correct order

    Do not accept Marks[Test, Student].

  5. 1 mark

    MP5 calculates total / 30 and outputs it for each test, after the inner loop

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

AI Tutor Hints first, answers after you try

Hi! Stuck? Ask me for a hint and I’ll help you work it out. Once you’ve checked your answer I can explain exactly where the marks are.