Data types, arrays and files – A Level Computer Science (old syllabus, to 2021) (9608)
70 questions · page 3 of 5 · Clear filters
Question text
3
The following structure chart shows the relationship between three modules.
TopLevel
B
D
A
C
E
SubA
SubB
Parameters A to E have the following data types:
A, D : STRING
C
: CHAR
B, E : INTEGER
(a) (i)
Write the pseudocode header for module SubA().
…
…
… [3]
(ii)
Write the pseudocode header for module SubB().
…
…
… [3]
(b) Module hierarchy and parameters are two features that may be represented on a structure
chart.
State two other features than can be represented.
Feature 1 …
Feature 2 …
[2]
Question 4 begins on the next page.
Question text
5
A student is learning about text files. She wants to write a program to count the number of lines in
a file.
(a) Use structured English to describe an algorithm she could use.
…
…
…
…
…
… [3]
(b) A procedure, CountLines(), is being written to count the number of lines in a text file. The
procedure will:
•
take a filename as a string parameter
•
count the number of lines in the file
•
output a single suitable message that includes the total number of lines.
Write pseudocode for the procedure CountLines().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
The following pseudocode searches for the longest run of identical characters in the array
Message.
DECLARE Message : ARRAY[1:100] OF CHAR
PROCEDURE Search()
DECLARE
Index : INTEGER
DECLARE
ThisChar : CHAR
DECLARE
ThisRun : INTEGER
DECLARE
LongRun : INTEGER
Message[1]
ThisChar
1
ThisRun
1
LongRun
2 TO 100
FOR Index
IF Message[Index] = ThisChar
THEN
ThisRun + 1
ThisRun
ELSE
Message[Index]
ThisChar
IF ThisRun > LongRun
THEN
ThisRun
LongRun
ENDIF
1
ThisRun
ENDIF
ENDFOR
OUTPUT "The longest run was " , LongRun
ENDPROCEDURE
(a) Draw a program flowchart to represent the procedure Search().
Variable and array declarations are not required in program flowcharts.
[6]
(b) (i)
Program variables have values as follows:
Variable
Value
MeltingPoint
180.5
Soluble
FALSE
Attempt
3
ProductName
"Mushroom Compost"
Version
'A'
ProductID
"BZ27-4"
Evaluate each expression in the following table.
If an expression is invalid, write ERROR.
For the built-in functions list, refer to the Appendix on page 18.
Expression
Evaluates to
STRING_TO_NUM(MID(ProductID, 3, 2)) + 4
INT(MeltingPoint / 2)
Soluble AND Attempt > 3
LENGTH(ProductID & NUM_TO_STRING(MeltingPoint))
RIGHT(ProductName, 4) & MID(ProductName, 5, 4)
[5]
(ii)
Programming languages support different data types.
Give an appropriate data type for the following variables from part (b)(i).
Variable
Data type
MeltingPoint
Soluble
Attempt
Version
ProductID
[5]
Question text
2
(a) A student is learning about arrays. She wants to write a program to:
•
search through a 1D array of 100 elements
•
count the number of elements that contain the string “Empty”
•
output the number of elements containing “Empty” together with a suitable message.
Use structured English to describe the algorithm she could use.
…
…
…
…
…
…
…
…
…
…
… [5]
(b) She uses the process of stepwise refinement to develop her algorithm.
Explain this process.
…
…
…
…
…
… [3]
(c) The student is learning about file handling.
She has been told that there are different file modes that can be used when opening a text
file. She wants to make sure that the existing contents are not deleted when the file is opened.
Identify two file modes she could use and describe their use.
Mode …
Description …
…
…
Mode …
Description …
…
Question text
6
A text file, StudentContact.txt, contains a list of names and telephone numbers of students
in a school. Not all students in the school have provided a contact telephone number. In this case,
their name will not be in the file.
Each line of the file is stored as a string that contains a name and telephone number, separated by
the asterisk character ('*') as follows:
<Name>'*'<TelNumber>, for example:
"Bill Smith*081234567"
A 1D array, ClassList, contains the names of students in a particular class. The array consists
of 40 elements of string data type. You can assume that student names are unique.
Unused elements contain the empty string "".
A program is to be written to produce a new text file, ClassContact.txt, containing student
names and numbers for all students in a particular class.
For each name contained in the ClassList array, the program will:
•
search the StudentContact.txt file
•
copy the matching string into ClassContact.txt if the name is found
•
write the name together with “*No number” into ClassContact.txt if the name is not found.
The program will be implemented as three modules. The description of these is as follows:
Module
Description
ProcessArray()
•
Check each element of the array:
Read the student name from the array
○
Ignore unused elements
○
Call SearchFile() with the student name
○
If the student name is found, call AddToFile() to
○
write the student details to the class file
If the student name is not found, call AddToFile()
○
to write a new string to the class file, formed as
follows:
<Name>“*No number”
•
Return the number of students who have not provided a
telephone number
SearchFile()
•
Search for a given student name at the start of each line
in the file StudentContact.txt:
If the search string is found, return the text line from
○
StudentContact.txt
If the search string is not found, return an empty
○
string
AddToFile()
•
Append the given string to a specified file, for example,
AddToFile(StringName, FileName)
(a) Write program code for the module SearchFile().
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
…
…
…
…
…
…
…
…
…
…
…
…
Question text
4
Part of a program written in pseudocode is shown.
01 DECLARE NumElements : INTEGER
…
10 FUNCTION ScanArray(SearchString : STRING) RETURNS INTEGER
11
12
DECLARE ArrayIndex : INTEGER
13
DECLARE ArrayString : STRING
14
DECLARE NumberFound : INTEGER
15
0
16
ArrayIndex
0
17
NumberFound
18
1 TO NumElements
19
FOR ArrayIndex
ResultArray[ArrayIndex, 1]
20
ArrayString
21
IF ArrayString = SearchString
22
THEN
23
CALL SaveToFile(ArrayString)
NumberFound + 1
24
NumberFound
25
ENDIF
26
ENDFOR
27
28
RETURN NumberFound
29
30 ENDFUNCTION
(a) (i)
Examine the pseudocode and complete the following table.
Answer
The identifier name of a global integer
The identifier name of a user-defined procedure
The line number of an unnecessary statement
The scope of ArrayString
[4]
(ii)
Describe in detail the purpose of lines 19 to 26 in the function ScanArray().
Do not use pseudocode in your answer.
…
…
…
…
…
…
… [4]
(b) The function ScanArray() needs to be amended so that the comparison is not case
sensitive. For example, comparing “Aaaa” with “AAAa” should evaluate to TRUE.
Write program code to implement the amended ScanArray() function.
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
…
…
…
…
…
…
…
…
…
…
…
Question text
1
(a) The following table contains statements written in pseudocode.
Show the type of programming construct each statement represents.
Put a tick (3) in the appropriate column for each statement.
Repetition
Statement
Selection
Assignment
(Iteration)
Index + 5
Index
1 TO 100
FOR Count
ReadValue(SensorID)
TempValue[Index]
IF Index < 30
UNTIL DayNumber > 7
OTHERWISE OUTPUT "ERROR"
[6]
(b) (i)
The following table contains statements written in pseudocode.
Give the most appropriate data type for the variable used in each statement.
Statement
Data type
Revision
'B'
13.3
MaxValue
TRUE
ArrayFull
"Design"
Activity
270
NumberOfEdits
[5]
(ii)
The following table contains statements written in pseudocode.
Complete the table by evaluating each expression using the values from part (b)(i).
If any expression is invalid, write “ERROR” in the Evaluates to column.
For the built-in functions list, refer to the Appendix on page 16.
Expression
Evaluates to
MID(Activity, 3, 4) & "ature"
INT(MaxValue * 2)
ArrayFull AND NumberOfEdits < 300
ASC(Revision + 1)
Activity = "Testing" OR Revision = 'A'
[5]
Question text
3
(a) Programming is sometimes referred to as a transferable skill.
You are asked to work on a program written in a language you are not familiar with.
Explain how transferable skills would help you work on the program.
…
…
…
… [2]
(b) Stepwise refinement is often used in the development of an algorithm.
Describe stepwise refinement.
…
…
…
… [2]
(c) A program needs to search through 1000 elements of an unsorted array to find a given value.
The program will output:
•
either the position in the array of the value
•
or the message “Not Found”
Outline the steps the program needs to follow.
Do not write pseudocode or program code.
…
…
…
…
…
…
…
…
…
…
… [4]
Question 4 begins on the next page.
Question text
4
Part of a program written in pseudocode is shown.
010 DECLARE NextArrayElement : INTEGER
…
100 FUNCTION ScanFile(SearchString: STRING) RETURNS INTEGER
101
102
DECLARE FileData : STRING
103
DECLARE FileLine : INTEGER
104
105
NextArrayElement
1
106
FileLine
1
107
FileData
ReadFileLine("DataFile.txt", FileLine)
108
109
WHILE FileData <> ""
110
IF LEFT(FileData, 7) = SearchString
111
THEN
112
ResultArray[NextArrayElement]
FileData
113
NextArrayElement
NextArrayElement + 1
114
ENDIF
115
FileLine
FileLine + 1
116
FileData
ReadFileLine("DataFile.txt", FileLine)
117
ENDWHILE
118
119
CALL ScanCompleted()
120
RETURN FileLine
121
122 ENDFUNCTION
(a) (i)
Examine the pseudocode. Complete the following table.
Answer
The identifier name of a local variable
The identifier name of a user-defined procedure
The identifier name of a user-defined function
The number of dimensions of ResultArray
The scope of FileData
[5]
(ii)
Describe in detail the purpose of lines 109 to 117 in the ScanFile() function. Do not
use pseudocode in your answer.
…
…
…
…
…
… [4]
(b) The function ScanFile() is to be amended so that the first 7 characters of FileData are
not written to the array.
Write program code to implement this amended ScanFile() function.
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
…
…
…
…
…
…
…
…
…
…
…
Question text
5
The procedure LineNumber() will:
•
read data from a text file
•
modify each line by adding a line number, and the string (": ")
•
output each modified line.
For example, when the procedure reads MyFile.txt, the output is:
10: <First line of MyFile.txt>
15: <Second line of MyFile.txt>
20: <Third line of MyFile.txt>
…
350: <Last line of MyFile.txt>
The procedure takes three parameters:
Identifier
Data type
Description
FileName
The name of the text file
STRING
StartNumber
The first line number to be added
INTEGER
StepNumber
The line number increment
INTEGER
In this example, the procedure call would be:
CALL LineNumber("MyFile.txt", 10, 5)
After every 20 lines, the program outputs a message asking whether the user wants to continue.
The program ends when the user enters an 'N' or the end of file is reached.
Write pseudocode for the LineNumber() procedure.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
3
An array contains 100 integer values. An algorithm will find the maximum and minimum values
stored in the array.
(a) A programmer has started to write this program using a conditional loop.
Name a more appropriate loop structure for this task and justify your choice.
Name …
Justification …
…
…
[2]
(b) Outline the steps the program will need to follow to implement the algorithm.
Do not write pseudocode or program code.
…
…
…
…
…
…
…
… [3]
Question 4 begins on the next page.
Question text
5
The function ReadFileLine() returns a specific line from a text file.
The function takes two parameters:
Identifier
Data type
Description
The name of the text file
FileName
STRING
The line number that is required
FileLine
INTEGER
The following pseudocode gives an example of the use of the function.
ReadFileLine(FileName, FileLine)
FileData
The function ReadFileLine() will:
•
open the file, FileName
•
read each line from the file until line FileLine is found or the end of the file is reached
•
if the line exists, return the string from this line; otherwise return the string "****"
Write pseudocode for the ReadFileLine() function.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
(a) A program stores data about hospital patients.
1
Give a suitable identifier name for each of the data items.
Description of data item
Suitable identifier name
The temperature of the patient
The temperature of the room
The patient identification number
The name of the nurse taking the measurement
[4]
Program variables have values as follows:
(b) (i)
Variable
Value
MyGreeting
"Happy Birthday"
MyInitial
'C'
AgeInYears
27
Weight
60.5
Married
TRUE
Children
TRUE
Evaluate each expression in the following table.
If an expression is invalid, write ERROR.
For the built-in functions list, refer to the Appendix on page 15.
Expression
Evaluates to
"Mon" & MID(MyGreeting, 10, 2)
AgeInYears + ASC(MyInitial)
INT(MyInitial)
MOD(Weight * 2, 10)
Married AND (NOT Children)
[5]
Programming languages support different data types.
(ii)
Give an appropriate data type for each of these variables from part (b)(i).
Variable
Data type
MyGreeting
MyInitial
AgeInYears
Weight
Married
[5]
Question text
A golf club holds information about its members. When a member completes a round of golf, their
5
score is stored along with their membership number and the date of the round.
(a) Explain why the club stores these data in a file rather than an array.
…
… [1]
(b) Editing functions such as cut, copy and paste are features provided by an Integrated
Development Environment (IDE).
Give two additional features of an IDE that are helpful when coding a program.
Feature 1 …
…
Feature 2 …
…
[2]
(c) The information is stored in a text file, ScoreDetails.txt. The format of each line of the
text file is as follows:
<MembershipNumber><Date><Score>
•
MembershipNumber is a four-digit numeric string.
•
Date is a six-digit numeric string in the format DDMMYY
•
Score is a two-digit numeric string in the range "50" to "99".
A procedure, AddNewScores(), is being developed. This will allow the user to enter scores
for several members on a particular date.
The procedure, AddNewScores(), will perform the following actions:
1.
Prompt for the date of the scores.
2.
Input the date of the scores.
3.
Prompt for the membership number.
4.
Input the membership number.
5.
If the membership number is an empty string then end the procedure.
6.
Prompt for the score.
7.
Input the score.
8.
Validate the score.
9.
If the validation fails then repeat from step 6.
10.
Form a text string from the data and write this to the ScoreDetails.txt file.
11.
Repeat from step 3.
Write program code for the AddNewScores() procedure.
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
…
…
…
…
…
…
…
…
…
Question text
6
(a) The following pseudocode includes references to a 1D array.
DECLARE StudentGrade : ARRAY[1:5] OF CHAR
DECLARE n : INTEGER
DECLARE x : CHAR
3
n
StudentGrade[n]
x
(i)
Use the correct technical terms to explain the meaning of [1:5] in this pseudocode.
…
…
…
… [2]
(ii)
Use the correct technical term to complete the following statement.
Integer n is used as the … to StudentGrade.
[1]
(b) A 2D array, Picture, contains data representing a bitmap image. Each element of the array
represents one pixel of the image. The image is grey-scale encoded where the value of each
pixel ranges from 0 (representing black) to 255 (representing white) with intermediate values
representing different levels of grey.
The following is an example of an image and the corresponding data values for the Picture
array.
Bitmap image
Values
240 10
10
10
10
10
10 240
80
80 240 80
80 240 80
80
80
80 240 80
80 240 80
80
80
80 150 150 150 150 80
80
80
80 240 240 240 240 80
80
80
80 150 150 150 150 80
80
240 240 150 150 150 150 240 240
240 240 150 150 150 150 240 240
In pseudocode, the array is declared as follows:
DECLARE Picture : ARRAY[1:8, 1:8] OF INTEGER
A function, Lighten(), is required to lighten the image. Lightening an image may cause it to
‘burn out’. An image is said to be ‘burnt out’ if any pixel is set to the maximum value of 255.
The function Lighten() will:
1.
increase the value of each pixel by 10%
2.
return TRUE if the resultant image is ‘burnt out’.
Write pseudocode to implement the Lighten() function.
Assume that the array Picture is a global variable.
…
…
…
…
…
…
…
…
…
…
…
…