Pseudocode
Pseudocode
SUBROUTINE TotalOut(a, b)
c ← a + b
WHILE a < c
a ← a + 1
b ← b – a
ENDWHILE
RETURN b
ENDSUBROUTINE
(a) Complete the trace table below when the subroutine call TotalOut(3, 4) is made
(you may not need to use all of the rows in the table):
a b c
(3)
(b) A programmer mistakenly tries to shorten the subroutine above by replacing the
lines:
c ← a + b
WHILE a < c
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(c) What value is returned by the subroutine call TotalOut(x, 0) where x is any
positive integer?
___________________________________________________________________
___________________________________________________________________
(1)
Page 1 of 14
(Total 6 marks)
Q2.
The figure below shows a famous algorithm that calculates the largest whole number that
can be divided into two given numbers without leaving a remainder. For example, given
the two numbers 12 and 16, 4 is the largest whole number that divides into them both.
Line numbers have been included but are not part of the
algorithm.
1 num1 USERINPUT
2 num2 USERINPUT
3 WHILE num1 ≠ num2
4 IF num1 > num2 THEN
5 num1 num1 − num2
6 ELSE
7 num2 num2 − num1
ENDIF
8
ENDWHILE
9
(a) Complete the trace table for the algorithm in the figure above when the user enters
15 and then 39 (you may not need to use all of the rows in the table).
num1 num2
15 39
(4)
(b) State the line number from the algorithm in the figure above where selection is first
used.
___________________________________________________________________
___________________________________________________________________
(1)
(c) State the line number from the algorithm in the figure above where iteration is first
used.
___________________________________________________________________
___________________________________________________________________
(1)
(d) How many lines in the algorithm in the figure above use variable assignment?
___________________________________________________________________
Page 2 of 14
___________________________________________________________________
(1)
(e) A programmer wants to implement the algorithm in the figure above in their code.
Explain why it could be a benefit to implement this as a subroutine.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(Total 9 marks)
Q3.
A built-in function commonly found in programming languages is one that finds the
character in a string at a specific position. In some programming languages this function is
called CharAt.
CharAt(str, i) returns the character found at position i of the string str.
For example,
CharAt("abc", 1) returns 'a'
CharAt("abc", 3) returns 'c'
(a) (i) What value will be returned by the function call CharAt("hello", 5)?
______________________________________________________________
______________________________________________________________
(1)
(ii) What value will be returned by the function call CharAt("goodbye", (1+3))?
______________________________________________________________
______________________________________________________________
(1)
(b) A palindrome is a string that is the same read forwards or backwards. For example,
"abba" and "abcba" are both palindromes but "abcbb" is not.
The following algorithm uses the function CharAt to check if a string is a palindrome.
This algorithm also uses the LEN function. LEN returns the length of a string, for
example LEN("cpu") returns 3.
Note: line numbers have been shown but are not part of the algorithm.
1 strIn ← USERINPUT
2 isPalindrome ← true
3 iUp ← 1
4 iDown ← LEN(strIn)
5 WHILE iUp < iDown
6 IF CharAt(strIn, iUp) ≠ CharAt(strIn, iDown) THEN
Page 3 of 14
7 isPalindrome ← false
8 ENDIF
9 iUp ← iUp + 1
10 iDown ← iDown – 1
11 ENDWHILE
Complete the trace table for this algorithm when the user input is "abcaba".
(6)
(Total 8 marks)
Q4.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(3)
___________________________________________________________________
___________________________________________________________________
Page 4 of 14
___________________________________________________________________
___________________________________________________________________
(2)
(c) result is a local variable within this algorithm. What is a local variable in a
subroutine?
___________________________________________________________________
___________________________________________________________________
(1)
(d) Another developer looks at this algorithm and states that they could implement the
whole algorithm in one line between the SUBROUTINE and ENDSUBROUTINE lines,
without using any loops.
___________________________________________________________________
___________________________________________________________________
(1)
(e) Explain why the algorithm for answer part (d) is more efficient than the original
algorithm.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(Total 9 marks)
Q5.
Player 1 enters the number 1 for paper, the number 2 for rock or the number 3 for
scissors. Player 2 then does the same.
You should assume that the numbers entered by the players are stored as integers.
Note: line numbers have been included but are not part of the algorithm.
Page 5 of 14
5 draw ← false
6 IF player1 = 1 THEN
7 IF player2 = 2 THEN
8 player1HasWon ← true
9 ENDIF
10 ENDIF
11 IF player1 = 2 THEN
12 IF player2 = 3 THEN
13 player1HasWon ← true
14 ENDIF
15 ENDIF
16 IF player1 = 3 THEN
17 IF player2 = 1 THEN
18 player1HasWon ← true
19 ENDIF
20 ENDIF
(a) Tick the line of code that is equivalent to lines 6 and 7 together.
Tick one
Line of code
box
IF player1 = 1 OR player2 = 2 THEN
(1)
___________________________________________________________________
___________________________________________________________________
(1)
(c) Tick the programming technique that has not been used in this algorithm.
Iteration
Selection
Variable assignment
(1)
(d) Using either pseudocode or a flowchart, extend the algorithm in the figure above so
that the variable draw is set to the value true when both player 1 and player 2
choose the same option. This code should follow on from the end of the algorithm in
the figure above.
___________________________________________________________________
___________________________________________________________________
Page 6 of 14
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(3)
(Total 6 marks)
Q6.
Figure 1
PROCEDURE Mult(n, m)
x ← 1
WHILE x ≤ m
OUTPUT n * x
x ← x + 1
ENDWHILE
ENDPROCEDURE
Figure 2
PROCEDURE Display(a, b)
IF b > 3 THEN
Mult(a, 3)
ELSE
Mult(a, b)
ENDIF
ENDPROCEDURE
(a) Select the most suitable data type for the parameter n in the procedure Mult.
(tick one box only)
Tick one
Most suitable data type of n
box
String
Boolean
Integer
(1)
Page 7 of 14
(b) Explain one difference between a procedure and a function.
___________________________________________________________________
___________________________________________________________________
(1)
Tick two
Statement
boxes
Display(2, 6) and Display(2, 3)
will both have the same output.
Display(2, 6) and Display(6, 2)
will both have the same output.
Display(2, −1) will not output
anything.
Display(−2, 1) will output two
different values.
(2)
(d) Complete the trace table below showing the changes in the variable x and the
output for the procedure call Mult(2,3).
x Output
(4)
(e) What is the output from the procedure call Display(3, (3−1))?
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(Total 10 marks)
Page 8 of 14
Mark schemes
Q1.
Mark as follows:
1 mark for all correct values in the correct order for column a;
1 mark for all correct values in the correct order for column b;
1 mark for column c having only the value 7;
The completed trace table should have these values although the candidate
may have entered the values on different rows (do not penalise as long as the
order of the values is correct).
a b c
3 4 7
4 0
5 −5
6 −11
7 −18
Q2.
The completed trace table is shown here (candidates’ values may appear on
different rows):
Page 9 of 14
num1 num2
15 39
24
3
4
Examples include:
It is easier to test;
because it can be tested in isolation from the rest of the code;
It is easier to maintain;
because as long as the interface is maintained it can be safely changed or
rewritten;
2
[9]
Q3.
Page 10 of 14
e
abcaba true 1 6
2 5
false 3 4
4 3
Q4.
(The algorithm in answer part (d)) does not need to iterate / loop over the
arrays / LEN could run in constant time / Only one addition is needed;
so it will run in less time;
Page 11 of 14
Q5.
Tick one
Line of code
box
IF player1 = 1 OR player2 = 2 THEN
Tick one
Programming technique
box
Iteration ✔
Selection
Variable assignment
(d) Marks awarded as follows (allow any logically equivalent and correct answer).
The marks are labelled A – C and shown in the examples where they are
awarded:
Page 12 of 14
3
[6]
Q6.
(a)
Most suitable
Tick one box
data type of n
String
Boolean
Integer ✔
1
Tick two
Statement
boxes
Display(2, 6) and
Display(2, 3) will both have ✔
the same output.
Display(2, 6) and
Display(6, 2) will both have
the same output.
Display(2, −1) will not output
anything. ✔
Page 13 of 14
1 mark for incrementing each row by 1;
1 mark for the last value of 4;
1 mark for outputs being twice the value of x if at least two values of x are
given (I. if x is incorrect)
x Output
1 2
2 4
3 6
4
4
Page 14 of 14