Programming – A Level Computer Science (old syllabus, to 2021) (9608)
130 questions · page 9 of 9 · Clear filters
Question text
3
A string encryption function is needed. The encryption uses a simple character-substitution method.
In this method, a new character substitutes for each character in the original string. This will create
the encrypted string.
The substitution uses the 7-bit ASCII value for each character. This value is used as an index for a
1D array, Lookup, which contains the substitute characters.
Lookup contains an entry for each of the ASCII characters. It may be assumed that the original
string and the substitute characters are all printable.
For example:
•
‘A’ has ASCII value 65
•
Array element with index 65 contains the character ‘Y’ (the substitute character)
•
Therefore, ‘Y’ substitutes for ‘A’
•
There is a different substitute character for every ASCII value
The programmer writes a function, EncryptString, to return the encrypted string. This function
will receive two parameters, the original, PlainText string and the 1D array.
(a) The first attempt at writing the pseudocode for this function is shown below.
Complete the pseudocode.
For the built-in functions list, refer to the Appendix on page 18.
FUNCTION EncryptString( … ) RETURNS STRING
DECLARE … , … : CHAR
DECLARE OldCharValue : …
DECLARE n : INTEGER
DECLARE OutString : STRING
… //initialise the return string
//loop through PlainText to produce OutString
FOR n
1 TO … //from first to last character
… //get next character
OldChar
… //find the ASCII value
OldCharValue
… //look up substitute character
NewChar
… //concatenate to OutString
ENDFOR
…
ENDFUNCTION
[10]
(b) Additional code needs to be written to allow the user to change some of the characters in the
array Lookup.
The user will input:
•
the array start position
•
the number of elements to change
•
each new substitute character
At the end, the program will finally output a confirmation message.
The first version of the algorithm is represented by the flowchart on the following page.
Write program code to declare the array Lookup.
(i)
Programming language …
…
…
… [2]
(ii)
Write program code to implement the flowchart design.
In addition to the Lookup array, assume that the following variables have been declared:
StartPos, NumToChange, n, NewChar
Programming language …
…
…
…
…
…
…
…
Question text
4
(a) Structured programming involves the breaking down of a problem into modules.
Give two reasons why this is done.
1 …
…
2 …
…
[2]
(b) A team needs to write a program to implement an online shopping system. Customers will
access the program via a website.
Customers can search for items before adding them to a virtual shopping basket. When they
have finished shopping, they pay for the items. The program provides output for the dispatch
of the items.
Some of the key features of the system are as follows:
•
a customer can add many items to the shopping basket
•
payment may be either by credit or debit card, or by adding to a customer account
•
the shop may dispatch the items in one or more packages
The structure chart below shows the program modules only.
(i)
Draw on the chart, the symbols that represent the key features listed in part (b) above.
Online shopping
Select Item
Checkout
Dispatch
Print
Print
Card
Account
Add to
Search
dispatch
address
payment
payment
basket
list
label
[3]
(ii)
A section of the chart in part (b)(i) is shown below. It is to show the parameters passed
between the Checkout and Card payment modules.
Checkout
A
B
C
Card
Account
payment
payment
Name the three data items corresponding to the arrows.
Arrow
Data item
A
B
C
[3]
Question 5 begins on page 13.
Question text
5
Toni has a large collection of jazz CDs that are stored in different places. She wants to record
where the CDs are stored. She decides to write a program to do this.
The program must store the data in a file, MyMusic.
(a) (i)
Why is a file needed?
…
… [1]
MyMusic is a text file with the data for each CD as one line of text.
(ii)
Data for a typical CD are:
Title:
Kind of Green
Artist:
Miles Coltrane
Location:
Rack1-5
The line will be formed by concatenating the three data items.
For the example above, the line stored will be:
Kind of GreenMiles ColtraneRack1-5
Describe a problem that might occur when organising the data in this way.
…
…
…
…
Describe a possible solution.
…
…
…
…
[4]
(b) Toni must input the data into the file for all of her CDs.
A procedure, InputData, is needed to do this.
Toni designs the procedure and chooses the following identifiers:
Identifier
Data type
CDTitle
STRING
CDArtist
STRING
CDLocation
STRING
The procedure repeatedly performs the following steps:
•
input a CD title (A rogue value of “##” is to be used to end the input)
•
input the artist
•
input the location
•
create the text line
•
write the text line to the file
When the rogue value is encountered the file is closed.
Write program code for the procedure InputData.
Visual Basic and Pascal: You should include declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.
Programming language …
…
…
…
…
…
…
…
…
Question text
6
A string-handling function has been developed. The pseudocode for this function is shown below.
For the built-in functions list, refer to the Appendix on page 18.
FUNCTION SSM(String1, String2 : STRING) RETURNS INTEGER
DECLARE n, f, x, y : INTEGER
0
n
0
f
REPEAT
n + 1
n
n
x
1
y
WHILE MID(String1, x, 1) = MID(String2, y, 1)
IF y = LENGTH(String2)
THEN
n
f
ELSE
x + 1
x
y + 1
y
ENDIF
ENDWHILE
UNTIL (n = LENGTH(String1)) OR (f <> 0)
RETURN f
ENDFUNCTION
(a) Complete the trace table below by performing a dry run of the function when it is called as
follows:
SSM("RETRACE", "RAC")
n
f
x
y
MID(String1, x, 1)
MID(String2, y, 1)
0
0
[6]
Describe the purpose of function SSM.
(b) (i)
…
…
…
… [2]
One of the possible return values from function SSM has a special meaning.
(ii)
State the value and its meaning.
Value …
Meaning …
[2]
(iii)
There is a problem with the logic of the pseudocode. This could generate a run-time
error.
Describe the problem.
…
…
…
… [2]
Appendix
Built-in functions
In each function below, if the function call is not properly formed, the function returns an error.
MID(ThisString : STRING, x : INTEGER, y : INTEGER) RETURNS STRING
returns the string of length y starting at position x from ThisString
Example: MID("ABCDEFGH", 2, 3) will return string "BCD"
LEFT(ThisString : STRING, x : INTEGER) RETURNS STRING
returns the leftmost x characters from ThisString
Example: LEFT("ABCDEFGH", 3) will return string "ABC"
RIGHT(ThisString: STRING, x : INTEGER) RETURNS STRING
returns the rightmost x characters from ThisString
Example: RIGHT("ABCDEFGH", 3) will return string "FGH"
ASC(ThisChar : CHAR) RETURNS INTEGER
returns the ASCII value of character ThisChar
Example: ASC('W') will return 87
LENGTH(ThisString : STRING) RETURNS INTEGER
returns the integer value representing the length of string ThisString
Example: LENGTH("Happy Days") will return 10
String operator
& operator
concatenates (joins) two strings
Example: "Summer" & " " & "Pudding" produces "Summer Pudding"
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every reasonable
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 International
Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after
the live examination series.
Cambridge International Examinations is part of the Cambridge Assessment Gr
Question text
1
The items in the table below are statements from a program in a generic programming language.
For the built-in functions list, refer to the Appendix on the last page.
(a) (i)
Show what type of programming construct each statement represents.
Complete the table by putting a tick (✓) in the appropriate column for each item.
Item
Statement
Selection
Iteration
Assignment
1
WHILE DegF > 37.5
2
MyName = "Gordon"
3
DegF = INT(DegF)
4
ENDIF
5
CASE OF MyFavourite
UNTIL x = 5
6
[6]
(ii)
State the purpose of each statement in the table in part (a)(i).
Do not use mathematical symbols in your descriptions.
Item
Purpose of statement
…
1
…
…
2
…
…
3
…
…
4
…
…
5
…
…
6
…
[6]
Evaluate the following expressions when MyString has the value
(iii)
"Corrective Maintenance":
Expression
Evaluates to
'P' & MID(MyString, 13, 4)
RIGHT(MID(MyString, 6, 10) ,4)
[2]
Question text
2
The engine management system of a car includes an energy-saving facility. When certain
conditions are met, this facility will automatically stop the engine.
The system is to be software-based. It will include a subroutine, EnergySaver, which repeatedly
takes data from sensors in the car. The subroutine decides whether or not to set the EngineStop
value.
The table of identifiers used by this subroutine is shown below.
(a) Complete the identifier table below by stating the data types.
Identifier
Data type
Description
Accelerator pedal position
Values: 0 to 100 in steps of 1
Accelerator
…
Meaning:
0: none (not pressed)
100: maximum (fully pressed)
Engine temperature in °C
EngineTemp
…
(-50 to +150 stored to 1 decimal place)
Normal engine temperature in °C
NormalTemp
…
Whole number; typical value 90
Road speed of car (in km/hr)
Speed
…
Values: 0 to 200 in steps of 1
Value used to signal engine must be stopped
Possible values:
EngineStop
…
TRUE: stop engine
FALSE: run engine
[5]
The condition for stopping the engine is that all three of the following are true:
•
Accelerator is not pressed
•
Engine temperature is normal or above
•
Car speed is zero
The initial design stage will produce a prototype of EnergySaver, with a user interface.
The structured English for this is:
1.
INPUT value for accelerator pedal position
2.
INPUT value for engine temperature
3.
INPUT value for normal engine temperature
4.
INPUT value for car speed
5.
EVALUATE engine stopping condition
6.
IF stopping condition satisfied SET engine stop value to TRUE
7.
IF stopping condition not satisfied SET engine stop value to FALSE
8.
OUTPUT message indicating engine stop value
(b) Write the pseudocode equivalent of the structured English. Use the identifiers from the table
in part (a).
For the built-in functions list, refer to the Appendix on the last page.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
3
String encryption was implemented using a simple character-substitution method. A function,
Decrypt, is needed to reverse the encryption process and return the original character.
The encryption uses the 7-bit ASCII value for each character. This value is used as an index for a
1D array, Lookup, which contains the substitute characters.
Lookup contains an entry for each of the ASCII characters.
This function, Decrypt, will accept two parameters, a single character, CipherChar, and the 1D
array, Lookup.
The steps involved in Decrypt are follows:
•
Search for the character in the array
•
Note the index value where the character is found (the index value is the ASCII value of the
original character)
•
Use the index value to obtain the original character
(a) The first attempt at writing the pseudocode for this function is shown below.
Complete the pseudocode.
For the built-in functions list, refer to the Appendix on the last page.
FUNCTION Decrypt( … , … ) RETURNS CHAR
DECLARE Found
: …
DECLARE … : INTEGER
DECLARE OriginalChar
: CHAR
1
// …
Index
FALSE
Found
//search for CipherChar in Lookup:
WHILE …
//compare CipherChar with this array element:
IF …
THEN
… //Set the flag
ELSE
Index … //Move to next array element
ENDIF
ENDWHILE
//dropped out of loop so must have found CipherChar:
… //convert Index to original character
RETURN …
ENDFUNCTION
[11]
Question 3 continues on page 8.
(b) A program is to be written to output part of the Lookup array.
The design of the algorithm is shown below.
It may be assumed that the characters output from Lookup are all printable.
START
Input StartIndex and
NumberToOutput
Index
StartIndex
Loop
Produce OriginalChar from
Index (using built-in function)
Assign current array element to
Increment Index
CipherChar
Output message line
(see examples on the next page)
NO
End of
loop?
YES
STOP
For example, for the input of 65 and 3, the output will be:
Index 65: Character A has substitute character Y
Index 66: Character B has substitute character Q
Index 67: Character C has substitute character F
Write program code to implement the flowchart design.
In addition to the Lookup array, assume that the following variables have been declared:
StartIndex, NumberToOutput, Index
Programming language …
…
…
…
…
…
Question text
4
(a) Name two features of your chosen high-level programming language that support the
implementation of a modular design.
1 …
2 …
[2]
(b) (i)
The structure chart shows part of the design of a program for an online shopping system.
The user has already added a number of products to their virtual basket.
Draw on the chart, the symbol to show that the process of modifying the basket contents
may be iterated (repeated).
Modify Basket Contents
D
B
E
A
F
C
Input product
Change product
Remove
data
quantity
product
[1]
(ii)
Each arrow in the structure chart above represents a parameter.
The table below shows the three data items that the six parameters pass between
modules.
Tick (✓) to match each parameter to the correct data item.
Parameter
Data item
A
B
C
D
E
F
Product ID
Quantity
Flag Value – indicating
operation success or fail
[4]
Question text
5
Claudia stores her large collection of music CDs in different places. Claudia wants to record where
she stores each CD. She decides to write a program to do this.
Data items for a typical CD are:
Title:
Kind of Green
Artist:
Miles Coltrane
Location: Rack3-23
The data is to be stored in a text file, MyMusic. Each line of the text file will be a string, formed by
concatenating the three data items.
Before concatenation, the title and artist will each be made into a fixed-length string of 40
characters. Space characters may need to be added to each data item.
The location is always 8 characters long.
(a) (i)
Explain the benefit of making the stored data into fixed-length strings.
…
…
…
…
State a drawback of this file design.
…
…
[3]
When Claudia buys a new CD, the CD data must be added to the existing file, MyMusic.
(ii)
She has written a procedure in pseudocode. This has the following file-handling
statements:
OPENFILE "MyMusic" FOR WRITE
WRITEFILE "MyMusic", OutputString
CLOSEFILE "MyMusic"
There is a problem with the logic of this pseudocode.
State the problem.
…
…
Identify the effect it will have if the final code is implemented in this way.
…
…
Give a possible solution.
…
…
[3]
(b) Claudia needs to output a list of all the CDs in a particular location.
She designs a procedure, OutputLocationList, to do this. She also chooses the following
identifiers:
Identifier
Data type
CDTitle
STRING
CDArtist
STRING
CDLocation
STRING
The procedure will:
•
prompt for the name of the location
•
input the location (such as “Rack3-23”)
•
search the file for all CDs at this location
•
output the title and artist of each CD found
•
output the total number of CDs found at that location (such as “17 CDs found”)
Write program code for the procedure OutputLocationList.
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 …
…
…
Question text
6
A string-handling function has been developed.
For the built-in functions list, refer to the Appendix on the last page.
The pseudocode for this function is shown below.
FUNCTION SF(ThisString
: STRING) RETURNS STRING
DECLARE x
:
CHAR
DECLARE NewString :
STRING
DECLARE Flag
:
BOOLEAN
DECLARE m, n
:
INTEGER
Flag
TRUE
NewString
""
m
LENGTH(ThisString)
1 TO m
FOR n
IF Flag = TRUE
THEN
x
UCASE(MID(ThisString, n, 1))
Flag
FALSE
ELSE
x
LCASE(MID(ThisString, n, 1))
ENDIF
NewString
NewString & x
IF x = " "
THEN
Flag
TRUE
ENDIF
ENDFOR
RETURN NewString
ENDFUNCTION
(a) (i)
Complete the trace table below by performing a dry run of the function when it is called
as follows:
SF("big BEN")
n
x
Flag
m
NewString
[4]
Describe the purpose of function SF.
(ii)
…
…
…
… [2]
(b) Test data must be designed for the function SF.
(i)
State what happens when the function is called with an empty string.
…
… [1]
(ii)
The function should be thoroughly tested.
Give three examples of non-empty strings that may be used.
In each case explain why the test string has been chosen.
String …
Explanation …
…
String …
Explanation …
…
String …
Explanation …
…
[3]
Appendix
Built-in functions (Pseudocode)
In each function below, if the function call is not properly formed, the function returns an error.
MID(ThisString : STRING, x : INTEGER, y : INTEGER) RETURNS STRING
returns the string of length y starting at position x from ThisString
Example: MID("ABCDEFGH", 2, 3) will return string "BCD"
LEFT(ThisString : STRING, x : INTEGER) RETURNS STRING
returns the leftmost x characters from ThisString
Example: LEFT("ABCDEFGH", 3) will return string "ABC"
RIGHT(ThisString: STRING, x : INTEGER) RETURNS STRING
returns the rightmost x characters from ThisString
Example: RIGHT("ABCDEFGH", 3) will return string "FGH"
CHR(x : INTEGER) RETURNS CHAR
returns the character whose ASCII value is x
Example: CHR(87) will return 'W'
ASC(x : CHAR) RETURNS INTEGER
returns the ASCII value of character x
Example: ASC('W') will return 87
LCASE(x : CHAR) RETURNS CHAR