Practice IB Computer Science (CS) Topic D.2 Features of OOP with authentic exam-style questions for both SL and HL students. This question bank focuses on the exact syllabus content for D.2 Features of OOP and mirrors Paper 1, 2, 3 style where relevant.
Get instant solutions, detailed explanations, and build confidence with questions aligned to IB examiner expectations.
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 software company is developing a system for smart-agriculture monitoring. The system is designed using an object-oriented approach with distinct modules for sensors, irrigation hardware, and data analysis.
Describe two benefits that the development team would experience by utilizing modularity in the design of this system.
Construct a UML class diagram for the SoilSensor class. The class must include the attributes sensorID, moistureLevel, and batteryLife, as well as the methods getReading() and updateFirmware(). You do not need to include data types or access modifiers.
The agricultural system manages various types of hardware devices, such as DripIrrigator, MistSprinkler, and Heater.
Explain how the concept of polymorphism could be used to allow a central controller to manage these different devices efficiently and how this affects the scalability of the system.
An e-commerce platform is designing a shopping cart system with classes like Product, Electronic, and Clothing. The Electronic and Clothing classes inherit from the Product class.
Define the term polymorphism.
Explain one advantage of polymorphism in this shopping cart system.
Explain two advantages of encapsulation in this context.
A software engineer is developing an inventory management system for a warehouse. To find specific items quickly, the engineer is implementing a search module. The initial version of the search function is shown below:
public int linearSearch(String[] inventory, String target){ int index = -1; for (int i = 0; i < inventory.length; i++) { if (inventory[i].equals(target)) { index = i; } } return index;}The warehouse currently stores thousands of items. The engineer is considering ways to optimize the search process and ensure the software is easy to maintain for future developers who might work on the system.
Outline one advantage of using modular sub-programs (methods or functions) in a large software project.
Describe two ways in which a syntax error differs from a logical error during the development process.
State the appropriate data type for the target parameter in the linearSearch function.
State the appropriate data type to be used for a variable that stores the total quantity of a specific item in stock.
State the appropriate data type for a variable that indicates whether a specific item is currently out of stock.
The developer is considering replacing the current linear search with a binary search algorithm. Identify two factors that must be considered to successfully implement this change.
Outline three ways in which thorough internal documentation (comments) and external documentation (technical manuals) assist in the maintenance of this software.
A software company is developing a Hospital Management System that includes classes such as Employee, Doctor, and Nurse. Both Doctor and Nurse inherit from the Employee class.
Define the term inheritance.
Explain one advantage of using inheritance in this Hospital Management System.
Describe two advantages of using libraries of objects in developing this system.