Programming – A Level Computer Science (old syllabus, to 2021) (9608)
130 questions · page 3 of 9
Question text
6
A company hires out rowing boats on a lake. The company has ten boats, numbered from 1 to 10.
The company is developing a program to help manage and record the hiring out process.
Hire information is stored in three global 1D arrays when a boat is hired out. Each array contains
10 elements representing each of the ten boats.
The three 1D arrays are summarised as follows:
Array
Data type
Description
Example data value
HireTime
STRING
"10:15"
The time the boat was hired out
Duration
INTEGER
30
The number of minutes of the hire
Cost
REAL
5.75
The cost of the hire
If an individual boat is not currently on hire, the corresponding element of the HireTime array will
be set to "Available".
The programmer has started to define program modules as follows:
Module
Description
•
Called with two parameters:
o a STRING value representing a time
AddTime()
o an INTEGER value representing a duration in minutes
•
Adds the duration to the time to give a new time
•
Returns the new time as a STRING
•
Called with a STRING value representing the time the
hire will start
•
Outputs the boat numbers that will be available for hire at
the given start time. A boat will be available for hire if it is
either:
ListAvailable()
o currently not on hire, or
o due back before the given hire start time
•
Outputs the number of each boat available
•
Outputs the total number of boats available or a suitable
message if there are none
•
Called with four parameters:
o an INTEGER value representing the boat number
o a STRING value representing the hire start time
o an INTEGER value representing the hire duration in
minutes
o a REAL value representing the cost of hire
RecordHire()
•
Updates the appropriate element in each array
•
Adds the cost of hire to the global variable DailyTakings
•
Converts the four input parameters to strings,
concatenated using commas as separators, and writes
the resulting string to the end of the existing text file
HireLog.txt
(a) Write pseudocode for the module ListAvailable().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
2
(a) A structure chart is often produced as part of a modular program design. The chart shows the
hierarchy of modules and the sequence of execution.
Give two other features the structure chart can show.
Feature 1 …
…
Feature 2 …
…
[2]
(b) Six program modules implement part of an online shopping program. The following table
gives the modules and a brief description of each module:
Module
Description
Allows the user to choose a delivery slot, select items to be added to
Shop()
the basket and finally check out
ChooseSlot()
Allows the user to select a delivery time. Returns a delivery slot number
FillBasket()
Allows the user to select items and add them to the basket
Completes the order by allowing the user to pay for the items. Returns
Checkout()
a Boolean value to indicate whether or not payment was successful
Search()
Allows the user to search for a specific item. Returns an item reference
Adds an item to the basket. Takes an item reference and a quantity as
Add()
parameters
(i)
The online shopping program has been split into sub-tasks as part of the design process.
Explain the advantages of decomposing the program into modules. Your explanation
should refer to the scenario and modules described in part (b).
…
…
…
…
…
… [3]
(ii)
Complete the structure chart for the six modules described in part (b).
Shop()
[6]
Question text
3
A navigation program includes a function, CheckCourse(). This function is called with a real
value, Course, and returns an integer value.
The identifier table and the program flowchart for the function are shown as follows:
Identifier
Type
Description
Course
REAL
The value passed to CheckCourse()
Adjust
INTEGER
The value returned by CheckCourse()
Check
INTEGER
A local variable
A function that is passed a REAL value representing the
Deviate()
FUNCTION
course and returns a REAL value representing the current
deviation
Alert()
PROCEDURE
A procedure that generates a warning
START
Set Check to integer
value of
Deviate(Course)
Set Adjust to 255
CASE OF
Check
–20 to –1
Set Adjust to 10
OTHERWISE
0
Set Adjust to 0
Alert()
1 to 20
Set Adjust to –10
RETURN Adjust
END
Write pseudocode to implement the function CheckCourse(). The pseudocode must follow the
algorithm represented by the flowchart. Declare any local variables used.
Refer to the Appendix on page 19 for a list of built-in pseudocode functions and operators.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
4
(a) Mustafa has developed the following pseudocode to generate ten random integers in the
range 1 to 100.
DECLARE Random : ARRAY [1:10] OF INTEGER
DECLARE Count, RNum : INTEGER
1 TO 10
FOR Count
INT(RAND(100)) + 1
Rnum
RNum
Random[Count]
ENDFOR
Refer to the Appendix on page 19 for a list of built-in pseudocode functions and operators.
Rewrite the pseudocode so that there are no duplicated numbers in the list of random
numbers.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
(b) The changes made to the pseudocode in part (a) were as a result of changes to the program
requirement.
Give the term used to describe changes made for this reason.
…
Question text
5
A global 1D array, Contact, of type STRING is used to store a list of names and email addresses.
There are 1000 elements in the array. Each element stores one data item. The format of each
data item is as follows:
<Name>':'<EmailAddress>
Name and EmailAddress are both variable-length strings.
For example:
"Sharma Himal:[email protected]"
A function, GetName(), is part of the program that processes the array. A data item string will be
passed to the function as a parameter. The function will return the Name part. Validation is not
necessary.
(a) Use structured English to describe the algorithm for the function GetName().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [7]
(b) (i)
The array is to be sorted using an efficient bubble sort algorithm. An efficient bubble sort
reduces the number of unnecessary comparisons between elements.
Describe how this could be achieved.
…
…
…
…
…
Question text
6
A company hires out rowing boats on a lake. The company has 17 boats numbered from 1 to 17.
Boats may be hired between 9:00 and 18:00, with a maximum hire duration of 90 minutes.
The company is developing a program to help manage and record the boat hire process.
The programmer has decided to store all values relating to hire time as strings. The program will
use a 24-hour clock format. For example:
Time (in words)
String value
"09:00"
Nine o’clock in the morning
"10:05"
Five minutes past ten o’clock in the morning
"14:50"
Ten minutes before three o’clock in the afternoon
The programmer has defined the first module as follows:
Module
Description
•
Takes two parameters:
StartTime: a STRING value representing a time as described
AddTime()
Duration: an INTEGER value representing a duration in minutes
•
Adds the duration to the time to give a new time
•
Returns the new time as a STRING
(a) (i)
Write pseudocode for the module AddTime(). Assume both input parameters are valid.
Refer to the Appendix on page 19 for a built-in list of pseudocode functions and
operators.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
2
(a) A structure chart is often produced as part of a modular program design. The chart shows the
relationship between modules and the parameters that are passed between them.
Give two other features the structure chart can show.
Feature 1 …
…
Feature 2 …
…
[2]
(b) The following structure chart shows the relationship between three modules.
ModuleA()
ParW
ParX
ParZ
ModuleB()
ModuleC()
Parameter data types are:
ParW : REAL
ParX : INTEGER
ParZ : STRING
(i)
Write the pseudocode header for module ModuleB().
…
…
… [3]
(ii)
Write the pseudocode header for module ModuleC().
…
…
… [3]
(c) A student is developing an algorithm to count the number of times a given string,
SearchString, appears in a 2D array. The array, Item, consists of 100 elements organised
as 50 rows of 2 columns. SearchString could appear in any row or column.
The array is declared in pseudocode as follows:
DECLARE Item : ARRAY [1:50, 1:2] OF STRING
The structured English description of the algorithm is:
1.
SET Count to 0.
2.
Examine the first row of the array.
3.
IF column 1 element value is equal to SearchString, ADD 1 to Count.
4.
IF column 2 element value is equal to SearchString, ADD 1 to Count.
5.
REPEAT from step 3 for next row, UNTIL row is last row.
6.
OUTPUT a suitable message and Count.
Write pseudocode for the algorithm.
…
…
…
…
…
…
…
…
…
Question text
3
(a) Study the following pseudocode.
Delta(Plan, Actual)
Error
CASE OF Error
Steer − 10
> 10 : Steer
ZCount + 1
0 : ZCount
Steer + 10
< -10 : Steer
OTHERWISE OUTPUT "Unexpected Error"
ENDCASE
Draw a program flowchart to represent the pseudocode. Variable declarations are not
required in program flowcharts.
[5]
(b) The following pseudocode algorithm has been developed to check whether a string contains
a valid password.
To be a valid password, a string must:
•
be longer than five characters
•
contain at least one numeric digit
•
contain at least one upper case letter
•
contain at least one other character (not a numeric digit or an upper case letter).
10 FUNCTION Check(InString : STRING) RETURNS BOOLEAN
11
12
DECLARE Index : INTEGER
13
DECLARE StrLen : INTEGER
14
DECLARE NumUpper, NumDigit : INTEGER
15
DECLARE NextChar : CHAR
16
DECLARE NumOther : INTEGER
17
0
18
NumUpper
0
19
NumDigit
20
LENGTH(InString)
21
StrLen
22
IF StrLen < 6
23
THEN
24
RETURN FALSE
25
ELSE
1 TO StrLen - 1
26
FOR Index
27
// loop for each character
MID(InString, Index, 1)
28
NextChar
29
IF NextChar >= '0' AND NextChar <= '9'
30
THEN
NumDigit + 1 // count digits
31
NumDigit
32
ELSE
33
IF NextChar >= 'A' AND NextChar <= 'Z'
34
THEN
NumUpper + 1 // count upper case
35
NumUpper
36
ENDIF
37
ENDIF
38
ENDFOR
39
ENDIF
40
StrLen – (NumDigit – NumUpper)
41
NumOther
42
IF NumDigit >= 1 AND NumUpper >= 1 AND NumOther >= 1
43
THEN
44
RETURN TRUE
45
ELSE
46
RETURN FALSE
47
ENDIF
48
49 ENDFUNCTION
The pseudocode does not work under all circumstances.
The function was dry run with the string "1234AP" and the following trace table was
produced. The string is an invalid password, but the pseudocode returned the value
TRUE.
Trace
StrLen
Index
NextChar
NumUpper
NumDigit
NumOther
table row
6
0
0
1
1
'1'
2
1
3
2
'2'
4
2
5
3
'3'
6
3
7
4
'4'
8
4
9
5
'A'
10
1
11
3
12
(i)
The pseudocode algorithm contains two errors.
State how the given trace table indicates the existence of each error.
Error 1 …
…
…
…
Error 2 …
…
…
…
[2]
(ii)
Give the line number of each error in the pseudocode algorithm and write the modified
pseudocode to correct each error.
Line number for error 1 …
Correct pseudocode …
…
Line number for error 2 …
Correct pseudocode …
…
Question text
4
A global 1D array, Contact, of type STRING is used to store a list of names and email addresses.
There are 1000 elements in the array. Each element stores one data item. The format of each
data item is as follows:
<Name>':'<EmailAddress>
Name and EmailAddress are both variable-length strings.
For example:
"Wan Zhu:[email protected]"
A function, Extract(), is part of the program that processes the array. A string data item is
passed to the function as a parameter. The function will return the Name part. Validation is not
necessary.
(a) Write program code for the function Extract().
Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
(b) The original function, Extract(), needs to be modified to separate the name from the email
address. The calling program can then use both of these values.
Write, in pseudocode, the header for the modified subroutine. Explain the changes you have
made.
Subroutine header …
Question text
5
A company hires out rowing boats on a lake. The company has 20 boats, numbered from 1 to 20.
For safety reasons, the boats have to be serviced (checked and any damage repaired) regularly.
The company is developing a program to help manage the servicing of the boats.
Every time a boat is serviced, details are added at the end of the text file, ServiceLog.txt, as a
single line of information. Each boat is serviced before it is hired out for the first time.
The format of each line is as follows:
<BoatNumber><Date>
BoatNumber and Date are as follows:
•
BoatNumber is a two-digit numeric string in the range "01" to "20"
•
Date is an 8-digit numeric string in the format YYYYMMDD
The programmer has defined the first module as follows:
Module
Description
•
Called with a string parameter representing the BoatNumber
•
GetLastService()
Searches through the file ServiceLog.txt
•
Returns the date of the last service in the form "YYYYMMDD"
(a) Write pseudocode for the module GetLastService().
Refer to the Appendix on page 19 for a list of built-in pseudocode functions and operators.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
Study the following pseudocode.
FUNCTION Search() RETURNS INTEGER
DECLARE N, C : INTEGER
DECLARE V, L : REAL
GetLevel()
V
V * 1.34
L
0
C
1 TO 10
FOR N
GetLevel()
V
IF V > L
THEN
C + 1
C
ENDIF
ENDFOR
OUTPUT "Process complete"
RETURN C
ENDFUNCTION
(a) (i)
This pseudocode lacks features that would make it easier to read and understand.
State three such features.
Feature 1 …
Feature 2 …
Feature 3 …
[3]
(ii)
Draw a program flowchart to represent the algorithm implemented in the pseudocode.
Variable declarations are not required in program flowcharts.
[5]
(b) (i)
Programming languages support different data types.
Complete the table by giving a suitable data type for each example value.
Example value
Data type
"NOT TRUE"
− 4.5
NOT FALSE
132
[4]
(ii)
Evaluate each expression in the following table.
If an expression is invalid then write ‘ERROR’.
Refer to the Appendix on page 16–17 for the list of built-in functions and operators.
Expression
Evaluates to
LEFT("Start", 3) & RIGHT("Apple", 3)
MID("sample", 3, 5)
NUM_TO_STRING(12.3 * 2)
INT(STRING_TO_NUM("53.4")) + 7
[4]
Question text
3
The following is a function design in pseudocode.
Line numbers are given for reference only.
10 FUNCTION Check(InString : STRING) RETURNS BOOLEAN
11
12
DECLARE NumDots : INTEGER
13
DECLARE Index : INTEGER
14
DECLARE NumOthers : INTEGER
15
0
16
NumDots
0
17
NumOthers
1
18
Index
19
20
WHILE NumDots < 3 AND Index <= LENGTH(InString)
21
22
IF MID(InString, Index, 1) = '.'
23
THEN
NumDots + 1
24
NumDots
25
ELSE
NumOthers + 1
26
NumOthers
27
ENDIF
Index + 1
28
Index
29
30
ENDWHILE
31
32
IF NumDots = NumOthers
33
THEN
34
RETURN TRUE
35
ELSE
36
RETURN FALSE
37
ENDIF
38
39 ENDFUNCTION
Study the pseudocode. Identify the relevant features in the following table.
Refer to the Appendix on pages 16–17 for the list of built-in functions and operators.
Feature
Answer
The number of the line containing a variable being incremented
The range of line numbers containing a pre-condition loop
The number of initialisation statements
The number of the line containing a logical operator
The range of line numbers containing a selection statement
The name of a built-in function
The name of a parameter
[7]
Question text
4
A student is developing a program to count how many times each character of the alphabet (A to Z)
occurs in a given string. Upper case and lower case characters will be counted as the same. The
string may contain non-alphabetic characters, which should be ignored.
The program will:
•
check each character in the string to count how many times each alphabetic character occurs
•
store the count for each alphabetic character in a 1D array
•
output each count together with the corresponding character.
(a) The student has written a structured English description of the algorithm:
1.
START at the beginning of the string
2.
SELECT a character from the string
3.
CONVERT the character to upper case
4.
CHECK whether the character is alphabetic and INCREMENT as required.
5.
REPEAT from step 2 until last character has been checked
6.
OUTPUT a suitable message giving the count of each alphabetic character
Step 4 above is not described in sufficient detail.
The student decides to apply a process to increase the level of detail given in step 4.
State the name of the process and use this process to write step 4 in more detail. Use
structured English for your answer.
Process …
Structured English …
…
…
…
…
…
[4]
(b) Write pseudocode to implement the program.
You should note the following:
•
InString contains the string to be checked. It has been assigned a value.
•
The elements of the array Result have all been initialised to zero.
•
The ASCII value of letter ‘A’ is 65.
You should assume the following lines of pseudocode have been written:
DECLARE InString : STRING
DECLARE Result : ARRAY [1:26] OF INTEGER
Declare any further variables you use. Do not implement the code as a subroutine.
Refer to the Appendix on pages 16–17 for the list of built-in functions and operators.
…
…
…
…
…
…
…
…
…
Question text
5
The following pseudocode checks whether a string is a valid password.
FUNCTION CheckPassword(InString : STRING) RETURNS BOOLEAN
DECLARE Index, Upper, Lower, Digit, Other : INTEGER
DECLARE NextChar : CHAR
Upper
0
Lower
0
Digit
0
Other
0
FOR Index
1 TO LENGTH(InString)
NextChar
MID(InString, Index, 1)
IF NextChar >= 'A' AND NextChar <= 'Z'
THEN
Upper
Upper + 1
ELSE
IF NextChar >= 'a' AND NextChar <= 'z'
THEN
Lower
Lower + 1
ELSE
IF NextChar >= '0' AND NextChar <= '9'
THEN
Digit
Digit + 1
ELSE
Other
Other + 1
ENDIF
ENDIF
ENDIF
ENDFOR
IF Upper > 1 AND Lower >= 5 AND (Digit - Other) > 0
THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION
(a) Describe the validation rules that are implemented by this pseudocode. Refer only to the
contents of the string and not to features of the pseudocode.
…
…
…
…
…
… [3]
(b) (i)
Complete the trace table by dry running the function when it is called as follows:
CheckPassword("Jim+Smith*99")
Result
Index
NextChar
Upper
Lower
Digit
Other
[5]
(ii)
State the value returned when the function is called using the expression shown. Justify
your answer.
Value …
Justification …
…
…
[2]
Question text
1
Study the following pseudocode.
PROCEDURE FillTank()
DECLARE Tries : INTEGER
DECLARE Full : BOOLEAN
1
Tries
ReadSensor("F1")
Full
IF NOT Full
THEN
WHILE NOT Full AND Tries < 4
CALL TopUp()
ReadSensor("F1")
Full
Tries + 1
Tries
ENDWHILE
IF Tries > 3
THEN
OUTPUT "Too many attempts"
ELSE
OUTPUT "Tank now full"
ENDIF
ELSE
OUTPUT "Already full"
ENDIF
ENDPROCEDURE
(a) (i)
The pseudocode includes features that make it easier to read and understand.
State three such features.
Feature 1 …
Feature 2 …
Feature 3 …
[3]
(ii)
Draw a program flowchart to represent the algorithm implemented in the pseudocode.
Variable declarations are not required in program flowcharts.
[5]
(b) (i)
Programming languages support different data types.
Complete the table by giving a suitable data type for each example value.
Example value
Data type
43
TRUE
− 273.16
"− 273.16"
[4]
(ii)
Evaluate each expression in the following table.
If an expression is invalid then write ‘ERROR’.
Refer to the Appendix on page 18 for the list of built-in functions and operators.
Expression
Evaluates to
RIGHT("Stop", 3) & LEFT("ich", 2)
MID(NUM_TO_STRING(2019), 3, 1)
INT(NUM_TO_STRING(-273.16))
INT(13/2)
[4]