B2.3 Programming constructs questions for IB Computer Science

183 questions on Programming constructs, written by Augment in the style of IB exam papers. Six of them are below in full, to answer on paper.

  • IB Computer Science
  • Part of B2 Programming
  • SL and HL

Questions by paper and level

SL and HL questions are for students at either level. HL only questions cover what only HL students study.

PaperSL and HLHL onlyAll
Paper 216815183

Counted on

Questions to try

Written by Augment in the style of IB exams, not past papers. Answer them on paper, then check each one against its markscheme with a free account.

Question 1 · CS-00954Paper 2 · 14 marks

A music streaming service checks whether a playlist contains the same song twice. Each song is identified by an integer ID, and a playlist is a list (Python) or array (Java) of IDs called ids.

Python

Python
def has_duplicate(ids):
    n = len(ids)
    for i in range(n - 1):
        for j in range(i + 1, n):
            if ids[i] == ids[j]:
                return True
    return False

Java

Java
public static boolean hasDuplicate(int[] ids) {
    int n = ids.length;
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (ids[i] == ids[j]) {
                return true;
            }
        }
    }
    return false;
}
  1. (a)

    State the value returned when ids holds [12, 7, 33, 7].

    1 mark

  2. (b)

    Calculate the number of times the comparison ids[i] == ids[j] is made, in the worst case, for a playlist of 6 songs.

    2 marks

  3. (c)

    Deduce the worst-case time complexity of has_duplicate / hasDuplicate, explaining your answer.

    3 marks

  4. (d)

    Describe the best case for this function/method and state its time complexity.

    2 marks

  5. (e)

    The service now keeps every playlist sorted by ID.

    Construct, in Python or Java, a function/method has_duplicate_sorted / hasDuplicateSorted that returns whether a sorted playlist contains a repeated ID, using a single loop. For example, [7, 12, 12, 33] returns true and [7, 12, 33] returns false.

    4 marks

  6. (f)

    State the time complexity of your solution to part (e) and outline why it scales better than has_duplicate / hasDuplicate for long playlists.

    2 marks

Question 2 · CS-00960Paper 2 · 5 marks

A data-compression library contains the function/method tally below.

Python

Python
def tally(n):
    count = 0
    for i in range(n):
        j = 1
        while j < n:
            j = j * 2
            count = count + 1
    return count

Java

Java
public static int tally(int n) {
    int count = 0;
    for (int i = 0; i < n; i++) {
        int j = 1;
        while (j < n) {
            j = j * 2;
            count = count + 1;
        }
    }
    return count;
}
  1. (a)

    State the value returned by tally(8) and by tally(10).

    2 marks

  2. (b)

    Deduce the time complexity of tally, explaining your answer.

    3 marks

Question 3 · CS-00964Paper 2 · 13 marks

A weather station takes one wind-speed reading every minute. The function/method analyse prints the mean reading and counts the pairs of readings that differ by more than limit.

Python

Python
def analyse(readings, limit):
    total = 0
    for r in readings:
        total = total + r
    print(total / len(readings))
    jumps = 0
    for i in range(len(readings)):
        for j in range(i + 1, len(readings)):
            if abs(readings[i] - readings[j]) > limit:
                jumps = jumps + 1
    print(jumps)

Java

Java
public static void analyse(int[] readings, int limit) {
    int total = 0;
    for (int r : readings) {
        total = total + r;
    }
    System.out.println((double) total / readings.length);
    int jumps = 0;
    for (int i = 0; i < readings.length; i++) {
        for (int j = i + 1; j < readings.length; j++) {
            if (Math.abs(readings[i] - readings[j]) > limit) {
                jumps = jumps + 1;
            }
        }
    }
    System.out.println(jumps);
}
  1. (a)

    State the output when analyse is called with the readings [12, 15, 11, 19, 14] and a limit of 3.

    2 marks

  2. (b)

    State the time complexity of the first loop and of the nested loops.

    2 marks

  3. (c)

    Explain why the overall time complexity of analyse is O(n2) rather than O(n+n2).

    2 marks

  4. (d)

    Analysing one day of data (1440 readings) takes 0.5 seconds. Calculate the approximate time needed to analyse one week of data (10 080 readings).

    2 marks

  5. (e)

    State the space complexity of analyse.

    1 mark

  6. (f)

    The meteorologist decides that only consecutive readings matter.

    Construct, in Python or Java, a function/method count_steps / countSteps that takes readings and limit and returns the number of consecutive pairs of readings that differ by more than limit. For example, for [12, 15, 11, 19, 14] and a limit of 3 it returns 3.

    4 marks

Question 4 · CS-00966Paper 2 · 8 marks

A train operator stores the numbers of the tickets sold for one journey in a list (Python) or array (Java) tickets, in the order they were sold. An inspector's handheld device needs to find a ticket number.

You must not use built-in searching functions or methods such as index() or in (Python) or indexOf() or contains() (Java).

  1. (a)

    Construct, in Python or Java, a function/method find_ticket / findTicket that takes tickets and a ticket number, and returns the index of the first occurrence of number, or −1 if it is not there. For example, if tickets holds [4410, 3172, 5098, 3172], searching for 3172 returns 1 and searching for 9999 returns −1.

    5 marks

  2. (b)

    State the worst-case time complexity of your function/method.

    1 mark

  3. (c)

    Outline why returning as soon as the ticket is found improves the average performance of the search.

    2 marks

Question 5 · CS-00967Paper 2 · 15 marks

An airline stores the booking numbers for a flight, as integers in ascending order, in a list (Python) or array (Java) bookings. Check-in staff need to find a booking quickly.

You must not use built-in searching functions or methods such as index(), in or the bisect module (Python), or Arrays.binarySearch() or indexOf() (Java).

  1. (a)

    State the condition that bookings must meet for a binary search to work.

    1 mark

  2. (b)

    Construct, in Python or Java, a function/method find_booking / findBooking that uses a binary search to return the index of target in bookings, or −1 if it is not present.

    6 marks

  3. (c)

    The flight has 500 bookings. Calculate the maximum number of times bookings[mid] is compared with target in a search that finds the booking.

    2 marks

  4. (d)

    Passenger surnames are held in a second list/array in the same order as bookings. Explain why a binary search cannot be used to find a passenger by surname.

    2 marks

  5. (e)

    The airline expects flights with up to 50 000 bookings in future. Compare linear search and binary search for finding a booking number in this system.

    4 marks

Question 6 · CS-01033Paper 2 · 14 marks

A city bike-sharing scheme receives a status message from each docking station, for example "ST042:12/20". This means station 42 has 12 bikes in its 20 docks. The station number always has three digits, but the other two numbers can have one or two digits. The first character is at index 0.

  1. (a)

    State the value of status.find(":") (Python) / status.indexOf(":") (Java) when status holds "ST042:12/20".

    1 mark

  2. (b)

    Construct a line of code that extracts the station number from status and stores it as an integer in station.

    3 marks

  3. (c)

    Construct, in Python or Java, a function/method free_docks(status) / freeDocks(status) that returns the number of empty docks. For example, "ST042:12/20" returns 8 and "ST107:0/16" returns 16.

    5 marks

  4. (d)

    Some messages are corrupted, for example "ST042:1x/20". Describe how exception handling could stop one corrupted message from crashing the program that processes all the stations.

    3 marks

  5. (e)

    Suggest two further test messages for free_docks / freeDocks, giving the expected result of each.

    2 marks

Practice all 183 in Augment

Free with an account. Every question has its markscheme, and Augment keeps the ones you got wrong so you can come back to them.

The notes on B2.3 Programming constructs

More questions in Programming

  1. B2.1Programming fundamentals
  2. B2.2Data structures
  3. B2.3Programming constructsThis page
  4. B2.4Programming algorithms
  5. B2.5File processing

Check your answers in Augment

A free account opens all 183 questions on Programming constructs with their markschemes, and keeps a record of what you have marked.