Return to main page
View source or report issues on GitHub

Collections in the IB Exam

What is a collection?

A collection:star: or a data structure is a general term for a set of MANY values that are connected, so that the entire group can be accessed with a single variable name. Collections will ALWAYS provide a way to add data and retrieve data and will USUALLY provide a way to iterate through the data, retrieving ever element one after the other.

Some types of collections that you might see in modern programming languages include arrays, linked lists, doubly linked lists, stacks, queues, dictionaries, and array lists - most of these will not be explored in the SL curriculum, with the two exceptions that follow.

Static vs Dynamic data structures

Some data structures are dynamic, which means they can grow in size arbitrarily; it is not necessary for the programmer to know in advance how large they will get. Other data structures are static, which means they take up a fixed amount of space and can neither grow nor shrink.

In our class, the most important dynamic data structure is the IB Collection described below, and the most important static data structure is the Array, discussed on the next page of notes.

The IB Collection

The IB defines their own special type of collection for use in all of their problems that involve pseudocode. We will call this special type of data structure the IB Collection. It is intentionally very limited, with only a few ways of interacting with it.

An IB Collection works like an class, and the class defines the following methods.

Method name Brief description Example:
HOT, a collection of temperatures
Comment
addItem() Add item HOT.addItem(42)
HOT.addItem("chile")
Adds an element to the end of the collection that contains the argument, whether it is a value, String, object, etc.
getNext() Get the next item TEMP = HOT.getNext() getNext() will return the first item in the collection when it is first called.

Note: getNext() does not remove the item from the collection.
resetNext() Go back to the start of the collection HOT.resetNext()
HOT.getNext()
Restarts the iteration through the collection. The two lines shown will retrieve the first item in the collection.
hasNext() Test: has next item if HOT.hasNext() then Returns TRUE if there are one or more elements in the collection that have not been accessed by the present iteration: used to see if we are at the end of the list.
isEmpty() Test: collection is empty if HOT.isEmpty() then Returns TRUE if the collection does not contain any elements.

Important understandings of an IB Collection

Here are some important things to understand about IB collections

Check Your Understanding

  1. What does the “resetNext()” command do for IB collections?

    Click to expand answer

    It makes it so the next time you call getNext() you will get the FIRST element.

  2. What will be printed (output) by the following code?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    COL.addItem("tree")
    COL.addItem("flower")
    COL.addItem("shrub")
    COL.addItem("grass")
    
    COL.resetNext()
    COL.getNext()
    output COL.getNext()
    COL.getNext()
    output COL.getNext()
    
    Click to expand answer
    1
    2
    
    flower
    grass
    

    shrub is skipped because there is no output before the third COL.getNext()

  3. What will be printed (output) by the following code?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    COL.addItem("tree")
    COL.addItem("flower")
    COL.addItem("shrub")
    COL.addItem("grass")
    
    COL.resetNext()
    loop while COL.hasNext()
       NEXT = COL.getNext()
       output NEXT
    end loop
    
    Click to expand answer
    1
    2
    3
    4
    
    tree
    flower
    shrub
    grass
    

    This is the standard IB Collections Loop!

  4. Write an algorithm that assumes you already have two collections, START and END. If START is empty, output the sentence “Nothing to copy”. If END is not empty, output the sentence “Destination not empty” Otherwise, copy all of the elements from the collection START into the collection END, then output “Collection copied”

    Click to expand answer
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    START.resetNext()
    END.resetNext()
    if START.isEmpty() then
       output "Nothing to copy"
    else if NOT END.isEmpty() then
       output "Destination not empty"
    else
       START.resetNext()
       loop while START.hasNext()
          NEXT = START.getNext()
          END.addItem(NEXT)
       end loop
       output "Collection copied"
    end if
    

    Alternatively, you could use END.addItem(START.getNext()) inside the loop

  5. You have two parallel collections named NAMES and AGES. “Parallel” means the first element of NAMES corresponds with the first element of AGES. Write code to iterate through both collections and if a persons age is at least 18, print out their name.

    Click to expand answer
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    NAMES.resetNext()
    AGES.resetNext()
    loop while NAMES.hasNext()
       N = NAMES.getNext()
       A = AGES.getNext()
       if A >= 18 then
          output N
       end if
    end loop
    

For more practice, visit the IB Pseudocode Practice Site and try the problems under the subheading Collections

Key terms