- Subjects
- Computer Science
- Question bank
- B3.1 Fundamentals of OOP for a single class
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.
| Paper | SL and HL | HL only | All |
|---|---|---|---|
| Paper 2 | 119 | 36 | 155 |
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.
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
class ReservationList:
def __init__(self):
self.head = NoneJava
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.
- (a)
Construct the class
Node, including a constructor that takes a title.3 marks
- (b)
Construct the method
add_to_front(Python) /addToFront(Java) that adds a new title at the start of the list.3 marks
- (c)
Construct the method
displaythat outputs every title in the list, from the first to the last.3 marks
- (d)
Construct the method
containsthat 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 andcontains("Matilda")returns false.4 marks
- (e)
Explain why the library does not need to decide in advance how many reservations the list can hold.
2 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
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 = NoneJava
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;
}- (a)
The slideshow holds the photos
beach.jpg,cliff.jpganddunes.jpg, in that order.Sketch the doubly linked list, showing
head,tailand every reference.3 marks
- (b)
Construct the method
insert_after(Python) /insertAfter(Java) that takes a referencenodeto a node already in the list and a photo name, and inserts a new node holding that photo immediately afternode. Your method must work whennodeis the last node.6 marks
- (c)
Explain the effect on the app if the method in part (b) did not update the
prevreference of the node that follows the new node.2 marks
- (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
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
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 missingJava
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
}
}- (a)
State the value of
topwhen the stack holds 4 pages.1 mark
- (b)
Construct the
pushmethod. If the stack is full it must returnFalse/falseand leave the stack unchanged; otherwise it addsurlto the top and returnsTrue/true.3 marks
- (c)
Construct the
popmethod. It returns the URL on top of the stack and removes it, or returnsNone/nullif the stack is empty.3 marks
- (d)
A
HistoryStackis 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
pushandpopwork as described, state the value returned by each operation frompush("weather")onwards.3 marks
- (e)
Outline why a stack is appropriate for the Back button.
2 marks
- (f)
Outline one disadvantage of using a fixed-size array for this stack.
2 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
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 riderJava
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;
}
}- (a)
Outline the purpose of the expression
(tail + 1) % len(data)/(tail + 1) % data.length.2 marks
- (b)
A
RideQueueis created with a capacity of 4. Copy and complete the trace table for the following operations.Operation
Value returned
headtailcount(start)
0
0
0
enqueue("A")enqueue("B")enqueue("C")dequeue()dequeue()enqueue("D")enqueue("E")enqueue("F")enqueue("G")5 marks
- (c)
State the contents of
data, from index 0 to index 3, after the operations in part (b).1 mark
- (d)
Construct a method
frontthat returns the rider at the front of the queue without removing them, orNone/nullif the queue is empty.2 marks
- (e)
Explain why
dequeuein this class takes the same time however many riders are waiting, unlike a queue that removes element 0 of a dynamic list.3 marks
A school library keeps a record of every book it owns. Each book is represented by an object of the class Book.
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 = FalseJava
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;
}
}- (a)
State the name of one accessor method in the class
Book.1 mark
- (b)
Construct a statement that creates a
Bookobject for Frankenstein by Mary Shelley, ISBN978-0-14-143947-1, and stores it in a variable calledb.2 marks
- (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
- (d)
The library stores its books in a list (Python) / an array of 500
Bookreferences (Java) calledbooks. Unused positions holdNone(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
- (e)
Explain why the attribute that records whether a book is on loan is private and can only be changed through
check_out/checkOutandreturn_book/returnBook.3 marks
A market garden places soil-moisture sensors in different growing zones. Each sensor is modelled by the class SoilSensor.
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.__zoneJava
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
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
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());- (a)
State the output of the code.
3 marks
- (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.
More questions in Object-oriented programming
- B3.1Fundamentals of OOP for a single classThis page
- B3.2Fundamentals of OOP for multiple classesHL only
More on Fundamentals of OOP for a single class
- B3.1 Fundamentals of OOP for a single class notesWhat Fundamentals of OOP for a single class covers, explained, with its key terms.
- IB Computer Science question bankEvery Computer Science topic counted, and one question from each with its markscheme.
- Computer Science exam formatEach Computer Science paper, how long it is and its share of the grade.
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.