Practice IB Computer Science (CS) Topic D.3 Program Development with authentic exam-style questions for both SL and HL students. This question bank focuses on the exact syllabus content for D.3 Program Development and mirrors Paper 1, 2, 3 style where relevant.
Get instant solutions, detailed explanations, and build confidence with questions aligned to IB examiner expectations.
The management of the company will launch a new scheme to give every 50th car driver and every 60th motorcyclist a free coffee voucher. The code for printing this voucher has already been created and is activated by calling the static method Vouchers.printCoffeeVoucher().
A getKind() method has already been added to the Vehicle class, which returns a char value indicating whether it is a car (c) or a motorbike (m).
One test performed on the finished code was defined as follows:
| Test data | Vouchers printed |
|---|---|
| 29 cars | 0 |
| 130 motorbikes | 2 |
The removeVehicle method of the ParkingArea class searches in the array for a Vehicle object with a specified registration plate, then removes it by setting that array index to null.
The method returns a reference to the Vehicle object that has been removed from the array, or null if no matching registration plate was found.
Construct the removeVehicle method.
Describe, without writing code, any changes required to the addVehicle method and the ParkingArea class to make the new voucher scheme work.
Identify three other tests you might perform on the completed code to prove that it functions correctly.
An airport uses an object-oriented program to keep track of arrivals and departures of planes. There are many objects in this system and some are listed below.
The code below outlines the Arrival class used in this program.
public class Flight
{ private String id;
public String getId() {return this.id;}
// ... more variables, accessor and mutator methods
}
public class Arrival
{ private Flight myFlight;
private String sta; // Scheduled Time of Arrival ('hh:mm')
private int runway;
private String gate;
private int delay;
private boolean landed;
public Arrival(Flight myFlight, String sta)
{ **this.**myFlight = myFlight;
**this.**sta = sta;
**this.**runway = 0;
**this.**gate = null;
**this.**delay = 0;
**this.**landed = false;
}
public void addDelay(int newDelay)
{ **this.**delay = newDelay;
}
public String getETA()
{ // calculates the Estimated Time of Arrival (ETA) of the flight
// by adding the delay to the sta and returning the result as a
// String ('hh:mm')
}
public int compareWith(String flightID)
{ if (myFlight.getId().equals(flightID)) { return 0; }
else { return 1; }
}
public int compareWith(Arrival anotherArrival)
{ // missing code
}
// ... plus accessor and mutator methods
}
The code below outlines part of the FlightManagement class used in this program.
For the purposes of this exam only arriving flights are being considered.
public class FlightManagement
{
private Arrival$$ inbound; // array of inbound airplanes
private int last = -1; // index of last used entry
public FlightManagement()
{ inbound = new Arrival;
}
public void add(Arrival newArrival)
{ // missing code that adds the newArrival to the array inbound
// sorted by ETA, and updates last
}
private int search (String flightID)
{ // missing code that searches the array inbound and
// returns the index of the Arrival object with flightID
}
public Arrival remove(String flightID)
{ Arrival result;
int index = search(flightID);
result = inbound;
while (index < last)
{ inbound = inbound;
index++;
}
last--;
return result;
}
// ... many more methods
}
The method search in the FlightManagement class searches the array inbound and returns the index of the Arrival object with the given flightID.
Write the code for the method search in the FlightManagement class.
Outline one advantage of using modularity in program development.
Easier to maintain/debug code as errors can be isolated to specific modules and fixed independently without affecting the rest of the program.
Outline one advantage of using a binary search.
Describe the relationship between Flight and Arrival shown in the code.
Outline the general nature of an object in object-oriented programming.
Outline one disadvantage of using a binary search.
Describe two disadvantages of using Object Oriented Programming (OOP).
OOP can lead to increased program complexity due to the need to create and manage multiple classes, inheritance hierarchies, and relationships between objects. This can make the code harder to understand and maintain, especially for larger systems.
OOP programs can have lower performance compared to procedural programming due to the overhead of creating objects, method calls, and memory allocation. The encapsulation and abstraction features of OOP can add extra layers of processing that impact execution speed.
Construct a UML diagram to represent the Arrival object.
A municipal library system manages a large collection of physical resources across several branches. To streamline operations, a digital cataloging system is being developed to track individual books and their availability.
Each book in the system has a title and a recorded page count. The system must also track whether the book is currently checked out to a member or available on the shelf. The books are managed within specific branches, each represented by a collection with a defined maximum capacity.
A developer has drafted the following Book and Library classes as part of the initial model:
public class Book { private String title; private int pages; private boolean borrowed;
public Book(String t, int p) { title = t; pages = p; borrowed = false; }
public void setBorrowed(boolean status) { borrowed = status; }
public String getTitle() { return title; }
public int getPages() { return pages; }}public class Library { private Book[] collection; private String branchName;
public Library(String bName, int max) { branchName = bName; collection = new Book[max]; }
public void addBook(Book b, int index) { if (index >= 0 && index < collection.length) { collection[index] = b; } }
public String getBranchName() { return branchName; }}Outline the difference between an object’s state and its behaviors, using examples from the Book class.
Explain the advantage of using the private access modifier for the pages variable.
Construct the method isThick() for the Book class, which returns true if the book has more than 600 pages, and false otherwise.
Distinguish between the declaration of the collection array and its initialization within the Library constructor.
Describe the role of the formal parameter status in the setBorrowed method.
The array inbound in the FlightManagement class is sorted by Estimated Time of Arrival (ETA).
Assume the following classes/fields/methods are available:
Class FlightManagement
Arrival[] inbound (sorted by Arrival's ETA in increasing order)int last (index of the last occupied element in inbound, so valid entries are 0 to last)Arrival remove(String flightID) (removes and returns the Arrival whose flight has ID flightID)void add(Arrival newArrival) (inserts newArrival into inbound so that it remains sorted by ETA)Class Arrival
String getETA() (returns ETA as a String in the format "HH:MM" so lexicographic order matches time order)boolean getLanded() (returns true if the flight has landed)Flight getMyFlight() (returns the associated Flight)int compareWith(Arrival other) (returns a negative value if this arrival's ETA is before other's ETA, 0 if equal, positive if after)void addDelay(int minutes) (updates this arrival's ETA by adding minutes)Class Flight
String getID() (returns the flight ID)output(x) outputs x.
In this question, interpret “delayed flights” as flights whose ETA is before a given time t and that have not yet landed.
Define the term method signature.
Construct a method showDelayed() that outputs the IDs of all delayed flights in the array inbound that have not yet landed and that have an ETA before a given time t. The time t is passed as a String parameter.
Without using a sorting algorithm, construct the method add(Arrival newArrival) that inserts a newArrival in the sorted array inbound. You may assume that newArrival has been instantiated and that the array inbound is not full.
Construct the method delay(String flightID, int minutes) that finds the Arrival with the given flightID, updates its ETA by adding the delay, and reorganizes the array so that it remains sorted by ETA.
To improve customer satisfaction, a supermarket chain wants to create an object-oriented program (OOP) to simulate the lines of customers at the check-outs in their point of sale (POS) system.
This point of sale (POS) system consists of several check-out counters. After filling their shopping carts with items, customers line up at one of the check-out counters. In most cases, they wait in line until it is their turn to pay.
Three real-world objects are implemented using the following classes:
The UML diagram for the class POSline is provided below.
State the relationship between the POSsystemand POSlineobjects.
Draw a diagram to show the relationship between the objects POSsystem, POSlineand Cart. You are not required to draw a complete UML diagram.
Define the term identifier.
Distinguish between a class and an instantiation. You must make reference to the UML provided.
State the code fragment that instantiates an array line of 20 Cartobjects.
Construct the method public void joinLine(Cart newCart)that adds a newCartto linein the next empty arraylocation. You may assume that lineis not full.
Construct the method public Cart leaveLine(int n)that removes the cart at position nfrom lineby shifting down all the array entries bigger than n, before returning the indicated Cartobject. You may assume that a cart exists at position n.