Practice IB Computer Science (CS) Topic D.1 Objects as a Programming Concept with authentic exam-style questions for both SL and HL students. This question bank focuses on the exact syllabus content for D.1 Objects as a Programming Concept 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 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.
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.
A fitness tracking application records various physical activities. Each activity is stored as a Workout object within a TrainingLog.
To improve the user experience, the application provides a feature to organize the workout history. Users can extract specific workouts into a separate 'Focus List' based on their intensity or performance metrics.
For this system, the focus list is generated by identifying all workouts that exceed a user-defined distance threshold. When a workout is moved to the Focus List, it is removed from the main training log to prevent duplication in the primary view.
In the design of the class Session, a variable isPaused is used to track the state of a workout. Outline two reasons why the boolean data type is the most appropriate choice for this variable.
Construct the code for a method filterByDistance (part of the class TrainingLog), which takes an array of Workout objects called history and a double value threshold as parameters.
The method should create a new Workout array of fixed size 50 and copy into it only those workouts whose distance is strictly greater than the threshold (unused positions in the returned array may remain null). These workouts should be removed from the history array (by setting their index to null) once they are copied.
Assume the history array has a fixed size of 50 and that the Workout class has a method getDistance() which returns a double. The method should return the new array of workouts.
You may use any standard programming techniques or methods you have developed.
The fitness application tracks and stores the precise GPS coordinates of users during their workouts.
Describe two ethical considerations the developers must address regarding the collection and management of this location data.
A software development company is designing a system to manage tasks and projects. The system uses objects to represent tasks and projects. Each task has attributes such as title, description, and status, while each project has a name, deadline, and a collection of tasks.
Define the term 'object' in the context of object-oriented programming.
Explain the relationship between classes and objects using the task and project system as an example.
Describe how encapsulation is applied in the task and project system.
Construct a UML class diagram to represent the 'Task' and 'Project' classes, showing their attributes and methods.
Discuss the importance of using objects to model tasks and projects in software development.