B3.1 Fundamentals of OOP for a single class questions for IB Computer Science

155 questions on Fundamentals of OOP for a single class, 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 B3 Object-oriented 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 211936155

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-01064Paper 2 · HL only · 15 marks

A school library lets students reserve books that are on loan. The reserved titles are held in a singly linked list. The list class is shown below.

Python

Python
class ReservationList:
    def __init__(self):
        self.head = None

Java

Java
public class ReservationList {
    private Node head;

    public ReservationList() {
        head = null;
    }
}

Each node holds a book title (a string) and a reference to the next node.

  1. (a)

    Construct the class Node, including a constructor that takes a title.

    3 marks

  2. (b)

    Construct the method add_to_front (Python) / addToFront (Java) that adds a new title at the start of the list.

    3 marks

  3. (c)

    Construct the method display that outputs every title in the list, from the first to the last.

    3 marks

  4. (d)

    Construct the method contains that takes a title and returns true if that title is in the list and false otherwise. For example, if the list holds "Dune", "Emma" and "Holes", contains("Emma") returns true and contains("Matilda") returns false.

    4 marks

  5. (e)

    Explain why the library does not need to decide in advance how many reservations the list can hold.

    2 marks

Question 2 · CS-01071Paper 2 · HL only · 13 marks

A photo-slideshow app lets a user step forwards and backwards through their photos and drop new photos in anywhere. The photos are stored in a doubly linked list.

Python

Python
class Node:
    def __init__(self, photo):
        self.photo = photo
        self.prev = None
        self.next = None

class Slideshow:
    def __init__(self):
        self.head = None
        self.tail = None

Java

Java
public class Node {
    String photo;
    Node prev;
    Node next;

    public Node(String photo) {
        this.photo = photo;
        this.prev = null;
        this.next = null;
    }
}

public class Slideshow {
    private Node head;
    private Node tail;
}
  1. (a)

    The slideshow holds the photos beach.jpg, cliff.jpg and dunes.jpg, in that order.

    Sketch the doubly linked list, showing head, tail and every reference.

    3 marks

  2. (b)

    Construct the method insert_after (Python) / insertAfter (Java) that takes a reference node to a node already in the list and a photo name, and inserts a new node holding that photo immediately after node. Your method must work when node is the last node.

    6 marks

  3. (c)

    Explain the effect on the app if the method in part (b) did not update the prev reference of the node that follows the new node.

    2 marks

  4. (d)

    State the time complexity of insert_after / insertAfter, and state the time complexity of inserting a photo after the photo with a given name. Assume the slideshow holds n photos.

    2 marks

Question 3 · CS-01102Paper 2 · 14 marks

A lightweight web browser for a smart TV remembers the pages a user has visited so that the Back button can return to them. It uses a stack built on a fixed-size array. top holds the index of the item on top of the stack.

Python

Python
class HistoryStack:
    def __init__(self, capacity):
        self.pages = [None] * capacity
        self.top = -1

    def is_empty(self):
        return self.top == -1

    def is_full(self):
        return self.top == len(self.pages) - 1

    def push(self, url):
        # code missing

    def pop(self):
        # code missing

Java

Java
class HistoryStack {
    private String[] pages;
    private int top;

    public HistoryStack(int capacity) {
        pages = new String[capacity];
        top = -1;
    }

    public boolean isEmpty() { return top == -1; }

    public boolean isFull() { return top == pages.length - 1; }

    public boolean push(String url) {
        // code missing
    }

    public String pop() {
        // code missing
    }
}
  1. (a)

    State the value of top when the stack holds 4 pages.

    1 mark

  2. (b)

    Construct the push method. If the stack is full it must return False / false and leave the stack unchanged; otherwise it adds url to the top and returns True / true.

    3 marks

  3. (c)

    Construct the pop method. It returns the URL on top of the stack and removes it, or returns None / null if the stack is empty.

    3 marks

  4. (d)

    A HistoryStack is created with a capacity of 3. The following operations are then carried out in order:

    push("home"), push("news"), push("sport"), push("weather"), pop(), push("games"), pop(), pop()

    Assuming push and pop work as described, state the value returned by each operation from push("weather") onwards.

    3 marks

  5. (e)

    Outline why a stack is appropriate for the Back button.

    2 marks

  6. (f)

    Outline one disadvantage of using a fixed-size array for this stack.

    2 marks

Question 4 · CS-01109Paper 2 · 13 marks

A taxi company's app holds riders waiting for a car in a queue. To avoid shifting elements, the queue is stored in a fixed-size array used as a circular queue: head is the index of the rider at the front, tail is the index where the next rider will be stored, and count is the number of riders waiting.

Python

Python
class RideQueue:
    def __init__(self, capacity):
        self.data = [None] * capacity
        self.head = 0
        self.tail = 0
        self.count = 0

    def enqueue(self, rider):
        if self.count == len(self.data):
            return False
        self.data[self.tail] = rider
        self.tail = (self.tail + 1) % len(self.data)
        self.count = self.count + 1
        return True

    def dequeue(self):
        if self.count == 0:
            return None
        rider = self.data[self.head]
        self.head = (self.head + 1) % len(self.data)
        self.count = self.count - 1
        return rider

Java

Java
class RideQueue {
    private String[] data;
    private int head = 0;
    private int tail = 0;
    private int count = 0;

    public RideQueue(int capacity) { data = new String[capacity]; }

    public boolean enqueue(String rider) {
        if (count == data.length) {
            return false;
        }
        data[tail] = rider;
        tail = (tail + 1) % data.length;
        count = count + 1;
        return true;
    }

    public String dequeue() {
        if (count == 0) {
            return null;
        }
        String rider = data[head];
        head = (head + 1) % data.length;
        count = count - 1;
        return rider;
    }
}
  1. (a)

    Outline the purpose of the expression (tail + 1) % len(data) / (tail + 1) % data.length.

    2 marks

  2. (b)

    A RideQueue is created with a capacity of 4. Copy and complete the trace table for the following operations.

    Operation

    Value returned

    head

    tail

    count

    (start)

    0

    0

    0

    enqueue("A")

    enqueue("B")

    enqueue("C")

    dequeue()

    dequeue()

    enqueue("D")

    enqueue("E")

    enqueue("F")

    enqueue("G")

    5 marks

  3. (c)

    State the contents of data, from index 0 to index 3, after the operations in part (b).

    1 mark

  4. (d)

    Construct a method front that returns the rider at the front of the queue without removing them, or None / null if the queue is empty.

    2 marks

  5. (e)

    Explain why dequeue in this class takes the same time however many riders are waiting, unlike a queue that removes element 0 of a dynamic list.

    3 marks

Question 5 · CS-01117Paper 2 · 14 marks

A school library keeps a record of every book it owns. Each book is represented by an object of the class Book.

Python

Python
class Book:
    def __init__(self, isbn, title, author):
        self.__isbn = isbn
        self.__title = title
        self.__author = author
        self.__on_loan = False

    def get_title(self):
        return self.__title

    def get_author(self):
        return self.__author

    def is_on_loan(self):
        return self.__on_loan

    def return_book(self):
        self.__on_loan = False

Java

Java
public class Book {
    private String isbn;
    private String title;
    private String author;
    private boolean onLoan;

    public Book(String isbn, String title, String author) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
        this.onLoan = false;
    }

    public String getTitle() { return title; }
    public String getAuthor() { return author; }
    public boolean isOnLoan() { return onLoan; }

    public void returnBook() {
        onLoan = false;
    }
}
  1. (a)

    State the name of one accessor method in the class Book.

    1 mark

  2. (b)

    Construct a statement that creates a Book object for Frankenstein by Mary Shelley, ISBN 978-0-14-143947-1, and stores it in a variable called b.

    2 marks

  3. (c)

    Construct the method check_out() (Python) / checkOut() (Java). If the book is not already on loan, the method marks it as on loan and returns true. If the book is already on loan, it returns false and changes nothing.

    3 marks

  4. (d)

    The library stores its books in a list (Python) / an array of 500 Book references (Java) called books. Unused positions hold None (Python) / null (Java).

    Construct the function/method count_available(books, author) / countAvailable(books, author) that returns the number of books by the given author that are not on loan.

    5 marks

  5. (e)

    Explain why the attribute that records whether a book is on loan is private and can only be changed through check_out / checkOut and return_book / returnBook.

    3 marks

Question 6 · CS-01118Paper 2 · 5 marks

A market garden places soil-moisture sensors in different growing zones. Each sensor is modelled by the class SoilSensor.

Python

Python
class SoilSensor:
    def __init__(self, sensor_id, zone):
        self.__sensor_id = sensor_id
        self.__zone = zone
        self.__moisture = 0

    def record(self, value):
        self.__moisture = value

    def get_moisture(self):
        return self.__moisture

    def get_zone(self):
        return self.__zone

Java

Java
public class SoilSensor {
    private String sensorId;
    private String zone;
    private int moisture;

    public SoilSensor(String sensorId, String zone) {
        this.sensorId = sensorId;
        this.zone = zone;
        this.moisture = 0;
    }

    public void record(int value) { moisture = value; }
    public int getMoisture() { return moisture; }
    public String getZone() { return zone; }
}

The following code is run.

Python

Python
s1 = SoilSensor("S1", "North")
s2 = SoilSensor("S2", "East")
s1.record(42)
s2.record(s1.get_moisture() - 5)
s1.record(30)
print(s1.get_moisture())
print(s2.get_moisture())
print(s2.get_zone())

Java

Java
SoilSensor s1 = new SoilSensor("S1", "North");
SoilSensor s2 = new SoilSensor("S2", "East");
s1.record(42);
s2.record(s1.getMoisture() - 5);
s1.record(30);
System.out.println(s1.getMoisture());
System.out.println(s2.getMoisture());
System.out.println(s2.getZone());
  1. (a)

    State the output of the code.

    3 marks

  2. (b)

    Outline the role of the constructor in the class SoilSensor.

    2 marks

Practice all 155 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 B3.1 Fundamentals of OOP for a single class

More questions in Object-oriented programming

  1. B3.1Fundamentals of OOP for a single classThis page
  2. B3.2Fundamentals of OOP for multiple classesHL only

Check your answers in Augment

A free account opens all 155 questions on Fundamentals of OOP for a single class with their markschemes, and keeps a record of what you have marked.