Programming – A Level Computer Science (old syllabus, to 2021) (9608)

130 questions · page 8 of 9 · Clear filters

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
106
9608/22/O/N/17 · Question 2Oct/Nov 2017Paper 2211 marks
Loading question…
Question text

2
A 1D array, ClassName, of type STRING contains 100 elements.
The following pseudocode represents a simple algorithm to process the array.
DECLARE SearchValue : STRING
DECLARE FoundFlag : BOOLEAN
DECLARE Index : INTEGER
INPUT SearchValue
FALSE
FoundFlag
1
Index
WHILE Index < 101 AND FoundFlag = False
IF ClassName[Index] = SearchValue
THEN
OUTPUT Index
TRUE
FoundFlag
ENDIF
Index + 1
Index
ENDWHILE
IF FoundFlag = FALSE
THEN
OUTPUT "Not found"
ENDIF
(a) Describe the purpose of the algorithm.
…
…
…
…
…
… [2]
(b) Draw a program flowchart to represent this algorithm.
Note that variable declarations are not required in program flowcharts.
[9]

107
9608/22/O/N/17 · Question 3Oct/Nov 2017Paper 228 marks
Loading question…
Question text

3
A 1D array, Product, of type STRING is used to store information about a range of products in a
shop. There are 100 elements in the array. Each element stores one data item.
The format of each data item is as follows:
<ProductID><ProductName>
ProductID is a four-character string of numerals
•
ProductName is a variable-length string
•
The following pseudocode is an initial attempt at defining a procedure, ArraySort, which will
perform a bubble sort on Product. The array is to be sorted in ascending order of ProductID.
Line numbers have been added for identification purposes only.
01
PROCEDURE SortArray
02
DECLARE Temp : CHAR
03
DECLARE FirstID, SecondID : INTEGER
1 TO 100
04
FOR I
2 TO 99
05
FOR J
MODULUS(LEFT(Product[J], 6))
06
FirstID
MODULUS(LEFT(Product[J + 1], 6))
07
SecondID
08
IF FirstID > SecondID
09
THEN
Product[I]
10
Temp
Product[J + 1]
11
Product[I]
Temp
12
Product[J + 1]
13
ENDFOR
14
ENDIF
15
ENDFOR
16
ENDPROCEDURE
The pseudocode on page 8 contains a number of errors. Complete the following table to show:
the line number of the error
•
the error itself
•
the correction that is required.
•
Note:
If the same error occurs on more than one line, you should only refer to it ONCE.
•
Lack of optimisation should not be regarded as an error.
•
Line
Error
Correction
number
01
Wrong procedure name – “SortArray”
PROCEDURE ArraySort
[8]

108
9608/22/O/N/17 · Question 4Oct/Nov 2017Paper 2224 marks
Loading question…
Question text

4
Programming languages provide built-in functions to generate random numbers.
To be truly random, the frequency of each number generated should be the same.
You are required to write program code to test the random number generator of your chosen
language.
The test should:
generate a given number of random numbers between 1 and 10 inclusive
•
keep a count of the number of times each number is generated
•
calculate the expected frequency of each number 1 to 10
•
output the actual frequency of each number 1 to 10
•
output the difference between the actual frequency and the expected frequency.
•
The program code should be written as a procedure. In pseudocode, the procedure heading will
be:
PROCEDURE TestRandom(Repetitions AS INTEGER)
The parameter, Repetitions, contains a value representing the total number of random numbers
that should be generated.
The following example shows the expected output for the procedure call, TestRandom(200).
The expected frequency is 20.
Number
Frequency
Difference
1
17
−3
2
21
1
3
12
−8
4
28
8
5
20
0
6
19
−1
7
21
1
8
16
−4
9
24
4
10
22
2
(a) Write program code for the procedure, TestRandom.
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
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

Advertisement
109
9608/22/O/N/17 · Question 5Oct/Nov 2017Paper 229 marks
Loading question…
Question text

5
A sports club maintains a record of the email address of each of its members. The details are
stored in a text file, EmailDetails.txt. The format of each line of the text file is as follows:
<MembershipNumber><EmailAddress>
MembershipNumber is a four-character string of numerals
•
EmailAddress is a variable-length string
•
When members leave the club their details need to be removed from the file.
A procedure, RemoveDetails is required. This will perform the following actions:
1.
Input the MembershipNumber of the club member to be removed
2.
Create a new file, NewEmailDetails.txt
3.
Copy all the lines from EmailDetails.txt to NewEmailDetails.txt, except the line
with the matching MembershipNumber
Write pseudocode for the procedure, RemoveDetails.
For the built-in functions list, refer to the Appendix on page 15.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [9]
Appendix
Built-in functions (pseudocode)
In each function, if the function call is not properly formed, the function returns an error.
MODULUS(x : INTEGER, y : INTEGER) RETURNS INTEGER
returns the remainder when x is divided by y using integer arithmetic.
Example: MODULUS(5, 2) will return 1
INT(x : REAL) RETURNS INTEGER
returns the integer part of x.
Example: INT(27.5415) returns 27
LENGTH(ThisString : STRING) RETURNS INTEGER
returns the integer value representing the length of string ThisString.
Example: LENGTH("Happy Days") returns 10
LEFT(ThisString : STRING, x : INTEGER) RETURNS STRING
returns leftmost x characters from ThisString.
Example: LEFT("ABCDEFGH", 3) return

110
9608/21/M/J/17 · Question 1May/June 2017Paper 2116 marks
Loading question…
Question text

1
(a) Simple algorithms usually consist of three different stages.
Complete the following table.
Add a description of the stage and an example pseudocode statement.
The first stage has been given.
Stage
Description and example
Description: …
…
Input
Pseudocode example: …
…
Description: …
…
…
Pseudocode example: …
…
Description: …
…
…
Pseudocode example: …
…
[7]
(b) (i)
AND and OR are two operators that may be used when implementing an algorithm.
An example of their use is given in the following pseudocode statement:
VarA OR VarB
MyFlag
State the data type of variable MyFlag.
… [1]
(ii)
State the name given to the type of operators to which AND and OR belong.
… [1]
(iii)
Evaluate the expressions given in the following table when the variable values are as
follows:
TRUE
FlagA
FALSE
FlagB
TRUE
FlagC
Expression
Evaluates to
FlagA AND (FlagB OR FlagC)
FlagA AND (FlagB AND FlagC)
(NOT FlagA) OR (NOT FlagC)
[3]
(c) A common construct found in many algorithms is a loop.
Using pseudocode, write a pre-condition loop to output all of the even numbers between 99
and 201.
…
…
…
…
…
…
…
…
… [4]

111
9608/21/M/J/17 · Question 3May/June 2017Paper 2111 marks
Loading question…
Question text

3
A string conversion function, StringClean, is to be written.
This function will form a new string, OutString, from a given string, InString, by:
•
removing all non-alphabetic characters
•
converting all alphabetic characters to lower case.
For example:
InString = "Good Morning, Dave"
OutString = "goodmorningdave"
The first attempt at writing the pseudocode for this function is shown below.
Complete the pseudocode using relevant built-in functions.
For the built-in functions list, refer to the Appendix on page 14.
FUNCTION StringClean( … ) RETURNS …
DECLARE NextChar : …
DECLARE … : STRING
… //initialise the return string
//loop through InString to produce OutString
1 TO … //from first to last
FOR n
… //get next character and
NextChar
… //convert to lower case
NextChar
IF … //check if alphabetic
THEN
… //add to OutString
ENDIF
ENDFOR
… //return value
ENDFUNCTION
[11]

112
9608/21/M/J/17 · Question 6May/June 2017Paper 2118 marks
Loading question…
Question text

6
A multi-user computer system makes use of passwords.
To be valid, a password must comply with the following rules:
•
at least two lower-case alphabetic characters
•
at least two upper-case alphabetic characters
•
at least three numeric characters
•
alpha-numeric characters only
A function, ValidatePassword, is needed to check that a given password follows these rules.
This function takes a string, Pass, as a parameter and returns a Boolean value:
•
TRUE if Pass contains a valid password
•
FALSE otherwise.
(a) Write program code to implement the new function ValidatePassword.
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
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

113
9608/22/M/J/17 · Question 1May/June 2017Paper 2215 marks
Loading question…
Question text

1
(a) Simple algorithms usually consist of input, process and output.
The statements in the following table are in a generic programming language.
Complete the table by placing ticks in the relevant boxes.
Item
Statement
Input
Process
Output
1
String1 = "Hello World"
2
DISPLAY RIGHT(String1, 5)
3
READFILE (MyFile, String2)
4
WRITEFILE (MyFile, "Data is " & String2)
[6]
(b) (i)
Complete the following two sentences.
A suitable operand type for an arithmetic operator is …
A suitable operand type for a logical operator is …
[2]
(ii)
The following table shows the values of three variables.
Variable
Value
FlagA
TRUE
FlagB
FALSE
FlagC
TRUE
Evaluate these expressions.
Expression
Evaluates to
(FlagA AND FlagB) OR FlagC
FlagA AND (FlagB OR FlagC)
(NOT FlagA) OR (NOT FlagC)
[3]
(c) The loop construct (also known as repetition or iteration) appears in many algorithms.
Use pseudocode to write a post-condition loop to output all the odd numbers between 100
and 200.
…
…
…
…
…
…
…
…
… [4]

Advertisement
114
9608/22/M/J/17 · Question 3May/June 2017Paper 2211 marks
Loading question…
Question text

3
A string conversion function, ExCamel, needs to be written.
This function forms a return string, OutString, from a given string, InString, by:
1
separating the original words (a word is assumed to start with a capital letter)
2
converting all characters to lower case.
The following shows a pair of example values for the string values InString and OutString.
InString : "MyUserInput"
OutString : "my user input"
You may assume that InString always starts with a capital letter.
The following is a first attempt at writing the pseudocode for this function.
Complete the pseudocode using appropriate built-in functions.
For the built-in functions list, refer to the Appendix on page 13.
FUNCTION ExCamel ( … ) RETURNS …
DECLARE NextChar : …
DECLARE … : STRING
DECLARE n: INTEGER
… // initialise the return string
// loop through InString to produce OutString
1 TO … // from first to last
FOR n
… // get next character
NextChar
IF … // check if upper case
THEN
IF n > 1
// if not first character
THEN
… // add space to OutString
ENDIF
… // make NextChar lower case
ENDIF
… // add NextChar to OutString
ENDFOR
… // return value
ENDFUNCTION
[11]

115
9608/22/M/J/17 · Question 4May/June 2017Paper 228 marks
Loading question…
Question text

4
(a) High-level programming languages have many features that support the modular approach.
One such feature is the use of parameters.
State two other features.
1 …
…
2 …
…
[2]
(b) Consider the following pseudocode.
PROCEDURE MyProc(x)
x + 1
x
ENDPROCEDURE
Intermediate lines of pseudocode not shown
4
x
CALL MyProc(x)
OUTPUT(x)
Parameter x is used to pass data to procedure MyProc.
There are two parameter passing methods that could be used.
Complete the following table for each of the two methods.
Name of parameter
Value
Explanation
passing method
output
…
…
…
…
…
…
…
…
…
…
…
…
[6]

116
9608/22/M/J/17 · Question 6May/June 2017Paper 2214 marks
Loading question…
Question text

6
A computerised vehicle licensing system stores details about vehicles and their registration marks
(number plates or license plates).
To be valid, a vehicle registration must comply with the following rules:
•
It must be between six and nine characters long.
•
Characters 1 to 3 are upper case alphabetic characters.
•
Characters 4 to 5 are numeric characters.
•
Remaining characters are upper case alphabetic.
A function, ValidateRegistration is needed to check that a given registration mark follows
these rules. This function takes a string, Registration as a parameter and returns a Boolean
value:
•
TRUE if it is a valid registration
•
FALSE otherwise.
(a) Write program code to implement the new function, ValidateRegistration.
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
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

117
9608/21/O/N/16 · Question 2Oct/Nov 2016Paper 2110 marks
Loading question…
Question text

2
A sensing device sends bit values to a computer along data channels.
•
Channel 1 transmits a sequence of binary values from a sensor
•
Channel 2 transmits at regular intervals to indicate whether the sensor is switched on or off:
•
0 indicates switched off
•
1 indicates switched on
A program tests the bits received from the sensing device.
A program reads the signal from Channel 2 after every six values from Channel 1.
A built-in function READ(<ChannelNumber>) reads a value from the specified channel.
Pseudocode for the program is as follows:
0
BitCount
01
READ(2)
Status2
02
WHILE Status2 = 1
03
04
1 TO 6
FOR ReadingCount
05
READ(1)
ThisBit
06
IF ThisBit = 1
07
THEN
08
BitCount + 1
BitCount
09
ENDIF
10
IF BitCount = 5
11
THEN
12
OUTPUT "Error – Investigate"
13
0
BitCount
14
ENDIF
15
ENDFOR
16
17
READ(2)
Status2
18
ENDWHILE
19
(a) Trace the execution of the program for the following sequence of bits.
1
0
1
1
1
0
1
1
0
0
1
1
Channel 1
1
1
0
Channel 2
Status2
ReadingCount
ThisBit
BitCount
OUTPUT
0
1
1
1
1
2
[7]
(b) Identify the following constructs in the given program, using line numbers.
For multi-line constructs give the first line number only.
Construct
Line number
Assignment
Selection
Iteration
[3]

118
9608/21/O/N/16 · Question 3Oct/Nov 2016Paper 2124 marks
Loading question…
Question text

3
You will need to refer to the list of pseudocode string-handling functions in the Appendix.
ASCII code table (part)
Character
Decimal
Character
Decimal
Character
Decimal
<Space>
32
I
73
R
82
A
65
J
74
S
83
B
66
K
75
T
84
C
67
L
76
U
85
D
68
M
77
V
86
E
69
N
78
W
87
F
70
O
79
X
88
G
71
P
80
Y
89
H
72
Q
81
Z
90
(a) For each statement, write the value assigned to the variable.
CHARACTERCOUNT("TSUNAMI")
Term
(i)
Term … [1]
ASC('G') + ASC(<Space>)
Answer1
(ii)
Answer1 … [1]
CHR(CHARACTERCOUNT("HELLO") + 70)
Answer2
(iii)
Answer2 … [1]
SUBSTR("Welcome home", 4, 7) )
Word
(iv)
Word … [1]
Question 3(b) continues on page 10.
(b) A programmer wants to design a procedure to calculate a customer ID number from the
customer’s surname.
The procedure will:
•
input the surname
•
isolate each character in the surname and find the corresponding ASCII code
•
calculate the total of all these ASCII codes
•
this total is the customer ID
(i)
Complete the pseudocode for this procedure.
You will need to refer to the list of pseudocode string-handling functions in the Appendix.
PROCEDURE CalculateCustomerID
OUTPUT "Key in surname"
INPUT Surname
Length
…
0
CustomerID
1 TO Length
FOR i
// NextChar is a single character from Surname
NextChar
…
ASC(NextChar)
NextCodeNumber
CustomerID + …
CustomerID
ENDFOR
OUTPUT "Customer ID is ", CustomerID
[3]
(ii) Write program code for procedure CalculateCustomerID.
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 …
…
…
…
…
…
…
…
…
…
…
…
…

Advertisement
119
9608/21/O/N/16 · Question 5Oct/Nov 2016Paper 215 marks
Loading question…
Question text

5
Study the following pseudocode statements.
CONST
Pi =
3.1
: REAL
DECLARE Triangle, Base, Height, Radius, Cone : REAL
DECLARE a, b, c, Answer2 : INTEGER
DECLARE Answer1
: BOOLEAN
2.6
Base
10
Height
(Base * Height) / 2
Triangle
1
Radius
2
Height
2 * Pi * Radius * (Radius + Height)
Cone
13
a
7
b
3
c
NOT((a + b + c) > 28)
Answer1
34
Total
Total - 2
Total
a + c * c
Answer2
Give the final value assigned to each variable.
Triangle …
(i)
[1]
Cone
(ii)
…
[1]
Answer1
(iii)
…
[1]
Total
(iv)
…
[1]
Answer2
(v)
…
[1]
Appendix
Built-in functions (pseudocode)
ONECHAR(ThisString : STRING, Position : INTEGER) RETURNS CHAR
returns the single character at position Position
(counting from the start of the string with
value 1) from the string ThisString.
For example: ONECHAR("New York", 5) returns 'Y'
CHARACTERCOUNT(ThisString : STRING) RETURNS INTEGER
returns the number of characters in ThisString.
For example: CHARACTERCOUNT("New York") returns 8
SUBSTR(ThisString : STRING, Value1 : INTEGER, Value2 : INTEGER) RETURNS
STRING
returns a sub-string from within ThisString.
Value1 is the start index position (counting from the left, starting with 1).
Value2 is the final index position.
For example: SUBSTR("art nouveau", 5, 11) returns "nouveau"
TONUM(ThisString : STRING) RETURNS INTEGER or REAL
returns the integer or real equivalent of the string ThisString.
For example: TONUM("502") returns the integer 502
TONUM("56.36") returns the real number 56.36
ASC(ThisCharacter : CHAR) RETURNS INTEGER
returns an integer which is the ASCII character code for the character ThisCharacter.
For example: ASC('A') returns integer 65
CHR(Value : INTEGER) RETURNS CHAR
returns the character that ASCII code number Value represents.
For example: CHR(65) returns 'A'
RND() RETURNS REAL
returns a random number in the range 0 to 0.99999
For example: RND() returns 0.67351
INT(ThisNumber : REAL) RETURNS INTEGER
returns the integer part of ThisNumber.
For example: INT(12.79) returns 12
Errors
For any function, if the program calls the function incorrectly, the function returns an error.
Concatenation operator
& – Concatenates two expressions of STRING or CHAR data type.
For example: "South" & " " & "Pole" produces "South Pole"
'B' & "000654" produces "B000654"
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 Group. Cambridge Assessment is the brand name of University of Cambridge Local

120
9608/21/M/J/16 · Question 1May/June 2016Paper 2117 marks
Loading question…
Question text

1
The items in the table below are individual statements in a generic programming language.
For the built-in functions list, refer to the Appendix on page 18.
(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
MyScore = 65
1
FOR IndexVal = 0 TO 99
2
MyArray[3] = MID(MyString,3,2)
3
IF MyScore >= 70 THEN
4
ENDWHILE
5
ELSE Message = "Error"
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 "Adaptive
(iii)
Maintenance".
Expression
Result
'D' & RIGHT(MyString, 4)
LEFT(RIGHT(MyString, 7), 3)
[2]

Advertisement