Example test [278 marks]
1. [Maximum mark: 15]
A school teacher decides to write a program to store class records and marks. Part of this program involves using a
sort algorithm. The algorithm shown is a selection sort and to test it, the teacher has set up an array VALUES[] with 5
elements of test data.
LIMIT = 4
loop COUNTER1 from 0 to LIMIT – 1
MINIMUM = COUNTER1
loop COUNTER2 from COUNTER1 + 1 to LIMIT
if VALUES[COUNTER2] < VALUES[MINIMUM] then
MINIMUM = COUNTER2
end if
end loop
if MINIMUM ≠ COUNTER1 then
TEMPORARY = VALUES[MINIMUM]
VALUES[MINIMUM] = VALUES[COUNTER1]
VALUES[COUNTER1] = TEMPORARY
end if
end loop
(a) Identify two variables that have been used in the algorithm.
[1]
(b) Copy and complete the table below to trace the algorithm using the data set:
20, 6, 38, 50, 40
[5]
(c.i) With reference to the algorithm in the flow chart, construct this algorithm in pseudocode so that it
performs the same function. [3]
(c.ii) State the type of sort in the algorithm constructed in c(i).
[1]
(d) Construct an algorithm fragment to output the data in the array VALUES[].
[2]
(e) The sorting algorithm could be part of a sub-program within a larger program.
Explain the benefits of using sub-programs when constructing a larger program. [3]
2. [Maximum mark: 15]
There were 10 gymnasts who took part in a gymnastics competition. The names and the scores for all competitors
were sorted in alphabetical order and stored in two arrays, NAMES and SCORES (see Figure 1).
Figure 1: Data held in the NAMES array and the SCORES array
For example, the score awarded to Mary Allen was 6.5 .
(a) State the name of the gymnast whose score is stored in SCORES[5].
[1]
(b) Construct an algorithm in pseudocode to determine the average score.
[3]
To qualify for the next round of competition, a competitor must have a score above the average score.
(c) Construct an algorithm in pseudocode that will determine and output the number of gymnasts
whose scores are above the average score.
You may assume that the average score is stored in the variable AVERAGE.
[4]
An algorithm is needed that:
allows input of a gymnast’s name
searches for this name in the NAMES array using a binary search
outputs the gymnast’s score. If the inputted name does not occur in the NAMES array, it outputs an appropriate
message.
For example, from the data given in Figure 1:
if “Allen, Mary” is the input name, then 6.5 should be output
if “Peterson, Tina” is the input name, then a message saying “this name is not found” should be output.
(d) Construct an algorithm in pseudocode as described above.
[7]
3. [Maximum mark: 15]
(a) Describe one standard operation of collections. [2]
The collection RAINBOW holds the names of the main colours of the rainbow along with their order indicator, as
follows:
Blue, E, Green, D, Indigo, F, Orange, B, Red, A, Violet, G, Yellow, C
(b) Construct an algorithm using pseudocode to read the data from RAINBOW and store the names of
the colours in a one-dimensional string array, COLOUR, and the order indicators in another one-
dimensional string array, ORDER.
The matching colour and order indicator must have the same index in the COLOUR and
ORDER arrays. [4]
(c) Construct an algorithm using pseudocode to sort the contents of the array ORDER into alphabetical order.
The indexes for the corresponding data in the two parallel arrays must remain the same after sorting. [5]
(d) Compare and contrast the bubble sort algorithm and the selection sort algorithm. [4]
4. [Maximum mark: 15]
There were 10 gymnasts who took part in a gymnastics competition. The names and the scores for all competitors
were sorted in alphabetical order and stored in two arrays, NAMES and SCORES (see Figure 1).
Figure 1: Data held in the NAMES array and the SCORES array
For example, the score awarded to Mary Allen was 6.5 .
(a) State the name of the gymnast whose score is stored in SCORES[5].
[1]
(b) Construct an algorithm in pseudocode to determine the average score.
[3]
To qualify for the next round of competition, a competitor must have a score above the average score.
(c) Construct an algorithm in pseudocode that will determine and output the number of gymnasts
whose scores are above the average score.
You may assume that the average score is stored in the variable AVERAGE.
[4]
An algorithm is needed that:
allows input of a gymnast’s name
searches for this name in the NAMES array using a binary search
outputs the gymnast’s score. If the inputted name does not occur in the NAMES array, it outputs an appropriate
message.
For example, from the data given in Figure 1:
if “Allen, Mary” is the input name, then 6.5 should be output
if “Peterson, Tina” is the input name, then a message saying “this name is not found” should be output.
(d) Construct an algorithm in pseudocode as described above.
[7]
5. [Maximum mark: 6]
(a) Construct a trace table for the following algorithm:
N = 1216
X=0
loop while N > 0
X = X + N mod 10
N = N div 10
end loop
output (X) [4]
(b) Deduce the purpose of the algorithm. [2]
6. [Maximum mark: 6]
Given the integer array DATA:
and the following algorithm:
K=0
A=0
B=0
loop while K<6
if DATA[K]>4 and DATA[K]<8
A=A+DATA[K]
B=B+1
end if
K=K+1
end loop
C=A/B
(a) Construct a trace table for this algorithm. [4]
(b) Deduce the purpose of this algorithm. [2]
7. [Maximum mark: 15]
There are 1000 rooms where students can live on a college campus. The rooms are numbered from 1 to 1000. The bill
for each room must be paid each month.
The room numbers of the students who have paid their bills for the month of May so far are stored in the
ROOMNUMS array (see Figure 1).
Figure 1: Example data stored in the ROOMNUMS array
For example, the bill for room number 93 has been paid. This can be seen in ROOMNUMS[6].
(a) State two characteristics of a linear array. [2]
The campus administrator would like to check whether the student in room number X has paid the bill for the month
of May or not.
The sub-program check(ROOMNUMS) accepts the array ROOMNUMS, allows room number X to be input, searches
for X in the ROOMNUMS array, and outputs an appropriate message.
(b) Identify two types of searching algorithm. [2]
Figure 2 shows two examples of input and output for the data stored in the ROOMNUMS array as given in Figure 1.
Figure 2: Examples of input and output
(c) Construct an algorithm in pseudocode for the sub-program check(). [5]
(d) Construct an algorithm in pseudocode to sort the ROOMNUMS array in ascending order using the
selection sort algorithm. [6]
8. [Maximum mark: 15]
There are 1000 rooms where students can live on a college campus. The rooms are numbered from 1 to 1000. The bill
for each room must be paid each month.
The room numbers of the students who have paid their bills for the month of May so far are stored in the
ROOMNUMS array (see Figure 1).
Figure 1: Example data stored in the ROOMNUMS array
For example, the bill for room number 93 has been paid. This can be seen in ROOMNUMS[6].
(a) State two characteristics of a linear array. [2]
The campus administrator would like to check whether the student in room number X has paid the bill for the month
of May or not.
The sub-program check(ROOMNUMS) accepts the array ROOMNUMS, allows room number X to be input, searches
for X in the ROOMNUMS array, and outputs an appropriate message.
(b) Identify two types of searching algorithm. [2]
Figure 2 shows two examples of input and output for the data stored in the ROOMNUMS array as given in Figure 1.
Figure 2: Examples of input and output
(c) Construct an algorithm in pseudocode for the sub-program check(). [5]
(d) Construct an algorithm in pseudocode to sort the ROOMNUMS array in descending order using the
bubble sort algorithm. [6]
9. [Maximum mark: 6]
Given the integer array DATA:
and the following algorithm:
K=5
A=1
B=0
while K >= 0
if DATA[K] mod 2 < 1
then
A = A * DATA[K]
else
B=B+1
end if
K=K-1
end while
output(A)
output(B)
(a) Construct a trace table for this algorithm. [4]
(b) Deduce the purpose of this algorithm. [2]
10. [Maximum mark: 4]
Given the one-dimensional array NAMES:
construct a trace table for the following algorithm:
K=3
loop while K>=0
A=K mod 3
output (NAMES[A])
K=K−1
end while [4]
11. [Maximum mark: 3]
List the output from the given algorithm for the following input.
2, 6, 8, 9, 12, 15, 18, 20
loop for Count from 0 to 7
input NUMBER
if NUMBER div 2 = NUMBER / 2 then
if NUMBER div 3 = NUMBER / 3 then
output NUMBER
end if
end if
end loop [3]
12. [Maximum mark: 5]
Construct a trace table for the following algorithm.
K=1
N=1
M=2
loop while K < 5
output(N,M)
K=K+1
N=N+2
M=M*2
end loop [5]
13. [Maximum mark: 4]
Construct a trace table for the following algorithm
A=3
B=7
loop while B >= A
A=A+1
output(B − A)
B=B−1
end loop [4]
14. [Maximum mark: 4]
Describe the steps involved in using the bubble sort algorithm to sort an array. [4]
15. [Maximum mark: 15]
An integer divisor of an integer, N(N>0), is an integer greater than zero that divides N without leaving a remainder.
The proper divisors of N are divisors of N other than N itself.
For example:
The divisors of 10 are 1, 2, 5 and 10.
The proper divisors of 10 are 1, 2 and 5.
(a.i) State the number of proper divisors of 2. [1]
(a.ii) State why 4 is not a proper divisor of 10. [1]
Every number can be classified as abundant, deficient, or perfect according to the following definitions:
A number is an abundant number if it is less than the sum of its proper divisors. For example, 12 is an abundant
number because 1 + 2 + 3 + 4 + 6 = 16, and 16 > 12.
A number is a deficient number if it is greater than the sum of its proper divisors. For example, 9 is a deficient number
because 1 + 3 = 4, and 4 < 9.
A number is a perfect number if it is equal to the sum of its proper divisors. For example, 28 is a perfect number
because 1 + 2 + 4 + 7 + 14 = 28, and 28 = 28.
(b) Construct an algorithm in pseudocode that will accept an integer, K(K>0), and output whether K is
an abundant number, a deficient number, or a perfect number. [6]
(c) The one-dimensional array DATA holds X(X>0) elements. Each element in the array is an integer
greater than zero.
The subprogram isAbundant() is available. It accepts an integer, N, and returns True if N is an
abundant number, otherwise it returns False. For example, isAbundant(17)returns False.
Construct an algorithm in pseudocode to:
determine the number of abundant integers in the array DATA that are odd
determine the number of abundant integers in the array DATA that are even
output these two numbers.
You should call the isAbundant() subprogram.
[7]
You can assume that the value of X and the array DATA have already been inputted.
16. [Maximum mark: 15]
The array DATA_ARR[] is a one-dimensional array of 12 integers.
Algorithm 1 represents a method of searching the array DATA_ARR[] to see if it contains a specific value.
Algorithm 1
input TO_FIND
LIMIT = 11
LOC = FALSE
ITERATE = 0
loop while not LOC and ITERATE <= LIMIT
if DATA_ARR[ITERATE] = TO_FIND then
LOC = TRUE
end if
ITERATE = ITERATE + 1
end loop
if LOC then
output TO_FIND, "is in the array"
else
output TO_FIND, "is NOT in the array"
end if
(a) Copy and complete the trace table for Algorithm 1 using TO_FIND = 39.
The first value of the first row has been done for you.
[4]
(b) State the constant used in Algorithm 1. [1]
Algorithm 2 represents an alternative method of searching the array DATA_ARR[] to see if it contains a specific
value.
Algorithm 2
input TO_FIND
LOC = FALSE
LOW_LIM = 0
UP_LIM = 11
loop while LOW_LIM <= UP_LIM and LOC = FALSE
MID_VAL = (LOW_LIM + UP_LIM) div 2
if DATA_ARR[MID_VAL] = TO_FIND then
LOC = TRUE
else
if TO_FIND < DATA_ARR[MID_VAL] then
UP_LIM = MID_VAL - 1
else
LOW_LIM = MID_VAL + 1
end if
end if
end loop
if LOC = TRUE then
output TO_FIND, "is in the array"
else
output TO_FIND, "is NOT in the array"
end if
(c) Copy and complete the trace table for Algorithm 2 using TO_FIND = 50.
The first two values of the first row have been done for you.
[4]
(d) Outline why MID_VAL could not be a constant. [1]
(e) Evaluate the use of a sequential search and a binary search including the advantages and
disadvantages of each. [5]
17. [Maximum mark: 15]
A teacher would like a simple program to store the names, marks and grades of students in a set of three parallel one-
dimensional arrays called NAME[], MARK[] and GRADE[] .
The grade boundaries for the individual grades are shown below:
The class has 30 students.
(a) Identify two components in a conditional statement.
[2]
(b) Construct an algorithm using pseudocode to take the marks that have been stored in MARK[],
convert them into the appropriate grade and store the calculated grades in GRADE[]. [5]
(c) Outline how the name, mark and grade in the three arrays correspond to the same student.
[2]
(d) Construct an algorithm using pseudocode to output the names and grades of all students who
achieve a grade of Merit or Distinction. [3]
(e) Explain how you would change your algorithm in part (d) to allow a user to choose a grade and
output the names and marks of the students who have achieved this grade. [3]
18. [Maximum mark: 15]
A teacher would like a simple program to store the names, marks and grades of students in a set of three parallel one-
dimensional arrays called NAME[], MARK[] and GRADE[] .
The grade boundaries for the individual grades are shown below:
The class has 30 students.
(a) Identify two components in a conditional statement.
[2]
(b) Construct an algorithm using pseudocode to take the marks that have been stored in MARK[],
convert them into the appropriate grade and store the calculated grades in GRADE[]. [5]
(c) Outline how the name, mark and grade in the three arrays correspond to the same student.
[2]
(d) Construct an algorithm using pseudocode to output the names and grades of all students who
achieve a grade of Merit or Distinction. [3]
(e) Explain how you would change your algorithm in part (d) to allow a user to choose a grade and
output the names and marks of the students who have achieved this grade. [3]
19. [Maximum mark: 15]
A school has 100 students. All student names (strings) and student ID numbers (five-digit integers) are held in two
separate one-dimensional arrays named SID and SNAMES.
For example, student Pia Baranger has ID number 11876.
A binary search algorithm is not used to find a particular name in array SNAMES.
(a) State the reason for not using a binary search. [1]
The school offers its sporting program to students and has a basketball team, a tennis team and a football team. Each
student must choose at least one of these three sports.
Three collections, BASKETBALL, TENNIS and FOOTBALL, are created. When a student chooses a sporting activity,
their ID number is added to the appropriate collection.
For example:
BASKETBALL={10011, 11876, 10122}
TENNIS={10011, 11876, 10002}
FOOTBALL={10011, 10002, 22103, 32000}
The method isIn(X, COL) is available, where:
X is a five-digit integer representing an ID number
COL is a collection that holds student ID numbers.
The method isIn(X, COL) returns True if the ID number X is in the collection COL; False otherwise.
For example:
isIn(11876, BASKETBALL) returns True
isIn(11876, FOOTBALL) returns False
(b) Construct an algorithm in pseudocode for the method isIn(X, COL). [4]
The football and tennis training sessions are held at the same time. The football coach would like to know how many
students will not be able to attend the football training session because they will be attending the tennis training
session.
(c) Construct an algorithm in pseudocode that will output the number of students who have chosen
both tennis and football. The method isIn() should be used in your answer. [3]
(d) The school coordinator would like to check whether there are students who have not yet chosen
any one of the three sports.
Construct an algorithm in pseudocode that will output the names of students who have not yet
chosen any one of the three sports. An appropriate message should be displayed if every student
has chosen a sport. [7]
20. [Maximum mark: 15]
(a) Consider the following algorithm.
N = 372
X = N DIV 100
Y = X + 10 * (N MOD 100 DIV 10)
Z = Y + (N MOD 10) * 100
Determine the values of variables X, Y, and Z after execution of this algorithm. Show your working. [3]
NUMBERS is a collection that holds only positive integers.
A three-digit number has three digits: a hundreds digit, a tens digit and a units digit.
For example, for 406, its hundreds digit is 4, its tens digit is 0 and its units digit is 6.
An algorithm is needed to copy each three-digit number from the collection NUMBERS, where the hundreds digit is
smaller than its tens digit and its tens digit is smaller than its units digit, into a one-dimensinal array named THREE. If
there are no such numbers in the collection then an appropriate message should be displayed.
For example:
If NUMBERS = {9, 3456, 12, 237, 45679, 368, 296}
then the contents of the array, THREE, is:
If NUMBERS = {1234, 56, 90, 324, 876}
then the array THREE is empty and a message such as “No such numbers”, should be outputted.
(b) Construct this algorithm. You may assume that the array THREE is initialized with a sufficient
number of elements. [8]
(c) Describe how a selection sort algorithm could be used to sort the array THREE in ascending order. [4]
21. [Maximum mark: 15]
The following method, calcBMI() accepts person’s height (H) in metres (m) and weight (W) in kilograms (kg) and
returns their Body Mass Index (BMI).
calcBMI(H, W)
X=H*H
B=W/X
return B
endcalcBMI
Boris weighs 104 kg and is 2.00 m tall. His BMI can be calculated by calling method calcBMI() as follows
BorisBMI = calcBMI(2.00, 104).
(a) State the value of variable BorisBMI. [1]
A person can belong to one of the following four weight categories:
(b) Use pseudocode to construct an algorithm which accepts a person’s BMI and outputs the weight
category the person belongs to. [4]
The data about a group of adults and their height measurement (in metres) and weight measurement (in kg) is held
in three one-dimensional arrays.
Where
NAME is a one-dimensional array holding names (currently sorted in alphabetical order).
WEIGHT is a one-dimensional array holding weight measurement in kilograms.
HEIGHT is a one-dimensional array holding height measurement in metres.
For example,
NAME[0] is Annie.
Her weight measurement is 52.40 kg and can be found in WEIGHT[0].
HEIGHT[0] is 1.56 which represents Annie’s height measurement in metres.
(c) State the name of the person whose height is held in HEIGHT[3]. [1]
(d.i) Identify one reason why a binary search algorithm cannot be used to find the name of person
whose height is given. [1]
(d.ii) Describe how the name of person whose height is given could be output. [2]
(e) Construct an algorithm which will output the names of all the people whose BMI is greater than this
group’s average BMI.
You should call method calcBMI() in your answer. [6]
22. [Maximum mark: 15]
The following flowchart represents a standard algorithm:
(a) Copy and complete the table that traces the algorithm in the flowchart using an input value of 19.
[4]
(b) State the purpose of the algorithm. [1]
(c) Construct the algorithm from the flowchart using pseudocode. Add additional pseudocode to
ensure that input is validated to only allow positive integers to be entered. [6]
(d) Efficiency is an important consideration when designing algorithms to ensure they don’t waste
computer resources such as memory or processing time.
Suggest two design considerations that could be made to an algorithm that would make it more
efficient. [4]
23. [Maximum mark: 15]
A company has 600 employees whose names are currently stored using a collection called NAMES. The names are
stored as surname, first name. For example: Smith, Jane, Uysal, Rafael, Ahmed, Ishmael, Jonsonn, Sara, …
(a) Construct a pseudocode algorithm that will store the surnames in one array and first names in
another. [4]
The names in the collection are kept in a random order. However, it would be more useful if they were kept in
alphabetical order.
(b) Construct a pseudocode algorithm that will sort the surnames into alphabetical order using the
bubble sort method. The order of the first names must also be changed so that they keep the same
index as their corresponding surname. [5]
The company’s staff list is now organized in the arrays in alphabetical order.
A binary search was used to find a specific name in the array.
(c) Describe the process a binary search would follow to find a record in the surname array. [4]
(d) Outline one benefit of using sub-programmes to implement your algorithms from parts (a) and (b). [2]
24. [Maximum mark: 15]
A school teacher decides to write a program to store class records and marks. Part of this program involves using a
sort algorithm. The algorithm shown is a selection sort and to test it, the teacher has set up an array VALUES[] with 5
elements of test data.
LIMIT = 4
loop COUNTER1 from 0 to LIMIT – 1
MINIMUM = COUNTER1
loop COUNTER2 from COUNTER1 + 1 to LIMIT
if VALUES[COUNTER2] < VALUES[MINIMUM] then
MINIMUM = COUNTER2
end if
end loop
if MINIMUM ≠ COUNTER1 then
TEMPORARY = VALUES[MINIMUM]
VALUES[MINIMUM] = VALUES[COUNTER1]
VALUES[COUNTER1] = TEMPORARY
end if
end loop
(a) Identify two variables that have been used in the algorithm.
[1]
(b) Copy and complete the table below to trace the algorithm using the data set:
20, 6, 38, 50, 40
[5]
(c.i) With reference to the algorithm in the flow chart, construct this algorithm in pseudocode so that it
performs the same function. [3]
(c.ii) State the type of sort in the algorithm constructed in c(i).
[1]
(d) Construct an algorithm fragment to output the data in the array VALUES[].
[2]
(e) The sorting algorithm could be part of a sub-program within a larger program.
Explain the benefits of using sub-programs when constructing a larger program. [3]
© International Baccalaureate Organization, 2025