Programming – A Level Computer Science (9618)
Quick quiz: 10 random questions from this topic. Answer them all, then open the Summary.
Question text
6
A music player stores music in a digital form and has a display which shows the track being
played.
(a) Up to 16 characters can be displayed. Track titles longer than 16 characters will need to be
trimmed as follows:
•
Words must be removed from the end of the track title until the resulting title is less than
14 characters.
•
When a word is removed, the space in front of that word is also removed.
•
Three dots are added to the end of the last word displayed when one or more words
have been removed.
The table below shows some examples:
Original title
Display string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
B
a
t
o
u
t
o
f
H
u
l
l
Bat out of Hull
B
o
h
e
m
i
a
n
.
.
.
Bohemian Symphony
P
a
p
e
r
b
o
o
k
W
r
i
t
e
r
Paperbook Writer
C
h
r
i
s
S
i
n
g
s
.
.
.
Chris Sings the Blues
G
r
e
e
n
H
o
m
e
.
.
.
Green Home Alabama
A function Trim() will:
•
take a string representing the original title
•
return the string to be displayed.
Assume:
•
Words in the original title are separated by a single space character.
•
There are no spaces before the first word or after the last word of the original title.
•
The first word of the original title is less than 14 characters.
Write pseudocode for the function Trim().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
7
A computer system for a shop stores information about each customer. The items of information
include name and address (both postal and email) together with payment details and order history.
The system also stores the product categories they are interested in and how they would like to be
contacted.
(a) The shop wants to add a program module that will generate emails to be sent to customers
who may be interested in receiving details of new products.
(i)
State three items of information that the new module would need. Justify your choice in
each case.
Information …
Justification …
…
Information …
Justification …
…
Information …
Justification …
…
[3]
(ii)
Identify two items of customer information that would not be required by the new module.
Justify your choice in each case.
Information …
Justification …
…
Information …
Justification …
…
[2]
(b) The program includes a module to validate a Personal Identification Number (PIN). This is
used when customers pay for goods using a bank card.
A state‑transition diagram has been produced for this module.
The table show the inputs, outputs and states for this part of the program:
Current state
Input
Output
Next state
S1
Input PIN
S2
S2
Re‑input PIN
Display error
S2
S2
Cancel
Re‑prompt
S1
S2
Valid PIN
Enable payment
S4
S2
Too many tries
Block Account
S3
Complete the state‑transition diagram to represent the information given in the table.
S2
START
S1
Cancel | Re-prompt
[4]
Question text
3
An algorithm will output the last three lines from a text file Result.txt
The lines need to be output in the same order as they appear in the file.
Assume:
•
Three variables LineX, LineY and LineZ will store the three lines. These are of type string
and all three variables have been initialised to an empty string.
•
The file exists and contains at least three lines.
(a) The algorithm to output the lines is expressed in eight steps.
Complete the steps.
1.
Open the file …
2.
Loop until …
3.
… and store in ThisLine
4.
Assign LineY to LineX
5.
Assign LineZ to LineY
6.
Assign ThisLine to LineZ
7.
After the loop, …
8.
Output LineX, LineY, LineZ
[4]
(b) Explain the purpose of steps 4, 5 and 6 in the algorithm from part (a).
…
…
… [1]
ĬÙĊ®Ġ´íÈõÏĪÅĊÝú¶þ×
,
,
(c) The requirement changes, and the algorithm will now output three lines from the file, starting
from a given line number.
The modified algorithm will be implemented as a function which will:
•
be called with an integer parameter representing the given line number
•
output three lines, starting at the given line
•
return TRUE if the 3 lines are output, or FALSE if it was not possible to output the 3 lines.
Describe the changes that need to be made to steps 2 to 8 of the algorithm given in part (a).
…
…
…
…
…
…
…
…
…
… [4]
ĬÛĊ®Ġ´íÈõÏĪÅĊÝü¶þ×
Question text
7
A program contains six modules:
Pseudocode module header
PROCEDURE Module-A()
PROCEDURE Module-X(T1 : INTEGER, S2 : REAL)
PROCEDURE Reset(BYREF Code : INTEGER)
FUNCTION Restore(OldCode : INTEGER) RETURNS BOOLEAN
FUNCTION Module-Y(RA : INTEGER, RB : BOOLEAN) RETURNS BOOLEAN
FUNCTION Module-Z(SA : INTEGER) RETURNS INTEGER
Module-X() calls Reset()
Module-Y() calls Restore()
(a) Complete the structure chart for these modules.
Module-A()
[4]
(b) Explain the meaning of the diamond symbol as used in the diagram in part (a).
…
… [2]
Question text
7
There are several different ways to express an algorithm during the design of a program.
(a) One part of the program contains an algorithm which is represented by a state-transition
diagram.
The table shows the inputs, outputs and states for the algorithm:
Current state
Input
Output
Next state
S1
A2
X2
S3
S1
A1
X1
S2
S2
A4
X4
S5
S3
A1
S3
S3
A3
X3
S2
S3
A2
X4
S4
S4
A1
X1
S4
S4
A3
S2
S4
A4
X4
S5
Complete the state-transition diagram to represent the information given in the table.
A2 | X2
START
S1
[5]
ĬÙĊ®Ġ´íÈõÏĪÅĊÝù·Ā×
,
,
(b) A structure chart is used to document a different part of the program.
This part of the program contains six modules:
Pseudocode module header
PROCEDURE Setup()
PROCEDURE Restart(H1 : STRING, C1 : INTEGER)
FUNCTION Modify(B1 : BOOLEAN) RETURNS INTEGER
PROCEDURE Final(T1 : INTEGER)
PROCEDURE Update(BYREF R2 : STRING)
FUNCTION Confirm() RETURNS BOOLEAN
Module Setup will repeatedly call three of the modules.
Complete the structure chart to document the information given in the table.
Setup
H1
B1
R2
[5]
ĬÛĊ®Ġ´íÈõÏĪÅĊÝû·Ā×
Question text
4
Study the following pseudocode. Line numbers are for reference only.
10 FUNCTION Convert(Name : STRING) RETURNS STRING
11
12
DECLARE Flag: BOOLEAN
13
DECLARE Index : INTEGER
14
DECLARE ThisChar : CHAR
15
DECLARE NewName : STRING
16
17
CONSTANT SPACECHAR = ' '
18
TRUE
19
Flag
1
20
Index
""
// formatted name string
21
NewName
22
23
WHILE Index <= LENGTH(Name)
MID(Name, Index, 1)
24
ThisChar
25
IF Flag = TRUE THEN
NewName & UCASE(ThisChar)
26
NewName
27
IF ThisChar <> SPACECHAR THEN
FALSE
28
Flag
29
ENDIF
30
ELSE
NewName & ThisChar
31
NewName
32
ENDIF
33
IF ThisChar = SPACECHAR THEN
TRUE
34
Flag
35
ENDIF
Index + 1
36
Index
37
ENDWHILE
38
39
RETURN NewName
40
41 ENDFUNCTION
(a) Complete the trace table below by dry running the function when it is called as follows:
Convert("∇in∇a∇∇Cup")
Result
Note: The symbol '∇' has been used to represent a space character.
Use this symbol for any space characters in the trace table.
The first row has been completed for you.
Name
Flag
Index
NewName
ThisChar
"∇in∇a∇∇Cup"
[5]
(b) The pseudocode for Convert() contains a conditional loop.
State a more appropriate loop structure.
Justify your answer.
Loop structure …
…
Justification …
…
…
[2]
(c) Two changes need to be made to the algorithm.
Change 1:
Convert to lower case any character that is not the first character after a space.
Change 2:
Replace multiple spaces with a single space.
(i)
Change 1 may be implemented by modifying one line of the pseudocode.
Write the modified line.
…
… [1]
(ii)
Change 2 may be implemented by moving one line of the pseudocode.
Write the number of the line to be moved and state its new position.
Line number …
New position …
…
[2]
Question text
9
A program allows a user to save passwords used to log in to websites. A stored password is then
inserted automatically when the user logs in to the corresponding website.
A student is developing a program to generate a strong password. The password will be of a fixed
format, consisting of three groups of four alphanumeric characters, separated by the hyphen
character '-'.
An example of a password is: "FxAf-3hzV-Aq49"
A valid password:
•
must be 14 characters long
•
must be organised as three groups of four alphanumeric characters. The groups are
separated by hyphen characters
•
may include duplicated characters, provided these appear in different groups.
The programmer has started to define program modules as follows:
Module
Description
• Generates a single random character from within one of the following
ranges:
○ 'a' to 'z'
RandomChar()
○ 'A' to 'Z'
○ '0' to '9'
• Returns the character
• Takes two parameters:
○ a string
○ a character
Exists()
• Performs a case-sensitive search for the character in the string
• Returns TRUE if the character occurs in the string, otherwise returns
FALSE
• Generates a valid password
• Uses RandomChar() and Exists()
Generate()
• Returns the password
Note: in a case-sensitive comparison, 'a' is not the same as 'A'.
(a) Write pseudocode for the module Generate().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
4
(a) A procedure LastLines() will:
•
take the name of a text file as a parameter
•
output the last three lines from that file, in the same order as they appear in the file.
Note:
•
Use local variables LineX, LineY and LineZ to store the three lines from the file.
•
You may assume the file exists and contains at least three lines.
Write pseudocode for the procedure LastLines().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
(b) The algorithm in part (a) is to be amended. The calling program will pass the number of lines
to be output as well as the name of the text file.
The number of lines could be any value from 1 to 30.
It can be assumed that the file contains at least the number of lines passed.
Outline three changes that would be needed.
1 …
…
…
Question text
4
Study the algorithm:
DECLARE Chars : ARRAY[1:4] OF CHAR
DECLARE I, J : INTEGER
DECLARE Key : CHAR
2
I
'D'
Chars[1]
'T'
Chars[2]
'H'
Chars[3]
'R'
Chars[4]
WHILE I <= 4
//Outer loop
Chars[I]
Key
I – 1
J
WHILE J >= 0 AND Chars[J] > Key
//Inner loop
Chars[J]
Chars[J + 1]
J – 1
J
ENDWHILE
Key
Chars[J + 1]
I + 1
I
ENDWHILE
(a) The outer loop structure used in the algorithm is not the most appropriate one to use.
State the type of loop structure that would be the most appropriate to use and justify why it is
the most appropriate.
Loop structure …
Justification …
…
[2]
ĬÙĊ®Ġ´íÈõÏĪÅĊßüµĀ×
,
,
(b) Complete the trace table by dry running the algorithm.
The first row has been completed.
Chars
I
Key
J
Chars[J]
[1]
[2]
[3]
[4]
2
'D'
'T'
'H'
'R'
[6]
ĬÛĊ®Ġ´íÈõÏĪÅĊßúµĀ×
Question text
5
A global 1D array Num of integers contains four elements. A program assigns values to these
elements as shown:
1
Num[1]
2
Num[2]
5
Num[3]
3
Num[4]
A procedure Process() manipulates the values in the array.
The procedure is written in pseudocode:
PROCEDURE Process(Start : INTEGER)
DECLARE CaseVar, Index, Count : INTEGER
Start
Index
0
Count
WHILE Count <= 20
CaseVar Num[Index]
CASE OF CaseVar
1 : Num[Index] Num[Index] + Index //clause one
Index Index + 1
Count Count + 1
2 : Num[Index] Num[Index] + Index //clause two
Index Index + 2
Count Count + 2
3 : Num[Index] Num[Index] * 2
//clause three
Index Index + 3
Count Count - 1
4 : Count Count + 4
//clause four
OTHERWISE : Count 20
ENDCASE
(Index MOD 4) + 1
Index
ENDWHILE
ENDPROCEDURE
ĬÑĊ¾Ġ´íÈõÏĪÅĊàù¸þ×
,
,
(a) Complete the trace table by dry running the procedure when it is called in the statement:
CALL Process(1)
Index
CaseVar
Count
Num[1]
Num[2]
Num[3]
Num[4]
[5]
ĬÓĊ¾Ġ´íÈõÏĪÅĊàû¸þ×
,
,
(b) As a reminder, the CASE structure in the pseudocode is:
CASE OF CaseVar
1 : Num[Index] Num[Index] + Index //clause one
Index Index + 1
Count Count + 1
2 : Num[Index] Num[Index] + Index //clause two
Index Index + 2
Count Count + 2
3 : Num[Index] Num[Index] * 2
//clause three
Index Index + 3
Count Count - 1
4 : Count Count + 4
//clause four
OTHERWISE : Count 20
ENDCASE
The CASE structure could be optimised by combining two existing clauses.
(i)
Identify the CaseVar values for these two existing clauses.
… [1]
(ii)
Write pseudocode for a single clause to replace the two clauses identified in (b) (i).
…
…
… [2]
ĬÑĊ¾Ġ´íÈõÏĪÅĊÞù¸Ā×