Clastify logo
Subjects
Features
Review
HOT
Tutoring

B2.1 Programming fundamentals

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

Orin Oral History Archive uses a program to prepare titles for audio recordings. A title-processing routine follows these numbered steps. The variables title, position and result are local to this routine. Character positions start at zero.

  1. Set title to "a_b_c".
  2. Set position to zero and result to the empty string.
  3. If position equals the number of characters in title, continue at step 7.
  4. Examine the character at position in title. If it is an underscore, concatenate one space to result. Otherwise, concatenate the examined character to result.
  5. Increase position by one.
  6. Return to step 3.
  7. Output result and end the routine.
A

State one potential point of failure that exception handling could manage when the archive program processes user-supplied recording information.

[1]
B

Trace the numbered steps. Copy and complete a trace table with the column headings position, result and output. Record a row immediately after step 2, after every execution of step 5, and after step 7. Use quotation marks around string values so that spaces are visible, and use a dash when no output occurs.

[3]
C

State the scope of the local variable result.

[1]
D

Some recording descriptions contain a private note enclosed in square brackets. Each description contains exactly one opening bracket and one closing bracket, in that order. Either bracket may be at an end of the description.

Construct an algorithm, using numbered steps in structured English, that receives a description and outputs it with the private note and both brackets removed. Preserve every character outside the brackets in its original order. For example, "Interview[permission pending] recorded" must produce "Interview recorded".

Do not write programming code.

[5]
E

A separate routine receives a recording duration as text. In a try block, it attempts to convert the text to an integer and then displays the converted duration. A matching catch/except block displays "Invalid duration" if conversion fails. A finally block displays "Check finished".

Sketch the control flow for successful conversion and failed conversion. Label the try, catch/except and finally sections.

[2]
F

A programmer implements the title-processing routine, but it outputs "abc" for the title "a_b_c". Describe the steps the programmer could follow to locate and correct this fault using step-by-step execution.

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

Rill Cycle Hire is developing a program to calculate hire charges and produce equipment labels. The following program contains a logic error: the charge should be the number of hours multiplied by the hourly rate.

 1  public class HireCheck {  
 2      static double hourlyRate = 1.5;  
 3      public static double quote(int hours) {  
 4          double cost = hours + hourlyRate;  
 5          return cost;  
 6      }  
 7      public static void main(String[] args) {  
 8          System.out.println(quote(2));  
 9          System.out.println(quote(4));  
10      }  
11  }  
A

Customers enter the hire duration as text, which the program converts to an integer. Explain why exception handling is suitable for this conversion.

[3]
B

An equipment label contains a zone followed by a colon and an equipment identifier. Both sections are non-empty and there is exactly one colon. A method must return the identifier in uppercase, followed by a slash and then the unchanged zone. For example, west:bk7 becomes BK7/west. Describe the steps of this method without writing code.

[4]
C

State the name of one local variable in the method quote.

[1]
D

State the two lines of output produced by the printed program before the logic error is corrected.

[2]
E

Construct a static method named charge for the same class. It receives, in order, the non-negative integer hours and the Boolean helmet, and returns a decimal charge as a double. Calculate the hire charge using the global variable hourlyRate. Add a single surcharge of 2.0 if a helmet is required. Use a local variable to store the charge and do not change hourlyRate.

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

Moss Seed Exchange uses a kiosk to print packet labels and calculate bills. The following program processes a sample label and calculates two bills.

 1  public class KioskCheck {  
 2      static int deliveryFee = 2;  
 3      public static int bill(int packets) {  
 4          int subtotal = packets * 3;  
 5          return subtotal + deliveryFee;  
 6      }  
 7      public static void main(String[] args) {  
 8          String label = "ab:17";  
 9          String prefix = label.substring(0, 2);  
10          System.out.println(bill(3));  
11          System.out.println(bill(1));  
12      }  
13  }  
A

During development, a bill is incorrect. Explain why breakpoint debugging is suitable for investigating the calculation in the method bill.

[3]
B

A separate method receives a string containing a packet quantity. It must attempt to convert the string to an integer and return that integer. If conversion fails, it must return -1. It must print Checked whether conversion succeeds or fails. Describe the steps of this method using exception handling, without writing code.

[4]
C

State the substring stored in prefix after line 9 executes.

[1]
D

State the two lines of output produced by the printed program.

[2]
E

Construct a static method named normaliseLabel that receives one string parameter, label, and returns a string. The label contains a non-empty prefix, one colon and a non-empty description. Return the prefix in uppercase, followed by a hyphen and the description with every underscore replaced by a space. Exclude the colon. For example, rt:red_bean must produce RT-red bean.

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

Vela Parcel Room is developing a program to process parcel quantities and destination labels. The following program processes one sample parcel record.

 1  public class ParcelCheck {  
 2      public static void main(String[] args) {  
 3          try {  
 4              int quantity = Integer.parseInt("4");  
 5              String label = "east|glass";  
 6              int separator = label.indexOf("|");  
 7              String area = label.substring(0, separator);  
 8              String item = label.substring(separator + 1);  
 9              item = item.replace("glass", "paper");  
10              System.out.println(area.toUpperCase());  
11              System.out.println(item + quantity);  
12          } catch (NumberFormatException error) {  
13              System.out.println("Invalid quantity");  
14          }  
15      }  
16  }  
A

The completed program will process each parcel record using a separate call to a method. Explain why local variables are suitable for storing the quantity and label being processed by that method.

[3]
B

A separate method is intended to double a parcel quantity but incorrectly adds 2 to it instead. Describe a step-by-step debugging procedure to locate and correct this error. Include a suitable test input and its expected result. Do not write code.

[4]
C

State the exception type handled by the printed program.

[1]
D

State the two lines of output produced by the printed program.

[2]
E

Construct a static method named unitsPerBox that receives two string parameters in this order: totalText and boxesText. It returns an integer. Successfully converted values will be non-negative integers within the supported integer range. Convert both strings to integers and return the integer quotient of the total divided by the number of boxes, discarding any remainder. Use exception handling to return -1 if either conversion fails or if the number of boxes is zero. Use a finally block to print Processed after every attempt, whether it succeeds or fails.

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