Clastify logo
Subjects
Features
Review
HOT
Tutoring

B2.2 Data structures

Practice exam-style IB Computer Science questions for Data structures, aligned with the syllabus and grouped by topic.

Paper
Difficulty
Status
Level
Java

This content can't be printed from the browser. Use Export PDF with Pro for an offline copy.

Question 1
SL • Paper 2
Medium
SL • Paper 2
Medium

At Fenwick Cycle Hub, routine inspection requests are processed in arrival order. A queue stores the estimated number of minutes needed for each waiting inspection.

In this program, add() enqueues an item, remove() dequeues an item, and peek() returns the front item without removing it.

 1  import java.util.ArrayDeque;  
 2  import java.util.Queue;  
 3  public class InspectionQueue {  
 4      public static void main(String[] args) {  
 5          Queue<Integer> jobs = new ArrayDeque<>();  
 6          jobs.add(4);  
 7          jobs.add(2);  
 8          jobs.add(5);  
 9          int first = jobs.remove();  
10          jobs.add(3);  
11          int total = 0;  
12          while (!jobs.isEmpty()) {  
13              total += jobs.remove();  
14          }  
15          System.out.println(first);  
16          System.out.println(total);  
17      }  
18  }  
A

Explain why a queue is suitable for storing the waiting inspection requests.

[3]
B

A different implementation stores the queue in an ArrayList, with the front item at index 0. Describe the steps of a dequeue operation that returns the front item. Include what happens when the queue is empty and how the remaining items are stored. Do not write code.

[4]
C

State the value at the front of jobs immediately after line 10 has executed.

[1]
D

State the output of the program.

[2]
E

Construct the public static method fitJobs with parameters Queue<Integer> jobs and int available, returning an integer. The queue contains positive inspection times and available is non-negative.

The method must repeatedly remove the front inspection if its time does not exceed the time still available, deducting its time from the available time. It must stop when the queue is empty or the front inspection will not fit. Inspections must not be skipped or reordered. Return the number of inspections removed. Use the queue operations shown in the program.

[5]
Question 2
SL • Paper 2
Medium
SL • Paper 2
Medium

The Tallowmere Costume Workshop prepares ribbon for costumes. The flowchart in builds a dynamic List, cuts, containing cutting positions from an array, lengths, of positive integer ribbon lengths measured in centimetres. The initial zero in cuts represents the beginning of the ribbon. Array indexing starts at 0.

contains a trace table for the flowchart.

A

State one characteristic of a static data structure.

[1]
B

Trace the flowchart in for lengths containing [4, 7, 3, 6]. Copy and complete the trace table in . Each row records one evaluation of a test: write that test's result in its column, write a variable's value only when it changes, and record the output on the row where it is produced.

[3]
C

State one property of the values stored in cuts that remains true throughout the loop.

[1]
D

The workshop also needs to recover individual ribbon lengths from a List of cutting positions. This List contains at least two values, starts with zero and is in strictly increasing order.

Construct an algorithm, using numbered steps in structured English, that receives this List and outputs a new List containing the differences between consecutive cutting positions, in the same order. Do not write code.

[5]
E

An initially empty queue stores costume labels awaiting ribbon attachment. The following operations are performed in order:

  1. Enqueue label P.
  2. Enqueue label R.
  3. Inspect the front label using front.
  4. Dequeue one label.
  5. Enqueue label T.
  6. Enqueue label V.
  7. Dequeue one label.
  8. Enqueue label W.

Sketch the final queue. Clearly label its front and rear.

[2]
F

A stack stores planned cutting positions in strictly increasing order from bottom to top. The workshop receives a shorter ribbon and must discard every planned position greater than its new length, while leaving all other positions in the stack. The stack may initially be empty.

Describe the steps required. Use only the stack operations pop, peek and isEmpty to access or change the stack. Do not write code.

[3]
Question 3
SL • Paper 2
Medium
SL • Paper 2
Medium

Mossbridge Nursery maintains a changing collection of growing trays. Moisture readings are stored in a two-dimensional array, with one row per recording session and one column per tray; a zero indicates a missing reading.

 1  public class TrayReadings {  
 2      public static void main(String[] args) {  
 3          int[][] readings = {{0, 4, 0}, {3, 2, 0}};  
 4          for (int row = 0; row < readings.length; row++) {  
 5              int missing = 0;  
 6              for (int column = 0; column < readings[row].length; column++) {  
 7                  if (readings[row][column] == 0) {  
 8                      missing++;  
 9                  }  
10              }  
11              System.out.println(missing);  
12          }  
13      }  
14  }  
A

The nursery also stores registered tray identifiers. Trays are registered and retired throughout the year. Explain why an ArrayList is more suitable than a fixed-size array for storing these identifiers.

[3]
B

Retired tray identifiers are temporarily replaced by zero in an ArrayList of integers. Describe the steps of a method that removes every zero from this list while preserving the order of the other identifiers. Your method must also work when consecutive entries are zero. Do not write code.

[4]
C

State what the condition on line 7 tests.

[1]
D

State the output of the program.

[2]
E

Construct the public static method completeTrays, which receives int[][] readings and returns an ArrayList<Integer> containing, in increasing order, the column indices for which every reading is greater than zero.

The array has at least one row and one column, and every row has the same length. The original readings must not be changed. You may assume that ArrayList has been imported.

[5]
Question 4
SL • Paper 2
Medium
SL • Paper 2
Medium

Rookmere Produce stores empty reusable crates in vertical piles. Each crate carries an integer batch code, and a stack records the codes in each pile as crates are added or removed.

The program uses push() to add a code, pop() to remove and return the top code, and peek() to inspect the top code without removing it.

 1  import java.util.Stack;  
 2  public class CratePile {  
 3      public static void main(String[] args) {  
 4          Stack<Integer> crates = new Stack<>();  
 5          crates.push(6);  
 6          crates.push(9);  
 7          crates.push(2);  
 8          crates.pop();  
 9          crates.push(4);  
10          System.out.println(crates.pop());  
11          crates.pop();  
12          System.out.println(crates.peek());  
13      }  
14  }  
A

Explain why a stack is suitable for recording the crates in a pile.

[3]
B

A fixed-capacity implementation stores stack codes in an array. A variable count stores the number of items, so the next unused position has index count. Describe the steps for pushing a new code. Include what happens if the stack is full. Do not write code.

[4]
C

State the code at the top of crates immediately after line 8 has executed.

[1]
D

State the output of the program.

[2]
E

Construct the public static method removeBatch, which receives Stack<Integer> crates and int rejected and returns an integer.

The method must remove every occurrence of the rejected batch code, leave all other codes in their original bottom-to-top order in crates, and return the number removed. The original stack may be empty.

Use one temporary stack. Access stack contents only through push(), pop(), peek() and isEmpty(). You may assume that Stack has been imported.

[5]

Build a Practice Exam — Computer Science HL

Test your knowledge with a custom paper containing questions from selected topics.

Practice exam question preview
Practice exams dashboard preview

B2.1 Programming fundamentals

B2.3 Programming constructs