Practice IB Computer Science (CS) Topic D.4 Advanced Program Development with authentic exam-style questions for both SL and HL students. This question bank focuses on the exact syllabus content for D.4 Advanced 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.
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.
A different airport has two runways for arriving flights. Two objects of the LinkedList class, named runway1 and runway2, are associated with these two runways.
Arriving flights are added to a list depending on the runway they are assigned to.
Both linked lists contain Arrival objects, corresponding to arriving flights, and are sorted by ETA.
The following diagram represents runway1.
Consider the following method.
void process(LinkedList
{ if (i < myList.size())
{ process(myList, i + 1);
System.out.println(myList.get(i).getETA());
}
}
Outline an advantage of using a library.
Evaluate the use of dynamic linked lists compared to static arrays when managing arriving airplanes.
Sketch the resulting linked list when an airplane with ID RO225, STA 12:05 and delay 0 is added to this list.
Define the term recursion.
Trace the call process(runway1, 0) given the diagram of runway1 as drawn above. Copy and complete the following table.
, recursive call made?, and printed output (ETA). The table has rows for i = 0, 1, 2, and the base case i = 3 with no output.)
In case of bad weather it is possible that one of the runways has to be closed and the two linked lists (runway1 and runway2) need to be merged using a method mergeLists() into one new linked list.
Construct the method public LinkedList<Arrival> mergeLists() that merges the two sorted linked lists into one sorted linked list result which needs to be returned. You may assume that runway1 and runway2 are accessible to the method and do not need to be passed. The original two lists are allowed to become null as a result of the merging.
In answering this question, you may use the following methods from the JETS LinkedList class, in addition to any method provided or developed in this exam paper.
addFirst(
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.
The organizing school arranges for visiting students to stay with a host family. The information about each of the visiting students is stored in a file. The student records are sorted by name. Some of the other variables included are school, gender, age and host family name, as shown in the UML diagram below.
A program needs to be written to match visiting students with host families. The matching process requires data to be manipulated extensively (adding, editing, deleting). The file will be read into RAM.
This program will be used for different events with different numbers of visitors. Therefore, it will be implemented using a dynamic data structure.
It has been decided to use a single linked list named guests to store and manipulate the Visitor objects.
Consider the following diagram which represents the list guests.
The following recursive method has been written to act on the list guests.
public void recursive(int k, char a)
{
if (k == guests.size())
{
output ('no such record');
}
else {
Visitor current = guests.get(k);
if ((current.getGender() == a) && (current.getAge() > 15))
{
output current.getName();
}
else
{
recursive(k + 1,a);
}
}
}
Define the term object reference.
Outline one reason why a linked list may be more suitable than a binary tree in this particular situation.
Construct the code needed to instantiate an object guests of the LinkedList class.
Construct the code for the method penultimate() that returns the second to last element in the linked list guests. You may assume that guests is locally accessible.
Using the data provided in the diagram, trace the call recursive(0,'F'), clearly showing the levels of recursion.
Outline one reason why the use of a recursive method may be inappropriate for linked lists.
Due to unforeseen circumstances, schools may cancel their participation in an event at the last minute. Therefore, a method is needed to remove all the visiting students of one school from the linked list guests.
Construct the method removeSchool that takes the name of a school as parameter and removes all students of that school from the list guests.