Skip to content
ExamSpine

Practice Paper 2, SL, set 1

SL · 75 minutes · 44 marks

Instructions

  • Answer every question.

  • Write code in the language your class uses, Python or Java. On screen, the switch above the paper shows the code in your language.

  • The marks for each part are shown in square brackets, such as [3].

  • Coding parts can be opened on their own: write your answer as you would on paper, then run it against tests.

  • The time is Spine's choice, from public descriptions of the new course, which Spine has not yet checked against the IB's subject guide. The marks are Spine's own.

Answer all questions

Question 1[14 marks]

A cinema lets people book seats online.

1.1

The Lumen is a small cinema with one screen of 8 rows, each of 12 seats. It wants an online system that lets customers book seats for its films.

(a) Outline how decomposition could be applied to the design of this booking system. Name two sub-problems in your answer. [3]

(b) The part of the system that lets customers pick seats stores each row as a list of 0s and 1s, where 0 is a free seat and 1 is a booked seat. Identify one detail of the real cinema or its customers that this leaves out, and outline why leaving it out helps. [2]

[5]

Answer it on its own page and mark it yourself

1.2

Complete the trace table for this Java code. The Lumen cinema stores one row of seats as an array: 0 is a free seat and 1 is a booked seat. This code goes along the row from seat 0. A cell stays empty for any variable a line leaves unchanged. Then answer the question below the table. [4]

int[] row = {1, 0, 0, 1, 0};
int free = 0;
int first = -1;
for (int i = 0; i < row.length; i++) {
    if (row[i] == 0) {
        free = free + 1;
        if (first == -1) {
            first = i;
        }
    }
}

Why does first start at -1 and not at 0?

Fill in the table on its own page and check it

1.3

The Lumen cinema's booking system stores one row of seats as a list (an array in Java) of whole numbers: 0 is a free seat and 1 is a booked seat. Seat positions start at 0. A group wants to sit together. Write a function that returns the position of the first seat of the first block of n free seats next to each other.

The design team wrote this plan for it:

RUN = 0
loop through the row from position 0
    if the seat is free, add 1 to RUN
    otherwise set RUN back to 0
    if RUN equals n, return the position
      of the first seat of this block
return -1
  1. A block is n free seats in positions next to each other. If there is more than one such block, return the position of the first seat of the one that comes first (the lowest position).
  2. If the row has no block of n free seats, return -1.
  3. A block may reach the last seat of the row.
  4. n is a whole number from 1 to 30. The row holds only 0s and 1s, has at most 30 seats, and can be empty.

Only the number your function returns is checked.

def first_block(row: list[int], n: int) -> int
public static int firstBlock(int[] row, int n)

Write it as in the exam, then run the testsPractise it with the Run button

[5]

Question 2[15 marks]

A school weather station records the temperature every hour.

2.1

Complete the trace table for this Python code. A school weather station stores one day's hourly temperatures, in whole degrees Celsius, in temps. This code counts the hours below freezing and finds the lowest reading. Then answer the question below the table. [4]

temps = [3, 2, -1, 4, -2, -1]
below = 0
lowest = temps[0]
for t in temps:
    if t < 0:
        below = below + 1
    if t < lowest:
        lowest = t

Why does the code start lowest at temps[0] rather than at 0?

Fill in the table on its own page and check it

2.2

A school weather station records the temperature once an hour, in whole degrees Celsius, and stores one day's readings in order in a list (an array in Java) called temps.

Write a function that returns the biggest rise in temperature from one hour to the next hour.

  1. A rise is a reading minus the reading one hour before it, such as temps[3] - temps[2].
  2. Only rises between hours next to each other count, including the rise into the last hour.
  3. If the temperature never goes up from one hour to the next, return 0. A fall is never a rise, however big it is.
  4. temps always has at least two readings, each between -60 and 60.
  5. Write the comparison yourself. The shortcuts listed with this question are not allowed.

Only the number your function returns is checked.

def biggest_rise(temps: list[int]) -> int
public static int biggestRise(int[] temps)

Paper 2 questions often ask you to find the largest value yourself, without a built-in, so practise the loop and the comparison.

Write it as in the exam, then run the testsPractise it with the Run button

[5]

2.3

The school weather station keeps a week of hourly readings in a two-dimensional list (a 2D array in Java) called grid. Each row is one day and each value in a row is one hourly temperature in whole degrees Celsius, so grid[2][5] is the reading for day 2, hour 5.

Write a function that returns the number of the day (the row index) whose readings add up to the highest total. Days are numbered from 0.

  1. A day's total is the sum of every reading in its row.
  2. If two or more days share the highest total, return the first of them.
  3. grid has at least one row, every row has at least one reading, and rows can have different lengths.
  4. Readings are between -60 and 60, and a row has at most 24 readings, so totals can be below 0.
  5. Write the adding and the comparing yourself. The shortcuts listed with this question are not allowed.

Only the number your function returns is checked.

def warmest_day(grid: list[list[int]]) -> int
public static int warmestDay(int[][] grid)

Paper 2 questions on 2D arrays expect you to write the nested loops that add each row yourself, so practise them without the built-ins.

Write it as in the exam, then run the testsPractise it with the Run button

[6]

Question 3[15 marks]

A school library program keeps track of its books with a Book class.

3.1

A school library program has a Book class. Here it is in Python and in Java.

class Book:
    def __init__(self, title, copies):
        self.title = title
        self.__available = copies

    def get_available(self):
        return self.__available

    def borrow(self):
        if self.__available > 0:
            self.__available -= 1
            return True
        return False
public class Book {
    private String title;
    private int available;

    public Book(String title, int copies) {
        this.title = title;
        this.available = copies;
    }

    public int getAvailable() {
        return available;
    }

    public boolean borrow() {
        if (available > 0) {
            available--;
            return true;
        }
        return false;
    }
}

(a) Identify the method in Book that is an accessor. [1]

(b) The number of available copies is private (in Python, its name starts with two underscores). Explain why it is private, and why code outside the class changes it only by calling borrow. [3]

(c) Another part of the program runs Book("Dune", 2) (in Java, new Book("Dune", 2)) and Book("Emma", 1). Distinguish between the class Book and the objects these lines create. [2]

[6]

Answer it on its own page and mark it yourself

3.2

The school library's Book class now also counts how many books have been created. Here it is in Python and in Java, with code that uses it. In Java the last part runs inside a main method.

class Book:
    count = 0

    def __init__(self, title, copies):
        self.title = title
        self.__available = copies
        Book.count += 1

    def get_available(self):
        return self.__available

    def borrow(self):
        if self.__available > 0:
            self.__available -= 1
            return True
        return False

b1 = Book("Dune", 1)
b2 = Book("Emma", 2)
print(b1.borrow())
print(b1.borrow())
print(b2.borrow())
print(b2.get_available())
print(Book.count)
public class Book {
    private static int count = 0;
    private String title;
    private int available;

    public Book(String title, int copies) {
        this.title = title;
        this.available = copies;
        count++;
    }

    public int getAvailable() {
        return available;
    }

    public boolean borrow() {
        if (available > 0) {
            available--;
            return true;
        }
        return false;
    }

    public static int getCount() {
        return count;
    }
}

// in main
Book b1 = new Book("Dune", 1);
Book b2 = new Book("Emma", 2);
System.out.println(b1.borrow());
System.out.println(b1.borrow());
System.out.println(b2.borrow());
System.out.println(b2.getAvailable());
System.out.println(Book.getCount());

(a) State the five lines of output, in order. [3]

(b) Explain why the last line of output is 2 and not 1. [2]

[5]

Answer it on its own page and mark it yourself

3.3

A school library keeps a Book object for each title. Its mutator methods refuse a change that would make the number of copies on the shelf impossible. Write a function that works out the same thing from simple data: it is given how many copies of a book the library owns, all on the shelf at the start, and a list (an array in Java) of actions, carried out in order. It returns how many copies are on the shelf after the last action.

  1. "borrow" takes one copy off the shelf, but only if at least one copy is there. Otherwise it is refused and changes nothing.
  2. "return" puts one copy back, but only if fewer than total copies are on the shelf. Otherwise it is refused and changes nothing.
  3. Any other text, such as "renew", changes nothing. Capital letters matter: "Borrow" is not "borrow".
  4. total is a whole number from 0 to 50. There are at most 50 actions, and the list can be empty.

Only the number your function returns is checked.

def copies_left(total: int, actions: list[str]) -> int
public static int copiesLeft(int total, String[] actions)

Write it as in the exam, then run the testsPractise it with the Run button

[4]

Finished? Open the markscheme