Practice IB Computer Science (CS) Topic D. Object-oriented Programming with authentic exam-style questions for both SL and HL students. This question bank focuses on the exact syllabus content for D. Object-oriented Programming 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.
A delivery company uses trains in its operations. It uses an object-oriented program to keep track of its trains and the parcels that it carries. The company has many objects in their program; here are some of them.
| Object | Description |
|---|---|
| Train | Each Train is made up of RollingStock objects, each of which is either a Wagon or an Engine. |
| RollingStock | A RollingStock object can be an Engine (that can pull) or a Wagon (that needs to be pulled). Each RollingStock has a unique ID number and a weight. |
| Engine | A variety of RollingStock. Each Engine has a maximum weight that it can pull. |
| Wagon | A variety of RollingStock. Each Wagon has a maximum cargo weight. |
| Parcel | Each Parcel is tagged with a tracking number, the addresses from where it came (origin) and to where it is going (destination) and its weight. |
The following code implements the Train class used in this program.
public class Train { // Wagons currently coupled to the train private Wagon[] mWagons; private int mWagonCount;
public Train(int maxWagons) { mWagons = new Wagon[maxWagons]; mWagonCount = 0; }
public void addWagon(Wagon w) { if (mWagonCount < mWagons.length) { mWagons[mWagonCount] = w; mWagonCount++; } }
public int getNumberOfWagons() { return mWagonCount; }
public Wagon removeWagon() { if (mWagonCount > 0) { mWagonCount--; Wagon removed = mWagons[mWagonCount]; mWagons[mWagonCount] = null; return removed; } return null; }}Define the function of a constructor.
Outline the advantages of polymorphism, using the RollingStock class as an example.
Construct a unified modelling language (UML) diagram of the Train class.
Construct a method getNumberOfWagons(), part of the Train class, that returns the number of wagons currently coupled to the train.
Construct the removeWagon() method that will remove one wagon from a train and return the removed object. Include appropriate error checking.
Consider the following recursive method.
public void recursionEx(int x){ if(x != 0) { System.out.println(x); recursionEx(x - 1); System.out.println(x); }}Identify two essential features of a recursive algorithm.
Trace the execution of the method call recursionEx(3) and complete the table to show the sequence of calls and the values printed to the console.
| Call | Value of x | Output |
|---|---|---|
| recursionEx(3) | 3 | 3 |
| recursionEx(2) | 2 | 2 |
| recursionEx(1) | 1 | 1 |
| recursionEx(0) | 0 | |
| return to recursionEx(1) | 1 | 1 |
| return to recursionEx(2) | 2 | 2 |
| return to recursionEx(3) | 3 | 3 |
Explain what would happen if the method was called by recursionEx(-3).
public static void recursionEx(int n) { if (n < 0) { System.out.println("Done"); } else { System.out.println(n); recursionEx(n - 1); System.out.println(n); }}Show the output that would be produced when the method is called with recursionEx(-3).
Identify three features which should be included in either program code or UML diagrams to help programmers understand and modify programs in the future.
[3 marks]
The array line in POSline is now replaced by a singly linked list of CartNode objects. The class CartNode has been defined as follows.
public class CartNode
{
private Cart myCart;
private CartNode next;
public CartNode(Cart aCart)
{
this.myCart = aCart;
this.next = null;
}
public Cart getCart(){ return this.myCart; }
public CartNode getNext(){ return this.next; }
public void setNext(CartNode nextNode)
{
this.next = nextNode;
}
}
The class POSlist has been implemented with the standard list methods addLast and removeFirst that act on list.
A method leaveList(int n) is required in the class POSlist, similar to the method leaveLine(int n) that was added to the class POSline.
Define the term object reference.
Using object references, construct the method public Cart removeFirst() that removes the first CartNode object from list. The method must return the Cart object in that node or null if list is empty.
Sketch the linked list list after running the following code fragment where cart1, cart2, cart3, cart4 and cart5 have been instantiated as objects of the class Cart.
POSlist queueList = new POSlist();
queueList.addLast(cart2);
queueList.addLast(cart1);
queueList.addLast(cart4);
queueList.removeFirst();
queueList.addLast(cart5);
queueList.addLast(cart3);
queueList.removeFirst();
Outline one feature of the abstract data structure queue that makes it unsuitable to implement customers waiting in line.
Using object references, construct the method public Cart leaveList(int n) that removes the nth CartNode object from list. The method must return the Cart object in that node.
You may assume that the nth CartNode object exists in the list.
You may use any method declared or developed.
Explain the importance of using coding style and naming conventions when programming.
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.