Arrays & lists

Computer Science · Data structures

Question 1 of 10

0

    The length of the list [7, 7, 7] is:

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Arrays & lists explained

    An array is a collection of items stored under one name, where each item is picked out by its index. A list is a similar idea, but in many languages it can grow or shrink as items are added or removed. Storing the ten scores of a class in one array is much tidier than using ten separate variables.

    Indexes usually start at 0, so the first item is at index 0 and an array of 5 items has indexes 0 to 4. Looping over an array is the usual way to process every item: for each index, read the item and do something with it, such as adding it to a running total.

    Conventions differ. Most programming languages count from 0, but some textbooks and some exam pseudocode start at 1, so check which convention your course uses. In Java, arrays have a fixed size, while ArrayList can grow. Python's list type can also grow.

    A common mistake is an index out of range: an array with 5 items has no item at index 5, so reading it causes an error in most languages.

    Worked example

    A teacher stores five test scores in an array with indexes 0 to 4: scores = [12, 7, 9, 15, 4]. Find the total and the mean.

    1. Add the items in a loop: 12 + 7 + 9 + 15 + 4 = 47.
    2. Divide the total by the number of items: 47÷5=9.447 \div 5 = 9.4.

    The mean score is 9.4.

    What you will practise

    100 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.

    Example questions

    • An array stores:
    • Array indices start at:
    • Accessing an element by index takes:
    • Inserting at the front of an array is:
    • Dynamic arrays grow by:
    • Removing the last element from a dynamic array is typically:
    • A linked list stores each element in a node that contains:
    • In most languages, the elements of an array are stored:

    …and 92 more.