Further programming: paradigms, OOP, files and exceptions – A Level Computer Science (old syllabus, to 2021) (9608)
45 questions · page 3 of 3
Question text
4
X-Games is an international extreme sports competition.
A program will store and process data about the teams in the competition.
•
Each team is made up of members.
•
Members can be added and removed from each team.
•
Each member has a first name, last name, date of birth and gender.
•
Each member can be an official or a competitor.
•
Each official has a job title and may be first-aid trained.
•
Each competitor takes part in one sport.
The program is written using object-oriented programming.
The program can output the full name and date of birth of any member. For example, “Nadia Abad
16/05/1995”
An introduction about a team member can be output using their name. For example, “Hello, I’m
Nadia Abad”.
The program outputs a different version of the introduction for a competitor. This version includes
the competitor’s sport. For example, “Hello, I’m Sally Jones and my sport is Skateboard Park.”
(a) Complete the following class diagram to show the attributes, methods and inheritance for the
program.
You do not need to write the get and set methods.
Member
Team
FirstName : STRING
TeamName : STRING
LastName : STRING
TeamList : ARRAY OF Member
DateOfBirth : DATE
Gender : STRING
Constructor()
Constructor()
…
Introduction()
…
DisplayFullnameAndDateOfBirth()
Competitor
Official
Sport : STRING
…
…
Constructor()
Constructor()
Introduction()
DisplayJobTitle()
[3]
(b) Write program code for the Member class.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
3
A declarative programming language is used to represent the following knowledge base:
01
person(jane).
02
person(ahmed).
03
person(caroline).
04
person(stuart).
05
food(chocolate).
06
food(sushi).
07
food(pizza).
08
food(chilli).
09
likes(jane, pizza).
10
likes(ahmed, chocolate).
11
likes(ahmed, pizza).
12
likes(jane, chilli).
13
likes(stuart, sushi).
14
dislikes(stuart, chocolate).
15
dislikes(jane, sushi).
16
dislikes(caroline, pizza).
These clauses have the following meanings:
Clause
Explanation
Jane is a person
01
Chocolate is a food
05
Jane likes pizza
09
Stuart dislikes (does not like) chocolate
14
(a) Mimi is a person who likes chocolate but does not like sushi or lettuce.
Write additional clauses to represent this information.
17 …
18 …
19 …
20 …
21 …
[5]
(b) Using the variable PersonName, the goal:
likes(PersonName, pizza).
returns:
PersonName = jane, ahmed .
Write the result that is returned by the goal:
likes(ahmed, FoodItem).
FoodItem = …
… [2]
(c) B might like A, if B is a person, A is a food and B does not dislike A.
Write this as a rule.
might_like( … , … )
IF …
…
… [6]
Question text
4
The following table shows part of the instruction set for a processor. The processor has one
general purpose register, the Accumulator (ACC), and an Index Register (IX).
Instruction
Explanation
Op code
Operand
LDM
#n
Immediate addressing. Load the number n to ACC.
LDD
<address>
Direct addressing. Load the contents of the location at the given
address to ACC.
LDI
<address>
Indirect addressing. The address to be used is at the given
address. Load the contents of this second address to ACC.
LDX
<address>
Indexed addressing. Form the address from <address> + the
contents of the index register. Copy the contents of this
calculated address to ACC.
LDR
#n
Immediate addressing. Load the number n to IX.
STO
<address>
Store the contents of ACC at the given address.
STX
<address>
Indexed addressing. Form the address from <address> + the
contents of the index register. Copy the contents from ACC to
this calculated address.
ADD
<address>
Add the contents of the given address to the ACC.
INC
<register>
Add 1 to the contents of the register (ACC or IX).
DEC
<register>
Subtract 1 from the contents of the register (ACC or IX).
JMP
<address>
Jump to the given address.
CMP
<address>
Compare the contents of ACC with the contents of <address>.
CMP
#n
Compare the contents of ACC with number n.
JPE
<address>
Following a compare instruction, jump to <address> if the
compare was True.
JPN
<address>
Following a compare instruction, jump to <address> if the
compare was False.
LSL
#n
Bits in ACC are shifted n places to the left. Zeros are introduced
on the right hand end.
LSR
#n
Bits in ACC are shifted n places to the right. Zeros are introduced
on the left hand end.
IN
Key in a character and store its ASCII value in ACC.
OUT
Output to the screen the character whose ASCII value is stored
in ACC.
END
Return control to the operating system.
(a) A program stores a letter. The user is allowed nine attempts to guess the stored letter. The
program outputs “?” and the user guesses a letter. If the user guesses the letter, the program
outputs “*”.
The following is pseudocode for this program.
REPEAT
OUTPUT '?'
INPUT GUESS
IF GUESS = LETTERTOGUESS
THEN
OUTPUT '*'
BREAK
ELSE
ATTEMPTS + 1
ATTEMPTS
ENDIF
UNTIL ATTEMPTS = 9
Write this program. Use the op codes from the instruction set provided.
Label
Op code
Operand
Comment
START:
LDM
#63
// load ASCII value for '?'
// OUTPUT '?'
// input GUESS
// compare with stored letter
// if correct guess, go to GUESSED
// increment ATTEMPTS
// is ATTEMPTS = 9 ?
// if out of guesses, go to ENDP
// go back to beginning of loop
GUESSED:
LDM
#42
// load ASCII for '*'
// OUTPUT '*'
ENDP:
END
// end program
ATTEMPTS:
0
LETTERTOGUESS:
'a'
[11]
(b) Five numbers are stored, starting in the location labelled NUMBERS. A program is needed to
multiply each of the numbers by 4 and store them back in their original location.
Write this program. Use the op codes from the instruction set on the opposite page.
Label
Op code
Operand
Comment
START:
// initialise the Index Register
// load the value from NUMBERS
// multiply by 4
// store the new value in NUMBERS
// increment the Index Register
// increment COUNT
// is COUNT = 5 ?
// repeat for next number
ENDP:
END
COUNT:
0
NUMBERS:
22
13
5
46
12
[10]
Instruction
Explanation
Op code
Operand
LDM
#n
Immediate addressing. Load the number n to ACC.
LDD
<address>
Direct addressing. Load the contents of the location at the given
address to ACC.
LDI
<address>
Indirect addressing. The address to be used is at the given
address. Load the contents of this second address to ACC.
LDX
<address>
Indexed addressing. Form the address from <address> + the
contents of the index register. Copy the contents of this
calculated address to ACC.
LDR
#n
Immediate addressing. Load the number n to IX.
STO
<address>
Store the contents of ACC at the given address.
STX
<address>
Indexed addressing. Form the address from <address> + the
contents of the index register. Cop
Question text
6
A programmer wants to create a computer simulation of animals searching for food in a desert.
The desert is represented by a 40 by 40 grid. Each position in the grid is represented by a pair of
coordinates. 'A' represents an animal and 'F' represents food. At the start of the simulation, the
grid contains 5 animals and 1 food source.
The following is an example of part of the grid.
0
1
2
3
4
...
37 38 39
0
A
..
1
F
..
2
..
A
3
A
..
...
..
..
..
..
..
..
..
..
..
38
A
..
A
39
..
A timer is used. In each time interval, each animal randomly moves 0 or 1 position in a random
direction. The program generates this movement by computing two random numbers, each of
which can be –1, 0 or 1. The program adds the first random number to the across number and the
second random number to the down number representing the animal’s position.
For example:
if 0 and 1 are generated, the across value does not change, the down value increases by 1
•
if –1 and 1 are generated, the across value decreases by 1, and the down value increases
•
by 1.
Each animal has an individual score. If the animal moves to a position in the grid with food ('F'):
the animal’s score increases by 1
•
the food disappears
•
one new animal ('A') is randomly generated and added to the grid (to a maximum of
•
20 animals)
one new food ('F') is randomly generated and added to the grid.
•
The simulation is to be implemented using object-oriented programming.
The programmer has designed two classes, Desert and Animal.
The Desert class consists of:
attributes
•
Grid
•
StepCounter
•
AnimalList
•
NumberOfAnimals
•
methods
•
Constructor
•
IncrementStepCounter
•
GenerateFood
•
DisplayGrid
•
Each attribute consists of a value and a get and set method that allow access to the attributes.
The following table describes the attributes and methods for the Animal class.
Identifier
Data type
Description
Instantiate an object of the Animal
class
Generate a pair of random numbers
•
between 0 and 39.
Constructor()
Place animal at that random
•
position.
Initialise the animal’s score to 0.
•
Delete the food.
•
Increase the score of the animal that
•
called the method.
Call the GenerateFood method of
EatFood()
•
the Desert class.
Call the Constructor method of
•
the Animal class.
Call the
•
GenerateChangeInCoordinate
method for each coordinate (across
or down number) of the animal’s
Move()
position.
Moves the animal to the new space.
•
If there is food in the new position,
•
call the EatFood method.
Initialised to 0
Score
INTEGER
The across value, between 0 and 39
Across
INTEGER
The down value, between 0 and 39
Down
INTEGER
(a) Write program code to declare the attributes and constructor for the Animal class.
You only need to write the set and get methods for the attribute Across.
You should also write:
the constructor for the class
•
set and get methods for the Across attribute only.
•
Programming language …
Program code
…
…
…
…
…
…
…
Question text
3
A declarative programming language is used to represent the knowledge base:
01
room(master_bedroom).
02
room(ensuite_bathroom).
03
room(office).
04
room(spare_bedroom).
05
room(nursery).
06
furniture(bed).
07
furniture(desk).
08
furniture(cot).
09
furniture(wardrobe).
10
furniture(computer).
11
located(bed, master_bedroom).
12
located(bed, spare_bedroom).
13
located(cot, nursery).
14
located(computer, office).
15
located(computer, master_bedroom).
These clauses have the following meanings:
Clause
Explanation
01
Master bedroom is a room
06
Bed is an item of furniture
11
Bed is located in the master bedroom
(a) Corridor is a room that contains a table and a lamp.
Write additional clauses to represent this information.
16 …
17 …
18 …
19 …
20 …
[5]
(b) Using the variable WhatItem, the goal:
located(WhatItem, master_bedroom).
returns:
WhatItem = bed, computer
Write the result returned by the goal:
located(bed, WhichRoom).
WhichRoom = …
… [2]
(c) (i)
Clauses to identify rooms that are next to each other need to be stored.
The nursery is next to the master bedroom. This information is stored as:
21 nextTo(nursery, master_bedroom).
22 nextTo(master_bedroom, nursery).
Explain why both clauses are necessary.
…
…
…
… [2]
(ii)
The corridor is next to the main bathroom.
Write additional clauses for this fact.
23 …
24 …
25 …
[3]
(d) B can be moved into A, if B is furniture, A is a room and B is not already in A.
Write this as a rule.
canBeMovedTo( … , … )
IF …
… [6]
Question text
5
The following table shows part of the instruction set for a processor. The processor has one
general purpose register, the Accumulator (ACC), and an Index Register (IX).
Instruction
Explanation
Op code
Operand
LDM
#n
Immediate addressing. Load the number n to ACC.
LDD
<address>
Direct addressing. Load the contents of the location at the given
address to ACC.
LDI
<address>
Indirect addressing. The address to be used is at the given
address. Load the contents of this second address to ACC.
LDX
<address>
Indexed addressing. Form the address from <address> + the
contents of the index register. Copy the contents of this calculated
address to ACC.
LDR
#n
Immediate addressing. Load the number n to IX.
STO
<address>
Store the contents of ACC at the given address.
STX
<address>
Indexed addressing. Form the address from <address> + the
contents of the index register. Copy the contents from ACC to this
calculated address.
ADD
<address>
Add the contents of the given address to the ACC.
INC
<register> Add 1 to the contents of the register (ACC or IX).
DEC
<register> Subtract 1 from the contents of the register (ACC or IX).
JMP
<address>
Jump to the given address.
CMP
<address>
Compare the contents of ACC with the contents of <address>.
CMP
#n
Compare the contents of ACC with number n.
JPE
<address>
Following a compare instruction, jump to <address> if the compare
was True.
JPN
<address>
Following a compare instruction, jump to <address> if the compare
was False.
LSL
#n
Bits in ACC are shifted n places to the left. Zeros are introduced
on the right hand end.
LSR
#n
Bits in ACC are shifted n places to the right. Zeros are introduced
on the left hand end.
IN
Key in a character and store its ASCII value in ACC.
OUT
Output to the screen the character whose ASCII value is stored
in ACC.
END
Return control to the operating system.
(a) Six letters are stored, starting at the location labelled LETTERS. A program is needed to
perform a linear search on LETTERS to find the letter 'x'. The program counts the number of
times 'x' appears in LETTERS.
The following is the pseudocode for the program.
0 TO 5
FOR COUNT
IF LETTERS[COUNT] = LETTERTOFIND
THEN
FOUND + 1
FOUND
ENDIF
ENDFOR
Write this program. Use the op codes from the given instruction set.
Label
Op code
Operand
Comment
START:
LDR
#0
// initialise Index Register
LOOP:
// load LETTERS
// is LETTERS = LETTERTOFIND ?
// if not, go to NOTFOUND
// increment FOUND
NOTFOUND:
// increment COUNT
// is COUNT = 6 ?
// if yes, end
// increment Index Register
// go back to beginning of loop
ENDP:
END
// end program
LETTERTOFIND:
'x'
LETTERS:
'd'
'u'
'p'
'l'
'e'
'x'
COUNT:
0
FOUND:
0
[10]
(b) Six values are stored, starting at the location VALUES. A program is needed to divide each of
the values by 8 and store them back in their original location.
Write this program. Use the op codes from the instruction set on the next page.
Label
Op code
Operand
Comment
START:
// initialise the Index Register
// load the value from VALUES
// divide by 8
// store the new value in VALUES
// increment the Index Register
// increment REPS
// is REPS = 6 ?
// repeat for next value
END
REPS:
0
VALUES:
22
13
5
46
12
33
[10]
Instruction
Explanation
Op code
Operand
LDM
#n
Immediate addressing. Load the number n to ACC.
LDD
<address>
Direct addressing. Load the contents of the location at the given
address to ACC.
LDI
<address>
Indirect addressing. The address to be used is at the given
address. Load the contents of this second address to ACC.
LDX
<address>
Indexed addressing. Form the address from <address> + the
contents of the index register. Copy the contents of this calculated
address to ACC.
LDR
#n
Immediate addressing. Load the number n to IX.
STO
<address>
Store the contents of ACC at the given address.
STX
<address>
Indexed addressing. Form the address from <address> + the
contents of the index register. Copy the contents from ACC to this
calculated address.
ADD
<addre
Question text
6
A bank has a range of customer accounts, which includes current accounts and savings accounts.
All accounts have:
•
an account number
•
a balance (amount of money in an account).
A current account has a level (bronze, silver or gold). A monthly fee ($) is taken from each account.
Savings account customers pay a regular amount ($) into their account. The payment interval is a
number of weeks (for example, 4).
An object-oriented program will be written to process data about the accounts.
(a) Complete the class diagram.
Account
AccountNumber : STRING
Balance : CURRENCY
Constructor()
GetAccountNumber()
GetBalance()
SetAccountNumber()
SetBalance()
CurrentAccount
SavingsAccount
…
…
…
…
…
…
…
…
Constructor()
…
…
…
…
…
…
…
…
GetPaymentInterval()
SetPaymentInterval()
[3]
(b) Write program code to declare the Account class.
Programming language …
Program code …
…
…
…
…
…
…
…
…
…
…
…
…
…
Question text
1
The following table shows part of the instruction set for a processor which has one general purpose
register, the Accumulator (ACC), and an Index Register (IX).
Instruction
Explanation
Op
Operand
code
Immediate addressing. Load the number n to ACC.
LDM
#n
Direct addressing. Load the contents of the location at the given address
LDD
<address>
to ACC.
Indirect addressing. The address to be used is at the given address.
LDI
<address>
Load the contents of this second address to ACC.
Indexed addressing. Form the address from <address> + the contents of
LDX
<address>
the index register. Copy the contents of this calculated address to ACC.
Store the contents of ACC at the given address.
STO
<address>
Add 1 to the contents of the register (ACC or IX).
INC
<register>
Subtract 1 from the contents of the register (ACC or IX).
DEC
<register>
Compare the contents of ACC with the contents of <address>.
CMP
<address>
Jump to given address.
JMP
<address>
Following a compare instruction, jump to <address> if the compare was
JPE
<address>
True.
Following a compare instruction, jump to <address> if the compare was
JPN
<address>
False.
Bitwise AND operation of the contents of ACC with the contents of
AND
<address>
<address>.
Bitwise XOR operation of the contents of ACC with the contents of
XOR
<address>
<address>.
Bitwise OR operation of the contents of ACC with the contents of
OR
<address>
<address>.
Key in a character and store its ASCII value in ACC.
IN
Output to the screen the character whose ASCII value is stored in ACC.
OUT
Return control to the operating system.
END
(a) A programmer writes a program that:
•
reads a character from the keyboard (assume it will be a capital letter)
•
outputs the alphabetical sequence of characters from ‘A’ to the character input. For
example, if the character ‘G’ is input, the output is:
ABCDEFG
The programmer has started to write the program in the table on the following page. The
Comment column contains descriptions for the missing instructions, labels and data.
Complete the following program. Use op codes from the given instruction set.
Label
Op code
Operand
Comment
START:
// INPUT character
// store in CHAR
// Initialise ACC (ASCII value for 'A' is 65)
// OUTPUT ACC
// compare ACC with CHAR
// if equal jump to end of FOR loop
// increment ACC
// jump to LOOP
ENDFOR:
END
CHAR:
[8]
(b) The programmer now starts to write a program that:
•
tests whether an 8-bit two’s complement integer stored at address NUMBER is positive or
negative
•
outputs 'P' for a positive integer and 'N' for a negative integer.
Complete the following program. Use op codes from the given instruction set.
Show the required value of MASK in binary.
Label
Op code
Operand
Comment
START:
MASK
// set to zero all bits except sign bit
// compare with 0
// if not equal jump to ELSE
THEN:
// load ACC with 'P' (ASCII value 80)
JMP
ENDIF
ELSE:
// load ACC with 'N' (ASCII value 78)
ENDIF:
END
NUMBER:
B00000101
// integer to be tested
MASK:
// value of mask in binary
[7]
Question text
4
An ordered linked list Abstract Data Type (ADT) has these associated operations:
•
create list
•
add item to list
•
output list to console
The ADT is to be implemented using object-oriented programming as a linked list of nodes.
Each node consists of data and a pointer.
(a) There are two classes, LinkedList and Node.
(i)
State the term used to describe the relationship between these classes.
… [1]
(ii)
Draw the appropriate diagram to represent this relationship. Do not list the attributes and
methods of the classes.
[2]
(b) The design for the Node class consists of:
•
attributes
Data : STRING
Pointer : INTEGER
•
methods
CreateNode(Data, Pointer)
SetData(Data)
SetPointer(Pointer)
GetData() RETURNS STRING
GetPointer() RETURNS INTEGER
The constructor method sets the attributes to the initial values that are passed as parameters.
Write program code for:
•
the Node class declaration
•
the constructor.
Programming language used …
Program code
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
(c) The identifier table for the LinkedList class is:
Identifier
Data type
Description
HeadPointer
INTEGER
Pointer to the first node in the ordered list.
FreeListPointer
INTEGER
Pointer to the first node in the free list.
NodeArray
ARRAY[0 : 7] OF Node
1D array stores the nodes that make the
ordered linked list. The unused nodes are
linked together into a free list.
Constructor()
Constructor instantiates
Question text
1
The following table shows part of the instruction set for a processor which has one general purpose
register, the Accumulator (ACC), and an Index Register (IX).
Instruction
Explanation
Op code
Operand
LDM
#n
Immediate addressing. Load the number n to ACC.
LDD
<address>
Direct addressing. Load the contents of the location at the given address
to ACC.
LDI
<address>
Indirect addressing. The address to be used is at the given address.
Load the contents of this second address to ACC.
LDX
<address>
Indexed addressing. Form the address from <address> + the contents of
the index register. Copy the contents of this calculated address to ACC.
STO
<address>
Store the contents of ACC at the given address.
INC
<register> Add 1 to the contents of the register (ACC or IX).
CMP
<address>
Compare the contents of ACC with the contents of <address>.
JMP
<address>
Jump to the given address.
JPE
<address>
Following a compare instruction, jump to <address> if the compare was
True.
JPN
<address>
Following a compare instruction, jump to <address> if the compare was
False.
AND
<address>
Bitwise AND operation of the contents of ACC with the contents of
<address>.
XOR
<address>
Bitwise XOR operation of the contents of ACC with the contents of
<address>.
OR
<address>
Bitwise OR operation of the contents of ACC with the contents of
<address>.
IN
Key in a character and store its ASCII value in ACC.
OUT
Output to the screen the character whose ASCII value is stored in ACC.
END
Return control to the operating system.
(a) A programmer writes a program that:
•
reads two characters input from the keyboard (you may assume they will be capital
letters in ascending alphabetical sequence)
•
outputs the alphabetical sequence of characters from the first to the second character.
For example, if the characters ‘B’ and ‘F’ are input, the output is:
BCDEF
The programmer has started to write the program in the following table. The Comment column
contains descriptions for the missing program instructions, labels and data.
Complete the following program. Use op codes from the given instruction set.
Op
Label
Operand
Comment
code
START:
// INPUT character
// store in CHAR1
// INPUT character
// store in CHAR2
// initialise ACC to ASCII value of CHAR1
// output contents of ACC
// compare ACC with CHAR2
// if equal jump to end of FOR loop
// increment ACC
// jump to LOOP
ENDFOR:
END
CHAR1:
CHAR2:
[9]
(b) The programmer now starts to write a program that:
•
converts a positive integer, stored at address NUMBER1, into its negative equivalent in
two’s complement form
•
stores the result at address NUMBER2
Complete the following program. Use op codes from the given instruction set.
Show the value stored in NUMBER2.
Op
Label
Operand
Comment
code
START:
MASK
// convert to one's complement
// convert to two's complement
END
MASK:
// show value of mask in binary here
NUMBER1:
B00000101
// positive integer
NUMBER2:
// negative equivalent
[6]
Question text
3
A programmer is writing a treasure island game to be played on the computer. The island is
a rectangular grid, 30 squares by 10 squares. Each square of the island is represented by an
element in a 2D array. The top left square of the island is represented by the array element [0, 0].
There are 30 squares across and 10 squares down.
The computer will:
•
generate three random locations where treasure will be buried
•
prompt the player for the location of one square where the player chooses to dig
•
display the contents of the array by outputting for each square:
–
'.' for only sand in this square
–
'T' for treasure still hidden in sand
–
'X' for a hole dug where treasure was found
–
'O' for a hole dug where no treasure was found.
Here is an example display after the player has chosen to dig at location [9, 3]:
…
…
…
…
…
… T …
…
…
… T …
...X …
The game is to be implemented using object-oriented programming.
The programmer has designed the class IslandClass. The identifier table for this class is:
Identifier
Data type
Description
2D array to represent the
Grid
ARRAY[0 : 9, 0 : 29] OF CHAR
squares of the island
instantiates an object of class
IslandClass and initialises
Constructor()
all squares to sand
generates a pair of random
numbers used as the grid
HideTreasure()
location of treasure and
marks the square with 'T'
takes as parameters a valid
grid location and marks the
DigHole(Row, Column)
square with 'X' or 'O' as
appropriate
takes as parameter a valid
grid location and returns the
grid value for that square
GetSquare(Row, Column) CHAR
from the IslandClass
object
(a) The programmer designed the pseudocode for the main program as follows:
DECLARE Island : IslandClass.Constructor()
// instantiate object
CALL DisplayGrid()
// output island squares
FOR Treasure ← 1 TO 3
// hide 3 treasures
CALL Island.HideTreasure()
ENDFOR
CALL StartDig()
// user to input location of dig
CALL DisplayGrid()
// output island squares
Write program code to implement this pseudocode.
Programming language used …
Program code …
…
…
…
…
…
…
…
…
…
…
Question text
1
The ticket machine in the following diagram accepts the following coins: 10, 20, 50 and 100 cents.
The ticket machine has:
•
a slot to insert coins
•
a tray to return coins
•
a ticket dispenser
•
two buttons:
•
button A (Accept)
•
button C (Cancel)
$
Coin Slot
&
Coin
Ticket dispenser
return
tray
When the user has inserted as many coins as required, they press button A to print the ticket.
To cancel the transaction, the user can press button C. This makes the machine return the coins.
Invalid coins have no effect.
The following state transition table shows the transition from one state to another of the ticket
machine:
Current state
Event
Next state
Idle
Coin inserted
Counting
Counting
Coin inserted
Counting
Counting
Button C pressed
Cancelled
Cancelled
Coins returned
Idle
Counting
Button A pressed
Accepted
Accepted
Ticket printed
Idle
(a) Complete the state-transition diagram.
start
Coin inserted
…
…
…
Cancelled
Coins returned
…
…
…
…
[7]
(b) A company wants to simulate the use of a ticket machine. It will do this with object-oriented
programming (OOP).
The following diagram shows the design for the class TicketMachine. This includes its
attributes and methods.
TicketMachine
Amount : INTEGER // total value of coins inserted in cents
State : STRING // "Idle", "Counting", "Cancelled"
// or "Accepted"
Create()
//
method to create and initialise an object
//
if using Python use __init__
SetState()
//
set state to parameter value
//
and output new state
StateChange()
//
insert coin or press button,
//
then take appropriate action
CoinInserted()
//
parameter is a string
//
change parameter to integer
//
and add coin value to Amount
ReturnCoins()
//
output Amount, then set Amount to zero
PrintTicket()
//
print ticket, then set Amount to zero
Write program code for the following methods.
Programming language …
Create()
(i)
…
…
…
…
… [3]
SetState()
(ii)
…
…
…
… [2]
ReturnCoins()
(iii)
…
…
…
…
Question text
3
(a) The numerical difference between the ASCII code of an upper case letter and the ASCII code
of its lower case equivalent is 32 denary (32 10 ).
For example, 'F' has ASCII code 70 and 'f' has ASCII code 102.
Bit number
7
6
5
4
3
2
1
0
ASCII code
ASCII code in binary
0
1
0
0
0
1
1
0
70
0
1
1
0
0
1
1
0
102
The bit patterns differ only at bit number 5. This bit is 1 if the letter is lower case and 0 if the
letter is upper case.
(i)
A program needs a mask to ensure that a letter is in upper case.
Write the binary pattern of the mask in the space provided in the table below.
Bit number
7
6
5
4
3
2
1
0
ASCII code
ASCII code in binary
0
1
0
0
0
1
1
0
70
0
1
1
0
0
1
1
0
102
Mask
Give the bit-wise operation that needs to be performed using the mask and the
ASCII code.
… [2]
(ii)
A program needs a mask to ensure that a letter is in lower case.
Write the binary pattern of the mask in the space provided in the table below.
Bit number
7
6
5
4
3
2
1
0
ASCII code
ASCII code in binary
0
1
0
0
0
1
1
0
70
0
1
1
0
0
1
1
0
102
Mask
Give the bit-wise operation that needs to be performed using the mask and the
ASCII code.
… [2]
The following table shows part of the instruction set for a processor which has one general
purpose register, the Accumulator (ACC), and an index register (IX).
Instruction
Explanation
Op
Operand
code
LDM
#n
Immediate addressing. Load the number n to ACC.
Direct addressing. Load the contents of the given address to
LDD
<address>
ACC.
Indexed addressing. Form the address from <address> +
LDX
<address>
the contents of the index register. Copy the contents of this
calculated address to ACC.
LDR
#n
Immediate addressing. Load the number n into IX.
STO
<address>
Store the contents of ACC at the given address.
INC
<register>
Add 1 to the contents of the register (ACC or IX).
CMP
<address>
Compare the contents of ACC with the contents of <address>.
CMP
#n
Compare the contents of ACC with number n.
Following a compare instruction, jump to <address> if the
JPE
<address>
compare was True.
Following a compare instruction, jump to <address> if the
JPN
<address>
compare was False.
Bitwise AND operation of the contents of ACC with the
AND
#n
operand.
Bitwise AND operation of the contents of ACC with the
AND
<address>
contents of <address>.
Bitwise XOR operation of the contents of ACC with the
XOR
#n
operand.
Bitwise XOR operation of the contents of ACC with the
XOR
<address>
contents of <address>.
OR
#n
Bitwise OR operation of the contents of ACC with the operand.
Bitwise OR operation of the contents of ACC with the contents
OR
<address>
of <address>.
Output to the screen the character whose ASCII value is
OUT
stored in ACC.
END
Return control to the operating system.
A programmer is writing a program that will output the first character of a string in upper case and
the remaining characters of the string in lower case.
The program will use locations from address WORD onwards to store the characters in the string.
The location with address LENGTH stores the number of characters that make up the string.
The programmer has started to write the program in the following table. The comment column
contains descriptions for the missing program instructions.
(b) Complete the program using op codes from the given instruction set.
Op
Label
Operand
Comment
code
START:
// initialise index register to zero
// get first character of WORD
// ensure it is in upper case using MASK1
// output character to screen
// increment index register
// load 1 into ACC
// store in COUNT
LOOP:
// load next character from indexed address WORD
// make lower case using MASK2
// output character to screen
// increment COUNT starts here
// is COUNT = LENGTH ?
// if FALSE, jump to LOOP
// end of program
COUNT:
Question text
4
When a car reaches a certain age, a safety assessment has to be carried out. A car’s brakes and
tyres must be tested. The tyre test result and the brakes test result for each car are recorded. If the
car passes the assessment, a safety certificate is issued.
Cars have a unique three-character registration.
The following knowledge base is used:
01
car(a05).
02
car(h04).
03
car(a03).
04
car(h07).
05
car(a23).
06
car(p05).
07
car(b04).
08
carRegYear(a05, 2015).
09
carRegYear(h04, 2013).
10
carRegYear(a03, 2008).
11
carRegYear(h07, 2011).
12
carRegYear(a23, 2008).
13
carRegYear(p05, 2014).
14
carRegYear(b04, 2014).
15
testBrakes(h07, pass).
16
testTyres(h07, fail).
17
testBrakes(a03, fail).
18
testTyres(a03, fail).
19
testBrakes(a23, pass).
20
testTyres(a23, pass).
21
carAssessmentDue if carRegYear(Car, RegYear)
and RegYear <= DeadlineYear.
22 issueCertificate(Car) if testTyres(Car, Result) and
testBrakes(Car, Result) and Result = pass.
DeadlineYear is assigned value 2011.
(a) (i)
Identify the car registrations for cars which are due to be tested.
… [1]
State how clause 22 determines whether or not a safety certificate will be issued.
(ii)
…
… [1]
(b) If a car fails one of the two tests, a retest is allowed.
Write a new rule for this.
retestAllowed( … ) if …
…
… [3]
(c) Logic programming uses a data structure called a list.
A new fact is added to the knowledge base.
23 carList = [a03,p05,b04,h04,h07,a23].
The following notation and operators are to be used with a list:
[X|Y] denotes a list with:
X the first list element
•
Y the list consisting of the remaining list elements
•
[] denotes an empty list
The list [a07,p03] is denoted by [A|B]
(i)
State the value of A and B.
A = …
B = …
[2]
The lists [c03,d02,n05|C] and [c03,d02,n05,p05,m04] are identical.
(ii)
State the value of C.
C = …
[1]
The list [a06,a02] is denoted by [D,E|F]
(iii)
State the value of F.
F = …
[1]
(d) The predicate conCatCompare is defined as a rule and returns TRUE or FALSE as follows:
conCatCompare(X, Y, Z)
Concatenates the lists X and Y and compares the new list with
list Z .
If equal, the clause evaluates to TRUE , otherwise FALSE .
Consider the clause:
conCatCompare(X, Y, [a7,b6,c4])
If:
the clause evaluates to TRUE
•
and Y represents the list [a7, b6, c4]
•
State the value of X.
X = … [1]
Question text
5
(a) A program calculates the exam grade awarded from a mark input by the user. The code is
written as a function CalculateGrade.
The function:
has a single parameter Mark of INTEGER data type
•
returns the grade awarded Grade of STRING data type
•
The logic for calculating the grade is as follows:
Mark
Grade
Under 40
FAIL
40 and over and under 55
PASS
55 and over and under 70
MERIT
70 and over
DISTINCTION
The programmer designs the following table for test data:
Mark
Description
Expected result (Grade)
Normal
Abnormal
Extreme/Boundary
(i)
Complete the table above.
[3]
(ii)
State why this table design is suitable for black box testing.
…
… [1]
(b) When designing and writing program code, explain what is meant by:
•
an exception
•
exception handling
…
…
…
…
…
… [3]
(c) A program is to be written to read a list of exam marks from an existing text file into a 1D array.
Each line of the file stores the mark for one student.
State three exceptions that a programmer should anticipate for this program.
1 …
…
2 …
…
3 …
… [3]
(d) The following pseudocode is to read two numbers:
01 DECLARE Num1
: INTEGER
02 DECLARE Num2
: INTEGER
03 DECLARE Answer : INTEGER
04 TRY
05
OUTPUT "First number..."
06
INPUT Num1
07
OUTPUT "Second number..."
08
INPUT Num2
09
Answer ← Num1 / (Num2 – 6)
10
OUTPUT Answer
11 EXCEPT ThisException : EXCEPTION
12
OUTPUT ThisException.Message
13 FINALLY
14
// remainder of the program follows
…
29
30 ENDTRY
The programmer writes the corresponding program code.
A user inputs the number 53 followed by 6. The following output is produced:
First number...53
Second number...6
Arithmetic operation resulted in an overflow
(i)
State the pseudocode line number which causes the exception to be raised.
…
[1]
Explain the purpose of the pseudocode on lines 11 and 12.
(ii)
…
…