Programming – A Level Computer Science (old syllabus, to 2021) (9608)
130 questions · page 4 of 9
Question text
3
A student is developing a program to search through a string of numeric digits to count how many
times each digit occurs. The variable InString will store the string and the 1D array Result will
store the count values.
The program will:
•
check each character in the string to count how many times each digit occurs
•
record the count for each digit using the array
•
output the count for each element of the array together with the corresponding digit.
(a) The array Result is a 1D array of type INTEGER.
Write pseudocode to declare the array and to initialise all elements to zero.
…
…
…
…
…
… [3]
(b) Write the pseudocode for the program.
Declare any variables you use. Do not implement the code as a subroutine.
Refer to the Appendix on page 18 for the list of built-in functions and operators.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
4
A program is being written to control the operation of a portable music player. One part of the
program controls the output volume.
The player has two buttons, one to increase the volume and one to decrease it. Whenever a
button is pressed, a procedure Button() is called with a parameter value representing the button
as follows:
Button
Parameter value
Volume increase
10
Volume decrease
20
For example, pressing the volume increase button three times followed by pressing the volume
decrease button once would result in the calls:
CALL
Button(10)
//
VolLevel
increased
by
1
CALL
Button(10)
//
VolLevel
increased
by
1
CALL
Button(10)
//
VolLevel
increased
by
1
CALL
Button(20)
//
VolLevel
decreased
by
1
The program makes use of two global variables of type INTEGER as follows:
Variable
Description
The current volume setting. This must be in the range 0 to 49.
VolLevel
A value that can be set to limit the maximum value of VolLevel,
in order to protect the user’s hearing.
MaxVol
A value in the range 1 to 49 indicates the volume limit.
A value of zero indicates that no volume limit has been set.
The procedure Button() will modify the value of VolLevel depending on which button has
been pressed and whether a maximum value has been set.
(a) Write pseudocode for the procedure Button(). Declare any additional variables you use.
The value of MaxVol should not be changed within the procedure.
Parameter validation is not necessary.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
5
The module headers for three modules in a program are defined in pseudocode as follows:
Pseudocode module header
PROCEDURE Lookup(P4 : INTEGER, BYREF M4 : STRING)
FUNCTION Update(T4 : INTEGER) RETURNS INTEGER
FUNCTION Validate(S2 : INTEGER, P3 : STRING) RETURNS BOOLEAN
A fourth module, Renew(), calls the three modules in the following sequence.
Validate()
Lookup()
Update()
Draw a structure chart to show the relationship between the four modules and the parameters
passed between them.
[7]
Question 6 begins on the next page.
Question text
6
A text file, StudentList.txt, contains a list of information about students in a school.
Each line of the file contains a reference, name and date of birth for one student. All the information
is held as strings and separated by the asterisk character ('*') as follows:
<Reference>'*'<Name>'*'<Date Of Birth>
An example of one line from the file is:
"G1234*Aleza Hilton*05062001"
The reference string may be five or eight characters in length and is unique for each student. It is
made up of alphabetic and numeric characters only.
A global 1D array, Leavers, contains the references of all students who have recently left the
school. The array consists of 500 elements of data type STRING. Unused elements contain the
empty string "".
A program is to be written to produce a new text file, UpdatedList.txt, containing information
only for students who are still attending the school.
The program is to be implemented as several modules. The outline description of three of these is
as follows:
Module
Outline description
•
Read each line from the file StudentList.txt
Check whether the Reference appears in the
•
array using SearchLeavers()
ProcessStudentList()
If the Reference does not appear then write the
•
line to the file UpdatedList.txt
Return the number of lines not copied.
•
•
Search for a given Reference in the array Leavers
SearchLeavers()
•
If the Reference is found, return TRUE, otherwise
return FALSE
•
Take two parameters: the name of an array and a
string.
CountTimes()
•
Count the number of elements that are the same as
the string. Return the count value.
(a) Write program code for the module SearchLeavers(). Declare any additional variables
you use.
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
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
2
(a) (i)
Two types of loop that may be found in an algorithm are the ‘pre-condition’ and ‘post-
condition’ loop.
Identify one other type of loop. Explain when it should be used.
Type …
Explanation …
…
…
[2]
(ii)
Part of a program flowchart is shown.
LOOP
AlarmReset()
Set Status1 to
GetStatus(Sys_A)
Set Status2 to
GetStatus(Sys_B)
Are Status1
NO
and Status2
both TRUE?
YES
Implement the flowchart in pseudocode using a post-condition loop.
…
…
…
…
…
… [4]
(b) The following lines of code are taken from a high-level language program.
100
setvar(Count, Integer)
110
setvar(Gross[0-20], Real)
120
setvar(Posn, Real)
130
setvar(Length, Integer)
140
setvar(Rate, Real)
150
Length := 7
160
Rate := 1.175
170
180
For (Count, 0, 20, 2)
190
{
200
Echo "Input next cost"
210
Posn := Read()
220
Gross[Count] := Mult(Posn, Rate) %Apply current tax rate
230
}
Study the code. Identify the relevant features in the following table.
Feature
Answer
The symbol used to indicate an assignment
The line numbers for the start and end of a count-controlled loop
The step value of the count-controlled loop
The character that indicates a comment
The name of a function
[5]
(c) A program written in a high-level language cannot be run directly.
Identify one type of translator that can be used to translate the program.
… [1]
Question text
3
Three program modules process updating of passwords in a file. A description of the relationship
between the modules is summarised as follows:
Module name
Description
•
Takes two parameters: AccountID and OldPassword
GetPassword()
•
Returns a string containing the new password
•
Takes two parameters: AccountID and NewPassword
•
Returns a Boolean value to indicate whether or not the
UpdateFile()
update was successful
•
Calls GetPassword() to obtain the new password then
ChangePassword()
calls UpdateFile() to write the new password to the file
Draw a structure chart to show the relationship between the three modules and the parameters
passed between them.
[5]
Question text
4
The following pseudocode algorithm checks whether a string is a valid email address.
FUNCTION Check(InString : STRING) RETURNS BOOLEAN
DECLARE
Index : INTEGER
DECLARE
NumDots : INTEGER
DECLARE
NumAts : INTEGER
DECLARE
NextChar : CHAR
DECLARE
NumOthers : INTEGER
0
NumDots
0
NumAts
0
NumOthers
1 TO LENGTH(InString)
FOR Index
MID(InString, Index, 1)
NextChar
CASE OF NextChar
NumDots + 1
'.': NumDots
NumAts + 1
'@': NumAts
NumOthers + 1
OTHERWISE NumOthers
ENDCASE
ENDFOR
IF (NumDots >= 1 AND NumAts = 1 AND NumOthers > 5)
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:
Check("[email protected]")
Result
Index
NextChar
NumDots
NumAts
NumOthers
[5]
(ii)
State the value returned when function Check is called as shown in part (b)(i).
… [1]
(c) The function Check() is to be tested.
State two different invalid string values that could be used to test the algorithm. Each string
should test a different rule.
Justify your choices.
Value …
Justification …
…
…
Value …
Justification …
…
…
[4]
Question text
5
Abbreviations are often used in place of a full name. Concatenating the first letter of each word in
the name makes an abbreviation.
For example:
Name
Abbreviation
United Nations
UN
World Wide Web
WWW
British Computer Society
BCS
A function, Abbreviate(), will take a string representing the full name and return a string
containing the abbreviated form.
You should assume that:
•
names only contain alphabetic characters and space characters
•
names always start with an alphabetic character
•
each word in the name always starts with an uppercase character
•
only a single space separates words in the name.
Write pseudocode to implement the function Abbreviate().
Refer to the Appendix on page 16 for the list of built-in functions and operators.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
(a) (i)
Algorithms may be expressed using four basic constructs. One construct is sequence.
Complete the following table for two other constructs.
Construct
Pseudocode example
…
…
…
…
…
…
…
…
…
…
[4]
(ii)
Simple algorithms usually consist of input, process and output.
Complete the table by placing ticks (‘3’) in the relevant boxes.
Pseudocode statement
Input
Process
Output
SensorValue * Factor
Temp
WRITEFILE "LogFile.txt", TextLine
WRITEFILE "LogFile.txt", MyName & MyIDNumber
READFILE "AddressBook.txt", NextLine
[4]
(b) Program variables have values as follows:
Variable
Value
Title
"101 tricks with spaghetti"
Version
'C'
Author
"Eric Peapod"
PackSize
4
WeightEach
6.2
Paperback
TRUE
(i)
Evaluate each expression in the following table.
If an expression is invalid, write ERROR.
For the built-in functions list, refer to the Appendix on page 16.
Expression
Evaluates to
MID(Title, 5, 3) & RIGHT(Author, 3)
INT(WeightEach * PackSize)
PackSize >= 4 AND WeightEach < 6.2
LEFT(Author, ASC(Version) - 65)
RIGHT(Title, (LENGTH(Author) – 6))
[5]
(ii)
Programming languages support different data types.
Give an appropriate data type for the following variables from part (b).
Variable
Data type
Title
Version
PackSize
WeightEach
Paperback
[5]
(c) White-box and black-box are two types of testing. In white-box testing, data are chosen to
test every possible path through the program.
Explain how data are chosen in black-box testing.
…
… [2]
Question text
2
(a) One type of loop that may be found in an algorithm is a count-controlled loop.
State one other type and explain when it should be used.
Type …
Explanation …
…
…
[2]
(b) Chris is asked to work on a program that has been coded in a language he is not familiar
with.
He has identified that the program contains the constructs: sequence, iteration and selection.
Identify three other features of the program that he should expect to recognise.
Feature 1 …
Feature 2 …
Feature 3 …
[3]
(c) The following lines of code are taken from a program in a high-level language.
ON x {
15: Call ProcA
20: y := 0
25: y := 99
NONE: Call ProcError
}
Identify the type of control structure and describe the function of the code.
Control structure …
Description …
…
…
…
[3]
Question text
3
(a) A student is developing an algorithm to search through a 1D array of 100 elements. Each
element of the array, Result, contains a REAL value.
The algorithm will output:
•
the average value of all the elements
•
the number of elements with a value of zero.
The structured English description of the algorithm is:
1.
SET Total value to 0
2.
SET Zero count to 0
3.
SELECT the first element
4.
ADD value of element to Total value
5.
IF element value is 0 then INCREMENT Zero count
6.
REPEAT from step 4 for next element, until element is last element
7.
SET Average to Total / 100
8.
OUTPUT a suitable message and Average
9.
OUTPUT a suitable message and Zero count
Write pseudocode for this algorithm.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [7]
(b) The student decides to change the algorithm and implement it as a procedure, ScanArray(),
which will be called with three parameters.
ScanArray(AverageValue, ZeroCount, ArrayName)
ScanArray() will modify the first two parameters so that the new values are available to the
calling program or module.
Write the pseudocode procedure header for ScanArray().
…
…
…
…
Question text
4
The following pseudocode is a string handling function.
For the built-in functions list, refer to the Appendix on page 16.
FUNCTION Clean(InString : STRING) RETURNS STRING
DECLARE NewString : STRING
DECLARE Index : INTEGER
DECLARE AfterSpace : BOOLEAN
DECLARE NextChar : CHAR
CONSTANT Space = ' '
FALSE
AfterSpace
""
NewString
1 TO LENGTH(InString)
FOR Index
MID(InString, Index, 1)
NextChar
IF AfterSpace = TRUE
THEN
IF NextChar <> Space
THEN
NewString & NextChar
NewString
FALSE
AfterSpace
ENDIF
ELSE
NewString & NextChar
NewString
IF NextChar = Space
THEN
TRUE
AfterSpace
ENDIF
ENDIF
ENDFOR
RETURN NewString
ENDFUNCTION
(a) (i)
Complete the trace table by performing a dry run of the function when it is called as
follows:
Clean("X∇∇∇Y∇and∇∇Z")
Result
The symbol '∇' represents a space character. Use this symbol to represent a space
character in the trace table.
Index
AfterSpace
NextChar
NewString
[6]
(ii)
State the effect of the function Clean().
…
… [1]
(iii)
The pseudocode is changed so that the variable AfterSpace is initialised to TRUE.
Explain what will happen if the function is called as follows:
Clean("∇∇X∇∇∇Y∇and∇∇Z")
Result
…
…
…
… [2]
(b) The following pseudocode declares and initialises an array.
DECLARE Code : ARRAY[1:100] OF STRING
DECLARE Index : INTEGER
1 TO 100
FOR Index
""
Code[Index]
ENDFOR
The design of the program is changed as follows:
•
the array needs to be two dimensional, with 500 rows and 4 columns
•
the elements of the array need to be initialised to the string "Empty"
Re-write the pseudocode to implement the new design.
…
…
…
…
…
…
…
…
…
… [4]
(c) State the term used for changes that are made to a program in response to a specification
ch
Question text
5
(a) Programming languages usually contain a range of built-in functions, such as a random
number generator.
State three advantages of using built-in functions.
1 …
2 …
3 …
[3]
(b) A student is learning about random number generation.
She is investigating how many times the random function needs to be called before every
number in a given series is generated.
She is using pseudocode to develop a procedure, TestRand(), which will:
•
use the random number function to generate an integer value in the range 1 to 50
inclusive
•
count how many times the random function needs to be called before all 50 values have
been generated
•
output a message giving the number of times the random function was called.
Write pseudocode for the procedure TestRand().
For the built-in functions list, refer to the Appendix on page 16.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
6
A text file, MyCDs.txt, stores information relating to a Compact Disc (CD) collection. Information
about each CD is stored on three separate lines in the file as follows:
Line 1:
<Artist Name>
Line 2:
<CD Title>
Line 3:
<Storage Location>
Information is stored as data strings.
A section of the file is shown:
File line
Data
100
"Green Floyd"
101
"Bowlful of Cereal"
102
"Shelf 4"
103
"Strolling Bones"
104
"Exile on Station Road"
105
"Box 12"
(a) A program, CDOrganiser, will be written to manage the stored information. The program will
consist of three modules: AddCD, FindCD and RemoveCD.
Give three reasons why it is good practice to construct the program using modules.
1 …
2 …
3 …
[3]
(b) The module, FindCD(), will check whether a given CD exists in the collection. The module
will be implemented as a function.
The function will:
•
be called with two strings as parameters, representing the artist name and CD title
•
return a string that gives the storage location, or an empty string if the given CD has not
been found.
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of the University of
Write program code for the function FindCD().
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
…
…
…
…
…
…
…
…
…
…
Question text
1
(a) Algorithms are used in computer programming.
(i)
Explain the term algorithm.
…
…
…
… [2]
(ii)
Algorithms usually consist of three different types of activity.
Complete the table below.
The third activity type is given.
Activity type
Pseudocode example
…
…
…
…
…
…
…
Output
…
[5]
(b) Program variables have values as follows:
Variable
Value
Married
03/04/1982
ID
"M1234"
MiddleInitial
'J'
Height
5.6
IsMarried
TRUE
Children
2
(i)
Evaluate each expression in the following table.
If an expression is invalid, write ERROR.
For the built-in functions list, refer to the Appendix on page 16.
Expression
Evaluates to
STRING_TO_NUM(RIGHT(ID, 3))
INT(Height * Children)
IsMarried AND Married < 31/12/1999
LENGTH(ID & NUM_TO_STRING(Height))
MID(ID, INT(Height) – Children, 2)
[5]
(ii)
Programming languages support different data types.
Give an appropriate data type for the following variables from part (b).
Variable
Data type
Married
ID
MiddleInitial
Height
IsMarried
[5]