Assessment Test Algorithms
Assessment Test Algorithms
(a) X = 1 + 5/2
(b) X = (25 – 4) / 3
(c) X = 5 + 7 MOD 2
2. (a) Given that a = 7 and b = 6, state in each case whether x will be set to True or False.
(b) Write the corrected algorithm using a WHILE… ENDWHILE loop instead of a FOR…
ENDFOR loop
4. Manjeri writes a program to draw circles in random colours, sizes and positions on the
screen. She uses a random number generator to generate a colour number which she
allocates to red, yellow, green or blue.
FOR i 1 TO 20
Xcoord RANDOM_INT(1, 400)
Ycoord RANDOM_INT (1, 300)
size RANDOM_INT (20, 70)
colourNumber RANDOM_INT (1, 4)
IF colourNumber = 1 THEN
colour “red”
ELSE IF colourNumber = 2 THEN
colour “yellow”
ELSE IF colourNumber = 3 THEN
colour “green”
ELSE
colour “blue”
ENDIF
brushColour colour
FillCircle(XCoord, YCoord, size)
ENDFOR
Delay 200 #delay a few seconds
Clear Screen
(b) State what is meant by selection and iteration, using examples from Manjeri’s
program.
Selection:
Iteration:
(c) An array colourArray could be used instead of the IF statement to assign colours
to the variable colour. The index of the array starts at 0.
(iii) Write a statement to assign a colour to the variable colour using the array.
(d) Give two reasons why it is important to use meaningful identifier names in
a program.
(e) Add statements to the program to make the routine repeat indefinitely.
Write one or more statements which will call the subroutine in order to check
whether the measurement is between 18 and 25, and output the result.
(b) Give three reasons why programmers use subroutines.
The following pseudocode algorithm is being written to validate the user input.
THEN
(a) Fill in the trace table below if the subroutines is called with the parameters 36, 30,
and state what value is returned to the calling program.
x y x≠y
36 30 True
8. The following is an incomplete algorithm for checking that a user has entered the
correct password. The password entered is to be checked against the password
“ABCdef123”
The user is given 3 attempts to get the password correct before being logged out.
passwordOK False
count 1
password input("Please enter password: ")
WHILE count < 3 and passwordOK = False
9. Emma is writing a program to encrypt text entered by a user. Only letters, spaces and
punctuation marks can be encrypted. Each letter in the text is replaced by the letter
three places further down the alphabet. A is replaced by D, b by e, … Y by B, z by c.
Spaces and punctuation marks are not changed.
Complete the test plan below by stating, for each input data, the expected outcome and
a reason for the test.
Expected
Input data Reason for test
outcome
HEY
XYZ123
I’m Zac
10. The pseudocode below shows an algorithm that is supposed to calculate the average of
a set of scores held in the array scores. The program contains two logic errors.
1. totalScore 0
2. score [25, 17, -5, 28, 30]
3. FOR n 0 TO LEN (score)
4. totalScore totalScore + score (n)
5. ENDFOR
6. averageScore totalScore / LEN (score) – 1
Identify the two lines with logic errors in the above pseudocode, and write correct
versions of each so that the program will work as intended.
11. The following subroutine calculates a rail fare, based on the distance travelled. A
discount of 10% is given for off-peak travel.
12. Indicate whether each of the following statements are true or false.
True or False?
[Total 68 marks]