Clastify logo
Subjects
Features
Review
HOT
Tutoring

B2.3 Programming constructs

Practice exam-style IB Computer Science questions for Programming constructs, 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

Mossmere Nursery prepares seedling trays. Each tray requires three units of growing material, and a preparation run produces at most four trays; the program also counts trays whose preparation starts with more than five units available.

The method trayCost calculates a tray's cost from its number of cells and the rate per cell.

 1  public class NurseryPlan {  
 2      static int trayCost(int cells, int rate) {  
 3          return cells * rate;  
 4      }  
 5      public static void main(String[] args) {  
 6          int stock = 11;  
 7          int trays = 0;  
 8          int wellSupplied = 0;  
 9          while (stock >= 3 && trays < 4) {  
10              if (stock > 5) {  
11                  wellSupplied++;  
12              }  
13              stock -= 3;  
14              trays++;  
15          }  
16          System.out.println(trays);  
17          System.out.println(wellSupplied);  
18      }  
19  }  
A

Explain why a conditional loop is appropriate for controlling a preparation run when the initial stock may vary.

[3]
B

A separate method receives a number of pots, a price per pot, a voucher amount and a delivery charge. The voucher is deducted once from the pot subtotal, and delivery is then added. Describe the steps of this method to return the amount payable, without writing code.

[4]
C

State the smallest integer value of stock for which the condition on line 10 is true.

[1]
D

State the output of the program.

[2]
E

Construct the static integer method orderCost with integer parameters small, large and rate. These represent non-negative numbers of small and large trays and a non-negative rate per cell. Small trays contain six cells and large trays contain nine cells. The method must use trayCost to calculate the cost of each tray size, add a packing charge of two units if at least one tray is ordered, and return the total cost.

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

Velden Parcel Hub calculates label charges using a parcel's integer mass and whether it is fragile. The following program calculates charges for two parcels.

 1  public class ParcelLabels {  
 2      static int labelCharge(int mass, boolean fragile) {  
 3          int charge;  
 4          if (mass <= 0) {  
 5              charge = -1;  
 6          } else if (mass > 4) {  
 7              charge = 8;  
 8          } else if (fragile) {  
 9              charge = 6;  
10          } else {  
11              charge = 4;  
12          }  
13          return charge;  
14      }  
15      public static void main(String[] args) {  
16          int mass = 5;  
17          System.out.println(labelCharge(mass, true));  
18          mass -= 3;  
19          System.out.println(labelCharge(mass, false));  
20      }  
21  }  
A

Explain why the selection chain in labelCharge is appropriate for assigning one charge, rather than using a separate independent if statement for each rule.

[3]
B

A loading method receives a non-negative number of waiting parcels and a positive van capacity. Each dispatch removes up to the van capacity from the waiting parcels, including a final partially filled van if necessary. Describe the steps to return the number of dispatches required, without writing code.

[4]
C

State the name of the local variable declared inside labelCharge that is not a parameter.

[1]
D

State the output of the program.

[2]
E

Construct the static int method deliveryFee with an int parameter mass and boolean parameters express and remote. It returns −1-1 when the mass is zero or negative. Otherwise, the base fee is nine units for a mass greater than six and five units for all other positive masses. Express delivery adds three units. A remote destination adds two units only when express delivery has not been selected. Return the resulting fee.

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

Lumafern Animation Studio uses programs to control brightness changes in animation previews. The following program calculates brightness values for two previews.

 1  public class PreviewBrightness {  
 2      static int pulseValue(int start, int steps) {  
 3          int level = start;  
 4          for (int tick = 0; tick < steps; tick++) {  
 5              level = level + 2;  
 6              level = level * 2;  
 7          }  
 8          return level;  
 9      }  
10      public static void main(String[] args) {  
11          System.out.println(pulseValue(1, 2));  
12          System.out.println(pulseValue(2, 1));  
13      }  
14  }  
A

Explain why defining pulseValue as a reusable method is suitable for the studio's program, rather than copying its calculation instructions wherever they are needed.

[3]
B

A separate method receives a non-negative integer number of animation frames and a Boolean approval value. It returns "EMPTY" if there are no frames, regardless of approval. Otherwise, it returns "HOLD" if approval is false, or "READY" if approval is true.

Describe the steps of this method without writing code.

[4]
C

State the value of tick during the final execution of the loop body in the first call to pulseValue.

[1]
D

State the output of the program.

[2]
E

Construct the static integer method countResets with integer parameters ticks, step and limit. The parameters represent a non-negative number of ticks, a positive brightness increase and a positive brightness limit.

The brightness level starts at zero. On each tick, first increase the level by step. If the resulting level is greater than limit, reset the level to zero and record one reset. A level equal to the limit is not reset.

The method must return the total number of resets after all ticks have been processed.

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

Brindle Cycle Workshop checks bicycle tyre pressures. The flowchart in (Figure 1) counts tyres below a target pressure of 40 psi and calculates the total pressure increase required. contains the trace table for the flowchart.

A

State one benefit of dividing the workshop's program into reusable functions.

[1]
B

Trace in for the tyre pressures 42, 35, 40, 28, 46 and 39 psi, in that order. Copy and complete 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 input or initialization action in that must occur before the loop condition is tested for the first time.

[1]
D

The workshop needs a separate algorithm to process a session containing an unknown number of tyre-pressure readings. Each reading is a non-negative integer; a reading of -1 ends the session and must not be processed. A reading below 40 psi is classified as LOW, a reading from 40 to 50 psi inclusive as READY, and a reading above 50 psi as HIGH.

Construct an algorithm, using numbered steps in structured English, that inputs the readings, outputs the classification of each reading and finally outputs the number classified as LOW. The algorithm must also work when the first input is -1. Do not write code.

[5]
E

A separate tyre check receives a pressure reading and a Boolean value called damaged. A tyre must be labelled CHECK if its pressure is below 40 psi or damaged is true; otherwise it must be labelled PASS.

Sketch a flowchart fragment for this selection, assuming both inputs have already been obtained. Label the decision branches and show both outputs.

[2]
F

The workshop wants a reusable function that receives a tyre pressure and a target pressure as inputs. It must return the pressure increase required when the tyre pressure is below the target, and return zero otherwise. Describe the steps performed by this function, without writing code.

[3]

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.2 Data structures

B2.4 Programming algorithms