Programming – A Level Computer Science (9618)
153 questions · page 6 of 11
Question text
6
A program displays a progress bar to inform the user of the progress of tasks that take a significant
time to complete, such as those involving file transfer operations.
Task progress is divided into 11 steps. Each step represents the amount of progress as a
percentage. An image is associated with each step and each image is stored in a different file.
Different progress bar images may be selected. For a given image, files all have the same filename
root, with a different suffix.
The table illustrates the process for using the image with filename root BargraphA
Percentage
Step
Image filename
Image
progress
1
< 10
BargraphA-1.bmp
BargraphA-2.bmp
2
>= 10 and < 20
3
>= 20 and < 30
BargraphA-3.bmp
BargraphA-9.bmp
9
>= 80 and < 90
10
>= 90 and < 100
BargraphA-10.bmp
11
100
BargraphA-11.bmp
A procedure Progress() will:
•
be called with two parameters:
○ an integer representing the percentage progress (0 to 100 inclusive)
○ a string representing the image filename root
•
generate the full image filename
•
call a procedure Display() using the full image filename as the parameter.
,
,
(a) Write pseudocode for procedure Progress().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
7
Seven program modules form part of a program. A description of the relationship between the
modules is summarised below. Any return values are stated in the description.
Module name
Description
Mod-A
calls Mod-B followed by Mod-C
•
called with parameters Par1 and Par2
•
Mod-B
calls either Mod-D or Mod-E, determined when the program runs
•
returns a Boolean value
•
called with parameters Par1 and Par3
•
Mod-C
Par3 is passed by reference
•
repeatedly calls Mod-F followed by Mod-G
Mod-D
called with parameter Par2
•
called with parameter Par3
Mod-E
•
returns an integer value
Mod-F
called with parameter Par3
•
called with parameter Par3
Mod-G
•
Par3 is passed by reference
Parameters in the table are as follows:
•
Par1 and Par3 are of type string.
•
Par2 is of type integer.
(a) (i)
Identify the modules that would be implemented as functions.
… [1]
(ii)
Modules Mod-F and Mod-G are both called with Par3 as a parameter.
In the case of Mod-F, the parameter is passed by value.
In the case of Mod-G, the parameter is passed by reference.
Explain the effect of the two different ways of passing the parameter Par3.
…
…
…
… [2]
,
,
(b) Draw a structure chart to show the relationship between the seven modules and the
parameters passed between them.
[6]
Question text
8
A teacher is designing a program to process pseudocode projects written by her students.
The program analyses a student project and extracts information about each module that is
defined (each procedure or function). This information is stored in a global 2D array ModInfo of
type string.
A module header is the first line of a module definition and starts with either of the keywords
PROCEDURE or FUNCTION.
An example of part of the array is given below. Row 10 of the array shows that a procedure header
occurs on line 27 and row 11 shows that a function header occurs on line 35. "P" represents a
procedure and "F" represents a function:
x = 1
x = 2
x = 3
ModInfo[10, x]
"27"
"P"
"MyProc(Z : CHAR)"
ModInfo[11, x]
"35"
"F"
"MyFun(Y : CHAR) RETURNS BOOLEAN"
The string stored in column 3 is called the module description. This is the module header without
the keyword.
A valid module header will:
•
be at least 13 characters long
•
start with the keyword PROCEDURE or FUNCTION. The keyword may appear in either upper or
lower case (or a mix of both) and must be followed by a space character.
The teacher has defined the first program module as follows:
Module
Description
Header()
•
called with a parameter of type string representing a line of
pseudocode
•
if the line is a valid procedure header, returns a string:
"P<Module description>"
•
if the line is a valid function header, returns a string:
"F<Module description>"
•
otherwise, returns an empty string
For example, given the string:
"FUNCTION Zap(X : INTEGER) RETURNS CHAR"
Header()returns the string:
"FZap(X : INTEGER) RETURNS CHAR"
,
,
(a) Write pseudocode for module Header().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
The following pseudocode represents part of the algorithm for a program:
CASE OF ThisValue
←
"Low"
// less than 30
< 30
: Level
←
1
Check
←
"Very Low"
// less than 20
< 20
: Level
←
ThisValue / 2
Check
←
"Medium"
// between 30 and 40
30 TO 40 : Level
←
ThisValue / 3
Check
←
Data[ThisValue] + 1
Data[ThisValue]
←
"High"
> 40
: Level
ENDCASE
(a) Complete the table by writing the answer for each row:
Answer
The value assigned to Level when ThisValue is 40
The value assigned to Check when ThisValue is 36
The value assigned to Level when ThisValue is 18
The number of elements in array Data that may be incremented
[4]
(b) The pseudocode contains four assignments to variable Level. One of these assignments
will never be performed.
Identify this assignment and explain why this is the case.
…
…
…
…
…
… [3]
(c) The following line is added immediately before the ENDCASE statement:
← "Undefined"
OTHERWISE : Level
State why this assignment is never performed.
…
… [1]
(d) Give the appropriate data types for the variables ThisValue, Check and Level.
ThisValue …
Check …
Level …
[3]
Question text
4
A global array is declared in pseudocode as follows:
DECLARE Data : ARRAY[1:150] OF STRING
A function TooMany() will:
1.
take two parameters:
•
a string (the search string)
•
an integer (the maximum value)
2.
count the number of strings in the array that exactly match the search string
3.
return TRUE if the count is greater than the maximum value, otherwise will return FALSE
(a) Write pseudocode for the function TooMany().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
(b) The global array is changed to a 2D array, organised as 150 rows by 2 columns. It is declared
in pseudocode as follows:
DECLARE Data : ARRAY[1:150, 1:2] OF STRING
The algorithm for the function in part (a) is changed. Strings will only be counted if both of
the following conditions are true:
•
The current row is an even number.
•
The search string exactly matches the value in either column.
Write pseudocode to check these conditions.
Assume that the row index is contained in variable Row and the search string in variable
Search.
…
…
…
Question text
5
An algorithm is designed to find the smallest numeric value from an input sequence and count
how many numeric values have been input.
An example of an input sequence is:
23, AB56, 17, 23ZW, 4, 10, END
Numeric input values are all integers and non-numeric input is ignored, except for the string "END"
which is used to terminate the sequence.
The algorithm is expressed in pseudocode as shown:
DECLARE NextInput : STRING
DECLARE Min, Count, Num : INTEGER
← 999
Min
← 0
Count
REPEAT
INPUT NextInput
IF IS_NUM(NextInput) = TRUE THEN
←
STR_TO_NUM(NextInput)
Num
IF Num > Min THEN
←
Num
Min
ENDIF
←
Count & 1
Count
ENDIF
←
"END"
UNTIL NextInput
OUTPUT "The minimum value is ", Min, " and the count was ", Count
(a) The pseudocode contains three errors due to the incorrect use of operators.
Identify each error and state the correction required.
1 …
…
2 …
…
3 …
…
[3]
(b) The operator errors are corrected and the algorithm is tested as follows:
The input sequence:
18, 4, ONE, 27, 189, ERIC, 3, 65, END
produces the output:
The minimum value is 3 and the count was 6
The algorithm is tested with a different test data sequence. The sequence contains a mix
of integer and non-numeric values. It is terminated correctly but the algorithm produces
unexpected results.
(i)
Explain the problem with the algorithm.
…
…
…
… [2]
(ii)
Give a sequence of four test data values that could be input to demonstrate the problem.
Value 1 …
Value 2 …
Value 3 …
Value 4 …
[2]
Question text
6
The pseudocode OUTPUT command starts each output on a new line.
(a) A new procedure MyOutput() will take a string and a Boolean parameter.
MyOutput() may be called repeatedly and will use concatenation to build a string using a
global variable MyString, up to a maximum length of 255 characters.
MyString will be output in either of these two cases:
1.
The Boolean parameter value is TRUE
2.
The resulting string (after concatenation) would be longer than 255 characters.
If MyString is not output, the string is concatenated with MyString.
For example, the calls to MyOutput() given below would result in the output as shown:
MyOutput("Hello ", FALSE)
MyOutput("ginger ", FALSE)
MyOutput("cat", TRUE)
MyOutput("How are you?", TRUE)
Resulting output:
Hello ginger cat
How are you?
Notes:
•
MyString is initialised to an empty string before MyOutput() is called for the first time.
•
No string passed to MyOutput() will be longer than 255 characters.
Write pseudocode for MyOutput().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [7]
(b) The design of the procedure given in part (a) is modified and MyString is changed from a
global to a local variable declared in MyOutput().
When the modified procedure is converted into program code, it does not work as expected.
Explain why it does not work as expected.
…
Question text
8
A class of students are developing a program to send data between computers. Many computers
are connected together to form a wired network. Serial ports are used to connect one computer to
another.
Each computer:
•
is assigned a unique three-digit ID
•
has three ports, each identified by an integer value
•
is connected to between one and three other computers.
Messages are sent between computers as a string of characters organised into fields as shown:
<STX><DestinationID><SourceID><Data><ETX>
Field
Field name
Description
number
a single character marking the start of the message
STX
n/a
(ASCII value 02)
DestinationID
1
three numeric characters that identify the destination computer
SourceID
2
three numeric characters that identify the source computer
a variable length string containing the data being sent
Data
3
(Minimum length is 1 character)
a single character marking the end of the message
ETX
n/a
(ASCII value 03)
For example, the following message contains the data "Hello Kevin" being sent from computer
"101" to computer "232":
<STX>"232101Hello Kevin"<ETX>
Each computer will run a copy of the same program. Each program will contain a global variable,
MyID of type string, that contains the unique ID of the computer in which the program is running.
The programmer has defined the first two program modules as follows:
Module
Description
• takes two parameters:
o a string containing a message
Transmit()
(already written)
o an integer containing a port number
• transmits the message using the given port
• takes three parameters:
o a string containing a text file name
o a string containing a Destination ID
SendFile()
o an integer containing a Port number
• transmits the file one line at a time
• transmits a final message with data string "****"
(a) Write pseudocode for module SendFile().
Assume:
•
module Transmit() has already been written and is used to transmit a message
•
the value of MyID may be used as SourceID
•
the file specified contains no blank lines
•
the file specified does not contain the line "****"
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
A shop sells car accessories. A customer order is created if an item cannot be supplied from
current stock. A program is being developed to create and manage the customer orders.
(a) The following identifier table shows some of the data that will be stored for each order.
Complete the identifier table by adding meaningful variable names and appropriate data
types.
Example
Explanation
Variable name
Data type
value
"Mr Khan"
The name of the customer
3
The number of items in the order
TRUE
To indicate whether this is a new customer
15.75
The deposit paid by the customer
[4]
(b) Other variables in the program have example values as shown:
Variable
Example value
Total
124.00
DepRate
2.00
Description
"AB12345:Cleaning Brush (small)"
Complete the table by evaluating each expression using the example values.
Expression
Evaluates to
(Total * DepRate) + 1.5
RIGHT(Description, 7)
(LENGTH(Description) - 8) > 16
NUM_TO_STR(INT(DepRate * 10)) & '%'
[4]
(c) The data that needs to be stored for each customer order in part (a) is not all of the same
type.
Describe an effective way of storing this data for many customer orders while the program is
running.
…
…
…
…
…
… [3]
Question text
4
A procedure Count() will:
1.
input a value (all values will be positive integers)
2.
count the number of odd values and count the number of even values
3.
repeat from step 1 until the value input is 99
4.
output the two count values, with a suitable message.
The value 99 must not be counted.
(a) Write pseudocode for the procedure Count().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
(b) The procedure Count() is to be tested.
Typical test data would consist of odd and even values, for example:
23, 5, 64, 100, 2002, 1, 8, 900, 99
The purpose of this test would be to test a typical mix of even and odd values and check the
totals.
Give three test data sequences that may be used to test different aspects of the procedure.
Do not include invalid data.
Sequence 1:
Test data …
Purpose of test. …
…
Question text
5
A global 1D array of integers contains four elements, which are assigned values as shown:
← 1
Mix[1]
← 3
Mix[2]
← 4
Mix[3]
← 2
Mix[4]
A procedure Process() manipulates the values in the array.
The procedure is written in pseudocode:
PROCEDURE Process(Start : INTEGER)
DECLARE Value, Index, Count : INTEGER
← Start
Index
← 0
Count
REPEAT
←
Mix[Index]
Value
←
Mix[Index] - 1
Mix[Index]
←
Value
Index
←
Count + 1
Count
UNTIL Count = 5
← Count * Index
Mix[4]
ENDPROCEDURE
Complete the trace table on the opposite page by dry running the procedure when it is called as
follows:
CALL Process(3)
Index
Value
Count
Mix[1]
Mix[2]
Mix[3]
Mix[4]
[6]
Question text
6
(a) A procedure CreateFiles() will take two parameters:
•
a string representing a file name
•
an integer representing the number of files to be created.
The procedure will create the number of text files specified.
Each file is given a different name. Each file name is formed by concatenating the file name
with a suffix based on the file number. The suffix is always three characters.
For example, the call CreateFiles("TestData", 3) would result in the creation of the
three files, TestData.001, TestData.002 and TestData.003.
Each file will contain a single line. For example, file TestData.002 would contain the string:
This is File TestData.002
Write pseudocode for CreateFiles().
Assume both parameters are valid and that the integer value is between 1 and 999, inclusive.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [6]
(b) A module CheckFiles() will count the number of files produced by CreateFiles() in
part (a).
CheckFiles() will take a string representing a file name and return the number of files
found.
(i)
Identify the type of module that should be used for CheckFiles().
… [1]
(ii)
Write the module header for CheckFiles().
…
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
8
A class of students are developing a program to send data between computers. Many computers
are connected together to form a wired network. Serial ports are used to connect one computer to
another.
Each computer:
•
is assigned a unique three-digit ID
•
has three ports, each identified by an integer value
•
is connected to between one and three other computers.
Data is sent as individual message strings.
Each string contains the destination ID (the ID of the computer that is to receive the message)
followed by the data:
<DestinationID><Data>
Messages may pass through several computers on the way to their destination.
When a message arrives at a computer, that is not the destination, the program needs to forward
it on to another computer using one of its serial ports.
The port to use is obtained from information that is stored in an array RouteTable.
RouteTable is a global 2D array of integers. It is declared in pseudocode as follows:
DECLARE RouteTable : ARRAY[1:6,1:3] OF INTEGER
The values in the first two columns of RouteTable define a range of ID values.
Column 3 gives the corresponding port number to use when forwarding the message to a computer
with an ID within this range.
For example, the contents of RouteTable could be:
Column 1
Column 2
Column 3
100
199
1
Row 1
200
259
2
Row 2
−1
<undefined>
<undefined>
Row 3
260
399
2
Row 4
400
599
3
Row 5
600
999
1
Row 6
In this example, a message that arrives with a DestinationID of "283" will be forwarded using
port 2.
Row 3 in the example shows an unused row. These may occur anywhere. Unused rows have the
column 1 element set to −1. The value of unused elements in the other two columns is undefined.
The programmer has defined the first program module as follows:
Module
Description
•
takes a DestinationID as a parameter of type string
•
searches for the range corresponding to the DestinationID
GetPort()
in the array
•
returns the port number, or returns −1 if no corresponding
range is found
(a) Write pseudocode for module GetPort().
Assume DestinationID contains a valid three-digit string.
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
A program is being developed in pseudocode before being converted into a programming
language.
(a) The following table shows four valid pseudocode assignment statements.
Complete the table by giving the data type that should be used to declare the variable
underlined in each assignment statement.
Assignment statement
Data type
← Total1 / Total2
MyVar1
MyVar2 ← 27/10/2023
MyVar3 ← "Sum1 / Sum2"
MyVar4 ← Result1 AND Result2
[4]
(b) Other variables in the program have example values as shown:
Variable
Value
Active
TRUE
Fraction
0.2
Code
"Ab12345"
Complete the table by evaluating each expression using the example values.
Expression
Evaluates to
Fraction >= 0.2 AND NOT Active
INT((Fraction * 100) + 13.3)
STR_TO_NUM(MID(Code, 4, 2)) + 5
LENGTH("TRUE" & Code)
[4]
(c) The program makes use of complex statistical functions. The required functions are not
built-in to the programming language and are too complicated for the programmer to write.
One solution would be to employ another programmer who has experience of writing these
functions, as there is no time to train the existing programmer.
State one other way that these functions may be provided for inclusion in the program.
…
… [1]
(d) The hardware that runs the program is changed and the program needs to be modified so
that it works with the new hardware.
Identify the type of maintenance that this represents and give one other reason why this type
of maintenance may be needed.
Type …
Reason …
…
[2]