0% found this document useful (0 votes)
82 views14 pages

Pseudocode

The subroutine returns the sum of the lengths of the two input arrays xs and ys. It takes in two array parameters xs and ys, initializes a local variable result to 0, uses two FOR loops to iterate through each array and increment result by 1 for each element, and returns the final value of result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views14 pages

Pseudocode

The subroutine returns the sum of the lengths of the two input arrays xs and ys. It takes in two array parameters xs and ys, initializes a local variable result to 0, uses two FOR loops to iterate through each array and increment result by 1 for each element, and returns the final value of result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 14

Q1.

The code below contains a subroutine that returns a value.

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

With the line:


WHILE a < (a + b)

Explain why this change is a mistake.

___________________________________________________________________

___________________________________________________________________

___________________________________________________________________

___________________________________________________________________
(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).

The first line has been completed for you.

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".

strIn isPalindrome iUp iDown


abcaba

(6)
(Total 8 marks)

Q4.

An algorithm is shown below:

SUBROUTINE subr(xs, ys)


result ← 0
FOR i ← 1 TO LEN(xs)
result ← result + 1
ENDFOR
FOR i ← 1 TO LEN(ys)
result ← result + 1
ENDFOR
RETURN result
ENDSUBROUTINE

(a) Describe this algorithm in terms of its inputs and output.

___________________________________________________________________

___________________________________________________________________

___________________________________________________________________

___________________________________________________________________

___________________________________________________________________

___________________________________________________________________
(3)

(b) What is the purpose of this algorithm?

___________________________________________________________________

___________________________________________________________________

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.

SUBROUTINE subr(xs, ys)


LEN(xs)+ LEN(ys)
ENDSUBROUTINE

State the mistake that the developer has made.

___________________________________________________________________

___________________________________________________________________
(1)

(e) Explain why the algorithm for answer part (d) is more efficient than the original
algorithm.

___________________________________________________________________

___________________________________________________________________

___________________________________________________________________

___________________________________________________________________
(2)
(Total 9 marks)

Q5.

The algorithm in the figure below simulates the game of rock–paper–scissors.

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.

1 options ← [‘paper’, ‘rock’, ‘scissors’]


2 player1 ← USERINPUT
3 player2 ← USERINPUT
4 player1HasWon ← false

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

IF player1 ≠ player2 THEN


IF player1 = 1 AND player2 = 2
THEN

(1)

(b) What data structure has been used for options?

___________________________________________________________________

___________________________________________________________________
(1)

(c) Tick the programming technique that has not been used in this algorithm.

Programming technique Tick one box

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.

The pseudocode in Figure 1 represents a procedure called Mult.

Figure 1

PROCEDURE Mult(n, m)
x ← 1
WHILE x ≤ m
OUTPUT n * x
x ← x + 1
ENDWHILE
ENDPROCEDURE

The pseudocode in Figure 2 represents a procedure called Display.

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)

(c) Tick the two correct statements.

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.

(a) 3 marks for AO2 (apply)

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

(b) 2 mark for AO2 (apply)

1 mark for each correct answer to a maximum of two.

The value of c is constant / does not change;


(a+b) / a / b may change;
There is a logical error;
2

(c) Mark is for AO2 (apply)


0;
1
[6]

Q2.

(a) 4 marks for AO2 (apply)


Mark as follows:
1 mark for num2 changing to 24;
1 mark for num2 changing to 9;
1 mark for num1 changing to 6;
1 mark for both num1 and num2 ending as 3;

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

(b) Mark is for AO1 (understanding)


4;
1

(c) Mark is for AO1 (understanding)


3;
1

(d) Mark is for AO1 (understanding)


4;
1

(e) 2 marks for AO1 (understanding)


1 mark for the benefit of using a subroutine;
1 mark for the explanation that matches the benefit;

Examples include:

It allows code reuse;


because the code can be called in different places in the code without being
written out in full;

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.

(a) (i) 'o' A. without quote marks.


1

(ii) 'd' A. without quote marks.


1

(b) The complete and correct trace table is:

strIn isPalindrom iUp iDown

Page 10 of 14
e

abcaba true 1 6

2 5

false 3 4

4 3

1 mark for isPalindrome first value true;


1 mark for isPalindrome last value false;
1 mark for iUp starting at 1;
1 mark for iUp incrementing by 1 and ending at 4;
1 mark for iDown starting at 6;
1 mark for iDown decrementing by 1 and ending at 3.
6
[8]

Q4.

(a) Marks are for AO2 (apply)

The algorithm takes two inputs (xs and ys);


The inputs are both arrays / strings / any iterable data structure;
The output is the variable result;
3

(b) Marks are for AO2 (apply)

(The algorithm) adds together / sums;


the length / size / number of elements of both arrays / inputs;
2

(c) Mark is for AO1 (recall)

A variable only accessible / visible within the subroutine;


A. alternative wording
1

(d) Marks are for AO3 (design)

They have left out the RETURN keyword;


A. corrected line of code
RETURN LEN(xs) + LEN(ys)
1

(e) Marks are for AO2 (apply)

(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;

R. it uses less lines of code or similar


2
[9]

Page 11 of 14
Q5.

(a) Third box only;

Tick one
Line of code
box
IF player1 = 1 OR player2 = 2 THEN

IF player1 ≠ player2 THEN


IF player1 = 1 AND player2 = 2
THEN ✔
1

(b) Array // list;

A. correct programming language-specific data structure


R. String
1

(c) Correct row only;

Tick one
Programming technique
box

Iteration ✔

Selection

Variable assignment

R. more than one row ticked


1

(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:

A. 1 mark for using selection;


B. 1 mark for a Boolean condition that tests for equivalency of the
variables player1 and player2;
C. 1 mark for assigning the value true to the variable draw;

Example 1 (italicised square brackets indicate where marks are awarded):


IF player1 = player2 THEN [A][B]
draw true [C]
ENDIF

Example 2 where the inverse is tested in a Boolean condition (italicised


square brackets indicate where marks are awarded):
draw true [C]
IF player1 ≠ player2 THEN [A][B]
draw false [also needed for C]
ENDIF

Example 3 (notes indicate where marks are awarded)

Page 12 of 14
3
[6]

Q6.

(a)

Most suitable
Tick one box
data type of n

String

Boolean

Integer ✔
1

(b) A function returns a value (whereas a procedure does not);


1

(c) 1 mark for each correct row;

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) 1 mark for the first value of 1;

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)

The completed correct trace table is:

x Output

1 2

2 4

3 6

4
4

(e) 1 mark for starting at 3;


1 mark for outputting 6 and nothing further (except 3);
2
[10]

Page 14 of 14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy