Programming – A Level Computer Science (9618)
153 questions · page 9 of 11
Question text
6
Components are weighed during manufacture. Weights are measured to the nearest whole gram.
Components that weigh at least 3 grams more than the maximum weight, or at least 3 grams less
than the minimum weight, are rejected.
A component is rechecked if it weighs within 2 grams of either the maximum or minimum weight.
The final outcome of weighing each component is shown below:
Outcome
Weight
Reject
Max + 2
Recheck
Max
Maximum weight
Max – 2
Accept
Min + 2
Recheck
Min
Minimum weight
Min – 2
Reject
A function Status() will be called with three parameters. These are integers representing the
weight of an individual component together with the minimum and maximum weights.
The value returned from the function will be as follows:
Outcome
Return value
'A'
Accept
'R'
Reject
'C'
Recheck
(a) Complete the following test plan for five tests that could be performed on function Status().
The tests should address all possible outcomes.
Test number
Component weight
Min
Max
Expected return value
'A'
1
2
3
4
5
[5]
(b) Write pseudocode for Status().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
(a) A programmer draws a program flowchart to show the sequence of steps required to solve a
problem.
Give the technical term for a sequence of steps that describe how to solve a problem.
…
… [1]
(b) The table lists some of the variables used in a program.
(i)
Complete the table by writing the most appropriate data type for each variable.
Variable
Use of variable
Data type
Temp
Stores the average temperature
PetName
Stores the name of my pet
To calculate the number of days until my next
MyDOB
birthday
LightOn
Stores state of light; light is only on or off
[4]
(ii)
One of the names used for a variable in the table in part 1(b)(i) is not an example of
good practice.
Identify the variable and give a reason why it is not good practice to use that name.
Variable …
Reason …
…
…
[2]
(c) Complete the table by evaluating each expression.
Expression
Evaluation
INT((31 / 3) + 1)
MID(TO_UPPER("Version"), 4, 2)
TRUE AND (NOT FALSE)
NUM_TO_STR(27 MOD 3)
[4]
Question text
3
The manager of a cinema wants a program to allow users to book seats. The cinema has several
screens. Each screen shows a different film.
(a) Decomposition will be used to break the problem down into sub-problems.
Describe three program modules that could be used in the design.
Module 1 …
…
…
Module 2 …
…
…
Module 3 …
…
…
[3]
(b) Two types of program modules may be used in the design of the program.
Identify the type of program module that should be used to return a value.
… [1]
Question text
6
(a) An algorithm will:
•
output each integer value between 100 and 200 that ends with the digit 7, for example,
107
•
output a final count of the number of values that are output.
Write pseudocode for this algorithm.
Any variables used must be declared.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
(b) Study the following pseudocode.
CASE OF MySwitch
'a'
1: ThisChar
'y'
2: ThisChar
'7'
3: ThisChar
'*'
OTHERWISE: ThisChar
ENDCASE
Write pseudocode with the same functionality without using a CASE structure.
…
…
…
…
…
…
…
…
…
Question text
7
A string is a palindrome if it reads the same forwards as backwards.
The following strings are examples of palindromes:
"Racecar"
"madam"
"12344321"
Upper-case and lower-case characters need to be treated the same. For example, 'A' is equivalent
to 'a'.
(a) A function IsPalindrome() will take a string parameter. The function will return TRUE if the
string is a palindrome and will return FALSE if the string is not a palindrome.
Write pseudocode for IsPalindrome().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
8
A program allows a user to save passwords used to login to websites. A stored password is
inserted automatically when the user logs into the corresponding website.
A student is developing a program to generate a password. The password will be of a fixed format,
consisting of three groups of four alphanumeric characters. The groups are separated by the
hyphen character '-'.
An example of a password is:
"FxAf-3haV-Tq49"
A global 2D array Secret of type STRING stores the passwords together with the website domain
name where they are used. Secret contains 1000 elements organised as 500 rows by 2 columns.
Unused elements contain the empty string (""). These may occur anywhere in the array.
An example of a part of the array is:
Array element
Value
Secret[27, 1]
"www.thiswebsite.com"
Secret[27, 2]
""
Secret[28, 1]
"www.thatwebsite.com"
Secret[28, 2]
""
Note:
•
For security, passwords are stored in an encrypted form, shown as "" in the
example.
•
The passwords cannot be used without being decrypted.
•
Assume that the encrypted form of a password will not be an empty string.
The programmer has started to define program modules as follows:
Module
Description
RandomChar()
•
Generates a single random character from within one of the
following ranges:
○ 'a' to 'z'
○ 'A' to 'Z'
○ '0' to '9'
•
Returns the character
Encrypt()
•
Takes a password as a parameter of type string
•
Returns the encrypted form of the password as a string
Decrypt()
•
Takes an encrypted password as a parameter of type string
•
Returns the decrypted form of the password as a string
For reference, relevant ASCII values are as follows:
Character range
ASCII range
'a' to 'z'
97 to 122
'A' to 'Z'
65 to 90
'0' to '9'
48 to 57
(a) Write pseudocode for module RandomChar().
You may wish to refer to the insert for a description of the CHR() function. Other functions
may also be required.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
.
Question text
2
An algorithm is described as follows:
1.
Input an integer value.
2.
Jump to step 6 if the value is less than zero.
3.
Call the function IsPrime() using the integer value as a parameter.
4.
Keep a count of the number of times function IsPrime() returns TRUE.
5.
Repeat from step 1.
6.
Output the value of the count with a suitable message.
Draw a program flowchart to represent the algorithm.
START
END
[4]
Question text
3
(a) The module headers for five modules in a program are defined in pseudocode as follows:
Pseudocode module header
FUNCTION Mod_V(S2 : INTEGER) RETURNS BOOLEAN
PROCEDURE Mod_W(P4 : INTEGER)
PROCEDURE Mod_X(T4 : INTEGER, BYREF P3 : REAL)
PROCEDURE Mod_Y(W3 : REAL, Z8 : INTEGER)
FUNCTION Mod_Z(F3 : REAL) RETURNS INTEGER
An additional module Head() repeatedly calls three of the modules in sequence.
A structure chart has been partially completed.
(i)
Complete the structure chart to include the information given about the six modules.
Do not label the parameters and do not write the module names.
A
B
C
D
E
F
[3]
(ii)
Complete the table using the information in part 3(a) by writing each module name to
replace the labels A to F.
Label
Module name
A
B
C
D
E
F
[3]
(b) The structure chart represents part of a complex problem. The process of decomposition is
used to break down the complex problem into sub-problems.
Describe three benefits of this approach.
1 …
…
2 …
…
3 …
…
[3]
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
5
Study the following pseudocode. Line numbers are for reference only.
10 PROCEDURE Encode()
11
DECLARE CountA, CountB, ThisNum : INTEGER
12
DECLARE ThisChar : CHAR
13
DECLARE Flag : BOOLEAN
0
14
CountA
10
15
CountB
TRUE
16
Flag
17
INPUT ThisNum
18
WHILE ThisNum <> 0
LEFT(NUM_TO_STR(ThisNum), 1)
19
ThisChar
20
IF Flag = TRUE THEN
21
CASE OF ThisChar
CountA + 1
22
'1' : CountA
23
'2' : IF CountB < 10 THEN
CountA + 1
24
CountA
25
ENDIF
CountB - 1
26
'3' : CountB
CountB - 1
27
'4' : CountB
FALSE
28
Flag
29
OTHERWISE : OUTPUT "Ignored"
30
ENDCASE
31
ELSE
32
IF CountA > 2 THEN
NOT Flag
33
Flag
34
OUTPUT "Flip"
35
ELSE
4
36
CountA
37
ENDIF
38
ENDIF
39
INPUT ThisNum
40
ENDWHILE
41
OUTPUT CountA
42 ENDPROCEDURE
(a) Procedure Encode() contains a loop structure.
Identify the type of loop and state the condition that ends the loop.
Do not include pseudocode statements in your answer.
Type …
Condition …
…
[2]
(b) Complete the trace table below by dry running the procedure Encode() when the following
values are input:
12, 24, 57, 43, 56, 22, 31, 32, 47, 99, 0
The first row is already complete.
ThisNum
ThisChar
CountA
CountB
Flag
OUTPUT
0
10
TRUE
[6]
(c) Procedure Encode() is part of a modular program. Integration testing is to be carried out on
the program.
Describe integration testing.
…
…
…
… [2]
Question text
6
A string represents a series of whole numbers, separated by commas.
For example:
"12,13,451,22"
Assume that:
•
the comma character ',' is used as a separator
•
the string contains only the characters '0' to '9' and the comma character ','.
A procedure Parse will:
•
take the string as a parameter
•
extract each number in turn
•
calculate the total value and average value of all the numbers
•
output the total and average values with a suitable message.
Write pseudocode for the procedure.
PROCEDURE Parse(InString : STRING)
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
7
A programming language has string functions equivalent to those given in the insert.
The language includes a LEFT() and a RIGHT() function, but it does not have a MID() function.
(a) Write pseudocode for an algorithm to implement your own version of the MID() function
which will operate in the same way as that shown in the insert.
Do not use the MID() function given in the insert, but you may use any of the other functions.
Assume that the values passed to the function will be correct.
…
…
…
…
…
…
…
…
…
… [4]
(b) The values passed to your MID() function in part (a) need to be validated.
Assume that the values are of the correct data type.
State two checks that could be applied to the values passed to the function.
1 …
…
2 …
…
[2]
Question text
8
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 global 2D array Secret of type STRING stores the passwords together with the website domain
name where they are used. Secret contains 1000 elements organised as 500 rows by 2 columns.
Unused elements contain the empty string (""). These may occur anywhere in the array.
An example of a part of the array is:
Array element
Value
Secret[27, 1]
"thiswebsite.com"
Secret[27, 2]
""
Secret[28, 1]
"thatwebsite.com"
Secret[28, 2]
""
Note:
•
For security, the passwords are stored in an encrypted form, shown as "" in the
example.
•
The passwords cannot be used without being decrypted.
•
You may assume that the encrypted form of a password will NOT be an empty string.
The programmer has started to define program modules as follows:
Module
Description
•
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
•
Takes a password as a parameter of type string
Encrypt()
•
Returns the encrypted form of the password as a string
•
Takes an encrypted password as a parameter of type string
Decrypt()
•
Returns the decrypted form of the password as a string
Note: in a case-sensitive comparison, 'a' is not the same as 'A'.
(a) Write pseudocode for the module Exists().
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
(a) The following table contains pseudocode examples.
Each example may include all or part of:
•
selection
•
iteration (repetition)
•
assignment.
Complete the table by placing one or more ticks (✓) in each row.
Pseudocode example
Selection
Iteration
Assignment
FOR Index
1 TO 3
GetResult()
Safe[Index]
NEXT Index
OTHERWISE : OUTPUT "ERROR 1202"
REPEAT UNTIL Index = 27
INPUT MyName
IF Mark > 74 THEN
'A'
Grade
ENDIF
[5]
(b) (i)
Program variables have values as follows:
Variable
Value
AAA
TRUE
BBB
FALSE
Count
99
Complete the table by evaluating each expression.
Expression
Evaluation
AAA AND (Count > 99)
AAA AND (NOT BBB)
(Count <= 99) AND (AAA OR BBB)
(BBB AND Count > 50) OR NOT AAA
[2]
(ii)
Give an example of when a variable of type Boolean would be used.
…
… [1]
Question text
3
Four program modules are defined as follows:
Pseudocode module header
PROCEDURE Sub1_A(XT : INTEGER, PB : STRING)
FUNCTION Sub1_B(RA : INTEGER) RETURNS BOOLEAN
PROCEDURE Sub1_C(SB : INTEGER, BYREF SA : STRING)
PROCEDURE Section_1()
(a) A structure chart will be produced as part of the development process.
Describe the purpose of a structure chart.
…
…
…
… [2]
(b) Module Section_1() calls one of the other three modules. The module called will be
selected when the program runs.
Draw the structure chart.
[5]