OCR A-Level Computer Science H446
Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation
Software Development
Types of Programming Language
Exchanging Data
Networks
Web Technologies
Data Types and Number Representation
Data Structures
Boolean Algebra
Legal, Moral, Cultural and Ethical Issues
Elements of Computational Thinking
Problem Solving and Programming
Algorithms
Data Structures
Lists and tuples, and creating, traversing, adding and removing items
- 2 things to know
- Component 01
Overview
Recall questions on lists and tuples and how they differ from arrays, immutability, when to choose a tuple, and how to create, traverse, add to and remove from arrays, records and lists.
What you need to know
Answer each one before you reveal it.
How do you add an item to an array, and to a list?
Questions from this subtopic
Each one with the model answer underneath.
- 1
What is a list?
Model answer
An ordered, dynamic collection of items where each item has a position. Unlike an array, a list can grow and shrink at run time, items can be inserted and removed at any position, and in many languages the items do not all have to be the same data type. Underneath it may be implemented with a dynamic array or a linked list.
- 2
Give two ways a list differs from an array.
Model answer
A list is dynamic - it can grow and shrink while the program runs - whereas an array's size is fixed on creation. A list also usually provides built-in operations for inserting and removing at any position and in many languages may hold mixed data types, whereas an array typically holds a single type and requires you to shuffle elements yourself.
- 3
What is a tuple?
Model answer
An ordered set of values of fixed length, which is immutable - once it has been created its contents cannot be changed. Tuples are usually written in parentheses, for example (53.4, -2.9) for a coordinate pair, and their values may be of different data types.
- 4
Tuples are immutable and lists are not. Explain exactly what that means in practice.
Model answer
Immutable means that once a tuple exists you cannot alter it: you cannot change the value at a position, you cannot add a new item to it, and you cannot delete an item from it. A list is mutable, so all three of those operations are allowed. To 'change' a tuple you must build a new tuple from the old one's values and assign that instead - the original is never modified. This mutable/immutable distinction is the examinable difference between the two.
- 5
Why would you deliberately choose a tuple rather than a list?
Model answer
When the data is a fixed group of values that should never change, immutability protects it from accidental modification and makes the programmer's intent clear. Tuples are therefore good for coordinates, RGB colour triples, a date as (day, month, year), returning several values from one subroutine, and as fixed keys. They also tend to be slightly more efficient because their size is known and fixed.
- 6
How do you create each of an array, a record, a list and a tuple?
Model answer
An array is created by declaring its name, the data type of its elements and its size (and for multi-dimensional arrays the size of each dimension), which reserves the memory. A record is created by first defining a record type listing the field names and their types, then declaring a variable of that type. A list is created empty or from an initial set of values and grows as needed. A tuple is created by writing its values in order, usually in parentheses, and is complete at that moment because it cannot afterwards be altered.
Other ways to work this
The same content, four other ways.
What is inside Levo
Six ways to work the same material.
Every question in Data Structures, on a schedule.
Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.
Or £46 a year, about £3.83 a month. Cancel anytime.