Question bank › IB Computer Science › B2 Programming

Static and dynamic data structures: arrays and lists

Answer on paper first, then reveal the mark scheme one mark at a time.

1Core4 marks

Arrays in Java are static data structures, whereas a Python list or a Java ArrayList is dynamic.

(a) Define the term static data structure. [1]

(b) Compare dynamic and static data structures in terms of memory allocation, resizing and memory use. [3]

2Core3 marks

Consider the following Python code.

scores = [12, 7, 19, 4]
scores.append(10)
scores.insert(1, 15)
scores.remove(19)
last = scores.pop()
print(scores)
print(len(scores), last)

State the exact output of this code. [3]

3Standard6 marks

A multi-storey car park records which bays are occupied in a 2D Python list park. Each inner list is one level; park[level][bay] is 1 if the bay is occupied and 0 if it is free. The contents are shown in the diagram.

(a) State the value of park[1][3]. [1]

(b) State the value of park[2][0]. [1]

(c) Construct a Python function count_free(park) that uses nested loops to return the total number of free bays in the car park. Your function must work for any number of levels and any number of bays per level. [4]

park (1 = occupied, 0 = free)bay 0bay 1bay 2bay 3bay 4park[0] Level 010110park[1] Level 100101park[2] Level 211101
Stuck on Static and dynamic data structures: arrays and lists?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Standard5 marks

Consider the following Java code.

ArrayList<String> playlist = new ArrayList<>();
playlist.add("Rain");
playlist.add("Echo");
playlist.add("Drift");
playlist.add(1, "Bloom");
playlist.remove(2);
playlist.set(0, "Flare");
System.out.println(playlist.get(1));
System.out.println(playlist.size());

(a) State the output of this code. [2]

(b) A programmer instead declares String[] tracks = new String[3]; and later executes tracks[3] = "Bloom";

Explain why this statement fails, whereas the call playlist.add("Bloom") succeeds even though playlist already holds three elements. [3]

5Standard4 marks

A weather station program stores temperature readings in a static 2D array of integers with 200 rows and 150 columns. Each integer occupies 4 bytes.

(a) Calculate the memory, in bytes, reserved for this array. [2]

(b) On a typical day only 30% of the array's elements contain readings. Calculate the number of bytes reserved for elements that are not used. [1]

(c) Suggest why a dynamic data structure might be more suitable for storing these readings. [1]

6Hard5 marks

The following Python code is intended to remove every even number from the list nums.

nums = [3, 8, 8, 5, 6, 10]
i = 0
while i < len(nums):
    if nums[i] % 2 == 0:
        nums.pop(i)
    i = i + 1
print(nums)

(a) State the output of this code. [1]

(b) Explain why the code does not work as intended. [2]

(c) Describe how the code could be corrected. [2]

7Hard6 marks

Two Python lists, a and b, each contain integers in ascending order. For example, a = [4, 9, 15, 22] and b = [1, 9, 30].

Construct a Python function merge(a, b) that returns a new list containing every element of both lists in ascending order. For the example it would return [1, 4, 9, 9, 15, 22, 30].

You must not use any built-in sorting function, and either list may be empty. [6]

8Hard4 marks

A developer must choose a data structure for each of the following situations.

Situation A: storing the 64 squares of a chessboard in a board-game program.

Situation B: storing the items a customer adds to and removes from an online shopping basket.

For each situation, identify whether a static or a dynamic data structure is more appropriate and justify your choice. [4]

9Stretch6 marks

A shop chain stores the number of units sold in a Java 2D array sales. Each row is a branch and each column is a month, so sales[branch][month] holds one value. The data is shown in the diagram.

(a) Calculate the total units sold in month 1 across all branches. [1]

(b) Construct the Java method below so that it returns the index of the month with the highest total sales across all branches. Assume every row has the same number of columns and that there are no ties. [5]

public static int bestMonth(int[][] sales) {
    // to be completed
}
sales (units sold)Month 0Month 1Month 2sales[0] Branch 014921sales[1] Branch 18176sales[2] Branch 2121119sales[3] Branch 35207
10Stretch6 marks

A sensor program uses a fixed-size list readings of capacity 8 to simulate a static array. The variable count holds how many positions are in use, and the used positions are kept in ascending order. The diagram shows the current state.

def insert_sorted(readings, count, value):
    pos = count - 1
    while pos >= 0 and readings[pos] > value:
        readings[pos + 1] = readings[pos]
        pos = pos - 1
    readings[pos + 1] = value
    return count + 1

(a) The call count = insert_sorted(readings, count, 10) is made. State the contents of positions [0] to [5] of readings afterwards, and the number of elements that were shifted. [2]

(b) A further call count = insert_sorted(readings, count, 1) is then made. State how many elements are shifted. [1]

(c) The function does not check whether the array is full. Explain what would happen if it were called when count is 8, and state the check that should be added. [2]

(d) With reference to your answers to (a) and (b), outline why inserting into the front of an array is slower than appending to the end. [1]

readings (capacity 8), count = 53[0]8[1]12[2]17[3]25[4][5][6][7]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session