Programming – A Level Computer Science (9618)
153 questions · page 4 of 11
Question text
4
A program includes the following assignment statement:
← STR_TO_NUM(x) / STR_TO_NUM(y)
Result
When the program evaluates the expression in the statement, it performs a calculation.
Variable Result is of type real and variables x and y are of type string.
Two checks are required before the calculation is performed:
1.
The two strings represent valid numeric values.
2.
The numeric value of string y is not zero.
(a) Identify the type of error that could occur if these checks are not carried out and state a
cause of this error.
Type …
…
Cause …
…
[2]
(b) The designer considers implementing the checks and calculation as a module (a procedure
or a function). One reason for this is that the same checks and calculations are performed at
several places in the program.
Give another reason why this is a suitable approach and state what is avoided by this
approach.
Reason …
…
Avoided …
…
[2]
ĬÙĊ®Ġ´íÈõÏĪÅĊßú¶Ā×
,
,
(c) The module to perform the checks and calculation will be implemented as a function. The
function will need to return both a real and a Boolean value. To achieve this a record type is
defined in pseudocode as follows:
TYPE Result
DECLARE Done : BOOLEAN
DECLARE Value : REAL
ENDTYPE
The function Evaluate() will:
•
take two parameters of type string representing the two numeric values
•
return a variable of type Result with the Done field set to FALSE if either of the following
applies:
◦
at least one of the strings does not represent a valid numeric value
◦
the numeric value of the string representing value y is zero
•
otherwise return a variable of type Result with the Done field set to TRUE and the
Value field assigned the result of the formula (based on the numeric value of the two
parameters).
Write pseudocode for the function Evaluate().
…
…
…
…
…
…
…
…
Question text
6
A factory produces food items. The items must be used within a certain number of days after their
production date. The number of days is known as the shelf life. It is different for each type of item
but is always a whole number in the range 1 to 21 (inclusive).
The latest date that an item can be used is called the ‘use-by’ date.
A program is needed to produce labels which show the ‘use-by’ date.
Part of the program is a function GetDate() which will:
•
take two parameters: a production date and a value representing the shelf life
•
return the corresponding ‘use-by’ date.
The program contains a global 1D array DaysInMonth of type integer which stores the number of
days in each month (index 1 is January):
Index
Value
31
1
28
2
Note: Leap years are not considered
31
3
30
4
30
11
31
12
(a) An algorithm uses the array DaysInMonth to calculate a ‘use-by’ date. An alternative design
would involve the use of multiple selection statements.
An array-based technique is often used when there is a large number of different values to
check and where no pattern exists.
One advantage of using an array-based technique is the speed of execution compared to the
use of multiple selection statements.
Give two other advantages of using an array for this type of operation rather than a solution
based on multiple selection statements.
1 …
…
2 …
…
[2]
ĬÙĊ®Ġ´íÈõÏĪÅĊàú¸Ā×
,
,
(b) Complete the pseudocode for the function GetDate().
Date functions from the insert should be used in your solution.
FUNCTION GetDate(ProductionDate : DATE, ShelfLife : INTEGER) RETURNS DATE
…
…
…
…
…
…
…
…
…
…
…
…
Question text
7
A program contains six modules with headers as follows:
Pseudocode module header
PROCEDURE Connect()
FUNCTION Activate(H1 : STRING, Code : INTEGER) RETURNS BOOLEAN
FUNCTION Sync(T1 : BOOLEAN, S2 : REAL) RETURNS INTEGER
PROCEDURE Initialise(BYREF ID : INTEGER, BYVAL CC : INTEGER)
FUNCTION Reset(RA : STRING) RETURNS INTEGER
FUNCTION Enable(SA : INTEGER) RETURNS BOOLEAN
Module Connect() will call either Activate() or Sync(). This is decided at run-time.
(a) Complete the structure chart for these modules.
Connect
T1
[5]
(b) Explain the meaning of the curved arrow symbol used in the diagram in part (a).
…
… [2]
ĬÕĊ®Ġ´íÈõÏĪÅĊßûµĂ×
Question text
8
An exam paper has a maximum of 75 marks. One of five pass grades (A to E) is assigned,
depending on the mark obtained. The lowest mark for a given grade is known as the grade
boundary. For example, if the grade boundary for an A grade is 65 marks, then any candidate who
achieves a mark of 65 or above will be awarded an A. A grade of U is awarded for marks below the
E grade boundary.
The five grade boundaries are stored in a global 1D array GradeBoundary of type integer.
For example:
Element
Value
Comment
GradeBoundary[1]
65
The minimum mark for an A grade.
GradeBoundary[2]
57
The minimum mark for a B grade.
GradeBoundary[3]
43
The minimum mark for a C grade.
GradeBoundary[4]
35
The minimum mark for a D grade.
GradeBoundary[5]
27
The minimum mark for an E grade.
A global 2D array Result of type integer contains candidate marks for the exam. Each row
relates to one candidate. Column 1 contains the candidate mark and column 2 contains the unique
candidate ID.
For example, for the fourth and fifth candidates:
Element
Mark
Element
ID
Result[4, 1]
56
Result[4, 2]
1074832
Result[5, 1]
54
Result[5, 2]
2573839
There are more rows in the array than candidates who sit the exam. Any unused rows will be at
the end of the array.
Candidate papers that are given a mark within two marks of any grade boundary must be checked.
For example, given the values in the example grade boundaries above, any paper that was
awarded between 41 and 45 marks (inclusive) would need to be checked.
A program is being written to identify papers that need to be checked.
The programmer has defined the first program module as follows:
Module
Description
•
called with a parameter of type integer representing a
candidate mark
CheckMark()
•
returns TRUE if the mark is within 2 of any of the five grade
boundaries, otherwise returns FALSE
ĬÕĊ®Ġ´íÈõÏĪÅĊÝûµĄ×
,
,
(a) Write pseudocode for module CheckMark().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
A program has been developed and released for general use. After a few months of use an error is
detected where under certain circumstances it outputs an unexpected value.
(a) The error in the program needs to be corrected.
Identify the stage of the program development life cycle that this correction is made in.
… [1]
(b) The program contains a function Lookup(). After investigation, it is found that this is the
function that sometimes returns an incorrect value.
An Integrated Development Environment (IDE) is used to help locate the error.
The IDE features of watch window, single stepping and breakpoint will be used.
Explain these features including the order that they will be used in to locate the error in
Lookup().
…
…
…
…
…
… [3]
(c) To solve the error a programmer decides to create a new module.
The design of the new module has been completed and the module is being coded.
Identify two features of an IDE that will help during the coding of this new module.
1 …
2 …
[2]
(d) The new module referred to in part (c) introduces three new variables.
Complete the following table by giving the appropriate data type for each.
Variable name
Used to store
Data type
Name
A customer name.
Index
An array index.
The result of the division of any two
Result
non-zero numbers.
[3]
ĬÍĊ¾Ġ´íÈõÏĪÅĊÝú¸þ×
Question text
2
A program is being developed to process bank card information.
When a card number is displayed, all the characters except the last four are replaced with the
asterisk character '*'.
Card numbers are stored as strings. The strings are between 10 and 20 characters in length.
The function Conceal() will take a string representing a card number and return a modified
string.
Example strings:
Original string
Modified string
"1234567890"
"******7890"
"1234567897652"
"*********7652"
"1234567890123456" "************3456"
(a) The function Conceal() will:
•
take a numeric string as a parameter representing the card number
•
return a string in which the asterisk character replaces all except the last four characters
of the card number parameter.
Write pseudocode for the function Conceal().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
ĬÍĊ¾Ġ´íÈõÏĪÅĊßú¸Ā×
,
,
(b) The requirements have been changed. Conceal() will now be written as a procedure which
will process 100 card numbers each time it is called.
The card numbers will be stored in a 2D array CardNumber. The original string will be stored
in column one and the modified string in column two.
(i)
Write pseudocode to declare the array.
…
… [2]
(ii)
The new procedure Conceal() will write the modified string to the corresponding
element in column two.
The array CardNumber is passed as a parameter to the new procedure Conceal().
Identify how this parameter should be specified in the new procedure header.
…
Question text
3
A program uses a stack to hold up to 60 numeric values.
The stack is implemented using two integer variables and a 1D array.
The array is declared in pseudocode as shown:
DECLARE ThisStack : ARRAY[1:60] OF REAL
The stack operates as follows:
•
Global variable SP acts as a stack pointer that points to the next available stack location. The
value of SP represents an array index.
•
Global variable OnStack represents the number of values currently on the stack.
•
The stack grows upwards from array element index 1.
(a) (i)
Give the initial values that should be assigned to the two variables.
SP …
OnStack …
[1]
(ii)
Explain why it is not necessary to initialise the array elements before the stack is used.
…
…
…
… [2]
(b) A function to add a value to ThisStack is expressed in pseudocode as shown.
The function will return a value to indicate whether the operation was successful or not.
Complete the pseudocode by filling in the gaps.
FUNCTION Push(ThisValue : REAL) RETURNS BOOLEAN
DECLARE ReturnValue : BOOLEAN
IF … THEN
RETURN … // stack is already full
ENDIF
ThisValue
…
…
SP
OnStack + 1
OnStack
RETURN TRUE
ENDFUNCTION
[4]
ĬÑĊ¾Ġ´íÈõÏĪÅĊÞüµþ×
Question text
4
A global integer variable Tick is always incremented every millisecond (1000 times per second)
regardless of the other programs running.
The value of Tick can be read by any program but the value should not be changed.
Assume that the value of Tick does not overflow.
As an example, the following pseudocode algorithm would output "Goodbye" 40 seconds after
outputting "Hello".
DECLARE Start : INTEGER
OUTPUT "Hello"
Tick
Start
REPEAT
//do nothing
UNTIL Tick = Start + 40000
OUTPUT "Goodbye"
A program is needed to help a user to time an event such as boiling an egg.
The time taken for the event is known as the elapsed time.
The program contains a procedure Timer() which will:
•
take two integer values representing an elapsed time in minutes and seconds
•
use the value of variable Tick to calculate the elapsed time
•
output a warning message 30 seconds before the elapsed time is up
•
output a final message when the total time has elapsed.
For example, to set an alarm for 5 minutes and 45 seconds the program makes the following call:
CALL Timer(5, 45)
When 5 minutes and 15 seconds have elapsed, the program will output:
"30 seconds to go"
When 5 minutes and 45 seconds have elapsed, the program will output:
"The time is up!"
ĬÑĊ¾Ġ´íÈõÏĪÅĊàüµĀ×
,
,
Write pseudocode for the procedure Timer().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
5
A program contains a global 1D array Data with 100 elements of type INTEGER.
The program contains a function Process() expressed in pseudocode as follows:
FUNCTION Process(Number : INTEGER, Label : STRING) RETURNS STRING
DECLARE Index, Count : INTEGER
DECLARE ReturnValue : STRING
INT(100 / Number)
Count
Data[Number]
Index
CASE OF (Index MOD 2)
TO_UPPER(RIGHT(Label, Count))
0 : ReturnValue
"****"
1 : ReturnValue
ENDCASE
RETURN RetVal
ENDFUNCTION
(a) Run-time errors can be generated in different ways. For example, a run-time error will be
generated if a function is called with invalid parameters.
The pseudocode contains three statements that could generate a run-time error.
Write the three statements and explain how each could generate a run-time error.
Statement 1 …
Explanation …
…
Statement 2 …
Explanation …
…
Statement 3 …
Explanation …
…
[3]
ĬÑĊ¾Ġ´íÈõÏĪÅĊÝü·þ×
,
,
(b) One type of run-time error can cause a program to stop responding (‘freezing’).
Identify a particular type of programming construct that can generate this type of error and
explain why it occurs.
Construct …
Explanation …
…
…
[2]
(c) The function Process() contains a selection construct using a CASE structure.
Write pseudocode using a single selection construct with the same functionality without
using a CASE structure.
…
…
…
…
… [2]
ĬÓĊ¾Ġ´íÈõÏĪÅĊÝú·þ×
Question text
6
A shop sells sandwiches and snacks. The owner chooses a ‘daily special’ sandwich which is
displayed on a board outside the shop. Each ‘daily special’ has two different fillings and is made
with one type of bread.
The owner wants a program to randomly choose the ‘daily special’ sandwich.
The program designer decides to store the possible sandwich fillings in a 1D array of type string.
The array is declared in pseudocode as follows:
DECLARE Filling : ARRAY [1:35] OF STRING
Each element contains the name of one filling.
An example of the first five elements is as follows:
Index
Element value
"Cheese"
1
"Onion"
2
"Salmon"
3
"Anchovies"
4
"Peanut Butter"
5
A second 1D array stores the possible bread used:
DECLARE Bread : ARRAY [1:10] OF STRING
Each element contains the name of one type of bread.
An example of the first three elements is as follows:
Index
Element value
"White"
1
"Brown"
2
"Pitta"
3
Both arrays may contain unused elements. The value of these will be an empty string and they
may occur anywhere in each array.
A procedure Special() will output a message giving the ‘daily special’ sandwich made from two
randomly selected different fillings and one randomly selected bread.
Unused array elements must not be used when creating the ‘daily special’ sandwich.
Using the above examples, the output could be:
"The daily special is Cheese and Onion on Brown bread."
ĬÑĊ¾Ġ´íÈõÏĪÅĊßü·Ā×
,
,
(a) Complete the pseudocode for the procedure Special().
Assume that both arrays are global.
PROCEDURE Special()
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
7
A coffee shop runs a computerised loyalty card system.
Customers are issued with a loyalty card with their name together with a unique customer ID.
Loyalty points are added to their card each time they spend money at the shop.
The following information is stored for each customer: ID, name, home address, email address,
mobile phone number, date of birth, number of points, date of last visit and amount of money
spent at last visit.
A new module will generate a personalised email message to each loyalty card customer who has
not visited the coffee shop in the last three months. The message will include a unique voucher
code which can be used to authorise a discount if the customer goes to the shop within the next
two weeks.
(a) Identify three items of customer information that will be used by the new module and justify
your choices.
Item 1 …
Justification …
…
Item 2 …
Justification …
…
Item 3 …
Justification …
…
[3]
(b) Identify the computational thinking skill that you needed to use to answer part (a).
… [1]
(c) It is decided to adopt a formal program development life cycle model for the development of
the new module.
The analysis of the new module is complete and the project moves on to the design stage.
During this stage all the necessary algorithms and module designs will be defined.
State three other items that will be defined for the new module during the design stage.
1 …
2 …
3 …
[3]
ĬÏĊ¾Ġ´íÈõÏĪÅĊàû¶Ă×
,
,
(d) Part of the coffee shop program contains three program modules as follows:
•
Module Init() has no parameters and returns a Boolean.
•
Module Reset() takes a string as a parameter and returns an Integer.
•
Module Check() repeatedly calls Init() followed by Reset().
Draw a structure chart to represent the relationship between the three modules, including all
parameters and return values.
[3]
ĬÍĊ¾Ġ´íÈõÏĪÅĊÞù¶Ą×
Question text
8
A program is being developed to implement a game for up to six players.
During the game, each player assembles a team of characters. At the start of the game there are
45 characters available.
Each character has four attributes, as follows:
Attribute
Examples
Comment
Player
0, 1, 3
The player the character is assigned to.
The job that the character will perform in the
Role
Builder, Teacher, Doctor
game.
The name of the character. Several characters
Name
Bill, Lee, Farah, Mo
may perform the same role, but they will each
have a unique name.
The skill level of the character. An integer in the
Level
14, 23, 76
range 0 to 100 inclusive.
The programmer has defined a record type to define each character.
The record type definition is shown in pseudocode as follows:
TYPE CharacterType
DECLARE Player : INTEGER
DECLARE Role : STRING
DECLARE Name : STRING
DECLARE Level : INTEGER
ENDTYPE
The Player field indicates the player to which the character is assigned (1 to 6). The field value is
0 if the character is not assigned to any player.
The programmer has defined a global array to store the character data as follows:
DECLARE Character : ARRAY[1:45] OF CharacterType
At the start of the game all record fields are initialised, and all Player field values are set to 0
The programmer has defined a program module as follows:
Module
Description
Assign()
•
called with two parameters:
○ an integer representing a player
○ a string representing a character role
•
search the Character array for an unassigned character with the
required role
•
If found, assign the character to the given player and output a confirmation
message, for example:
"Bill the Builder has been assigned to player 3"
•
If no unassigned character with the required role is found, output a suitable
message.
ĬÍĊ¾Ġ´íÈõÏĪÅĊßù¸Ă×
,
,
(a) Write pseudocode for module Assign().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
(a) The following table contains pseudocode examples.
Each example may contain statements that relate to one or more of the following:
•
selection
•
iteration (repetition)
•
subroutine (procedure or function).
Complete the table by placing one or more ticks (' ✓ ') in each row.
Pseudocode example
Selection
Iteration
Subroutine
←
FOR Index
1 TO 3
IF Safe[Index] = TRUE THEN
←
Flap[Index]
0
ENDIF
NEXT Index
CASE OF Compound(3)
REPEAT UNTIL AllDone() = TRUE
WHILE Result[3] <> FALSE
[4]
(b) Complete the table by giving the appropriate data type in each case.
Variable
Example data value
Data type
Available
TRUE
Received
"18/04/2021"
Index
100
[3]
(c) Evaluate each expression in the table by using the data values shown in part (b).
Write ‘ERROR’ if the expression contains an error.
Expression
Evaluates to
Available AND NOT(Index > 100)
Index MOD 30
NUM_TO_STR(Index + "33")
[3]
ĬÕĊ®Ġ´íÈõÏĪÅĊÝù·þ×
Question text
4
An examination paper has a maximum of 75 marks. One of five pass grades (A to E) is assigned,
depending on the mark obtained. The lowest mark for a given grade is known as the grade boundary.
A program is being written to process examination marks.
The five grade boundaries are stored in a global 1D array GB of type INTEGER, for example:
Index
Value
Comment
65
1
The minimum mark for an A grade.
57
2
The minimum mark for a B grade.
43
3
The minimum mark for a C grade.
35
4
The minimum mark for a D grade.
27
5
The minimum mark for an E grade.
Any paper that achieves a mark within 2 marks of a grade boundary must be checked. Using the
given table, a paper with 45 marks would need to be checked.
(a) The pseudocode algorithm to determine whether a paper should be checked is as shown.
The mark for the paper is stored in variable Mark. Global variables Mark, Index, Upper and
Lower are declared as integers.
Complete the pseudocode.
← 1 TO …
FOR Index
Lower ← GB[Index] - 2
Upper ← …
IF Mark … AND Mark … THEN
OUTPUT "Check this paper"
ENDIF
NEXT Index
[4]
(b) An alternative algorithm to determine if a paper needs to be checked uses a global 1D array
Check, containing 76 elements of type BOOLEAN. The indices of the array are from 0 to 75
(inclusive), corresponding to the range of possible marks.
An element value in Check is TRUE if the index is within 2 marks of a grade boundary. For
example, in the case where the C grade boundary is 43 the corresponding part of the Check
array would be as follows:
Index
Value
FALSE
40
TRUE
41
TRUE
42
← The grade boundary for a C grade
TRUE
43
TRUE
44
TRUE
45
FALSE
46
ĬÙĊ®Ġ´íÈõÏĪÅĊàû¶Ā×
,
,
(i)
The mark for a given paper is stored in variable Mark.
Describe how an algorithm would use the Check array to determine whether this paper
should be checked.
…
…
…
… [2]
(ii)
A procedure GBInitialise() will initialise the Check array using values from the GB
array.
Note it can be assumed that the maximum grade boundary value for A is 70 and the
minimum value for E is 15.
Write pseudocode for the procedure.
…
…
…
…
…
…
…
…
…
Question text
6
In some countries, on the third Sunday in March, daylight saving time begins when clocks move
forward by one hour.
A module AdjustClock() will take an integer parameter representing a year. The module will
return an integer value representing the number of the day in March on which the clocks move
forward.
For example, the following line of pseudocode would assign DayNumber the value 20:
← AdjustClock(2022)
DayNumber
Write pseudocode for the function AdjustClock().
Date functions from the insert should be used in your solution.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [7]
ĬÛĊ®Ġ´íÈõÏĪÅĊÝù¸þ×