Further programming: paradigms, OOP, files and exceptions – A Level Computer Science (old syllabus, to 2021) (9608)
45 questions · page 1 of 3
Question text
4
Teachers in a school may work on Mondays, Tuesdays and Wednesdays. There are three time
slots on each day: time slot 1, time slot 2 and time slot 3.
A teacher is either busy or free.
The school is using a declarative language to write a program to record which teachers are busy
in each time slot on each day.
The following knowledge base is used:
01 teacher(james).
02 teacher(jill).
03 teacher(karl).
04 teacher(kira).
05 day(monday).
06 day(tuesday).
07 day(wednesday).
08 timeSlot(1).
09 timeSlot(2).
10 timeSlot(3).
11 busy(james, monday, 1).
12 busy(james, tuesday, 2).
13 busy(karl, monday, 1).
14 busy(kira, wednesday, 3).
These clauses have the following meaning:
Clause
Explanation
01
James is a teacher
05
Monday is a day
08
1 is a time slot
11
James is busy in time slot 1 on Monday
(a) More facts need to be included.
Fred is a teacher who is busy in time slot 1 on Tuesday.
Write additional clauses for these facts.
15 …
16 …
[2]
(b) Additional clauses are needed to identify whether Jill is busy in time slot 1 on Monday,
Tuesday, or Wednesday.
Write these additional clauses.
17 …
18 …
19 …
[2]
(c) Write a goal, using the variable X, to find all the teachers who are busy in time slot 3 on
Monday.
…
… [1]
(d) Write a rule to find whether a teacher X is free in a specific time slot Y on day Z.
IsTeacherFree(X, Z, Y)
IF
…
…
…
… [4]
Question text
7
Sonya is writing a computer program that requires a user input. The user should input an integer
between 1 and 100. Sonya wants to use exception handling.
(a) Explain the reasons why Sonya should use exception handling in her program.
…
…
…
… [2]
(b) Write program code to read in the number from the user and raise an exception if the data is
not valid.
Programming language …
Program code
…
…
…
…
…
…
… [3]
(c) Give two other examples of where exception handling can be used in a program.
1 …
2 …
[2]
Question text
7
A treasure box is hidden within a computer game.
The box has a code that needs to be entered to allow the user into the box. The box contains up
to 10 objects that are defined as being of the class FieldObject. The definition for the class Box
is:
Box
// small, medium or large
Size : STRING
// the 10 items the box holds
Contents : ARRAY[0 : 9] OF FieldObject
// the code to unlock the box
Lock : STRING
// the strength of the box
Strength : INTEGER
// decreases by 1 each time an
// incorrect code is entered
// instantiates an object of the Box
Constructor()
// class and assigns initial values
// to the attributes
// checks if the code is correct to
Unlock()
// unlock the box
// returns the array
GetContents()
// sets the size of the box
SetSize()
// sets the contents of the box
SetContents()
// sets the lock code
SetLock()
// sets the strength
SetStrength()
(a) The constructor creates a new instance of a box. It takes the size of the box, one item of
content and the lock code as parameters. The strength is initialised to 100.
Write program code to create the class and constructor for Box.
Do not write the code for Unlock() or any of the set or get methods. Use the constructor for
your chosen language.
Programming language …
Program code …
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
(b) The player inputs the code to unlock the box. Each time they enter an incorrect code, the
strength of the box decreases by 1. If the strength of the box becomes 0, the box automatically
unlocks.
The class Box has a method Unlock() that:
•
takes the
Question text
8
A game stores details about characters.
A declarative programming language is used to represent the following knowledge base:
01 hair(blonde).
02 hair(black).
03 hair(red).
04 face(glasses).
05 face(moustache).
06 face(beard).
07 person(ismail).
08 person(anisha).
09 person(kim).
10 person(kyle).
11 has(kyle, glasses).
12 has(kyle, beard).
13 has(anisha, red).
14 has(kyle, black).
These clauses have the following meaning:
Clause
Explanation
01
Hair can be blonde
04
Glasses can be on the face
08
Anisha is a person
12
Kyle has a beard
13
Anisha has red hair
A person, X, is a selected person if they have black hair and either a moustache or a beard.
Write a rule to represent this condition.
SelectedPerson(X)
IF
…
…
…
… [2]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of the University of
Question text
3
A programmer is creating a program for a puzzle competition.
The programmer has designed the PuzzlePlayer class to store details for each player, including
the player’s score.
The following diagram shows the design for the PuzzlePlayer class.
PuzzlePlayer
PlayerID : STRING // initialised in constructor to "PL12a3"
Name : STRING
// initialised in constructor to ""
Score : INTEGER
// initialised in constructor to 0
Constructor()
//
method used to create an instance of the
//
PuzzlePlayer class and initialise its attributes
GetPlayerID()
//
returns PlayerID
GetName()
//
returns Name
GetScore()
//
returns Score
SetPlayerID()
//
validates the parameter value to make sure it
//
is 6 characters in length and starts with
//
"PL", then sets PlayerID to this value
SetName()
//
sets the Name to the parameter value
SetScore()
//
sets the Score to the parameter value
(a) Write program code for the Constructor() method.
Use the appropriate constructor method for your chosen programming language.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
… [2]
(b) Write program code for GetPlayerID() method.
Programming language …
Program code
…
…
…
…
…
… [2]
(c) The method SetPlayerID() validates the parameter value. It checks that it is 6 characters
in length and that the first two characters are "PL". The method sets PlayerID to the
parameter value. It returns TRUE if the parameter value is valid and FALSE if it is not valid.
The function
Question text
4
A declarative programming language is used to represent the following knowledge base.
01 type(cheddar).
02 type(brie).
03 type(paneer).
04 type(parmesan).
05 country(england).
06 country(france).
07 country(india).
08 country(italy).
09 hard(parmesan).
10 soft(brie).
11 strong_smell(brie).
12 strong_smell(cheddar).
13 origin(brie, france).
These clauses have the following meanings:
Clause
Meaning
02
Brie is a type of cheese
06
France is a country
09
Parmesan is a hard cheese
10
Brie is a soft cheese
12
Cheddar has a strong smell
13
Brie is from France
(a) Camembert is a type of soft cheese from France and has a strong smell.
Write additional clauses to represent this information.
14 …
15 …
16 …
17 …
[4]
(b) Stilton (X) could be from England (Y) and Stilton could be a hard cheese, if Stilton is a type of
cheese, England is a country and Stilton is not a soft cheese.
Write this as a rule.
Cheese_Question(X, Y)
IF …
…
… [4]
Question text
7
The following table shows part of the instruction set for a processor that 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.
Direct addressing. Load the contents of the location at the given
LDD
<address>
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
LDX
<address>
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.
Indexed addressing. Form the address from <address> + the contents
STX
<address>
of the Index Register. Copy the contents of ACC to this calculated
address.
ADD
<address>
Add the contents of the given address to 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.
Following a compare instruction, jump to <address> if the compare
JPE
<address>
was True.
Following a compare instruction, jump to <address> if the compare
JPN
<address>
was False.
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.
The assembly language program in the table on the opposite page allows a username to be input
as a string of up to 8 characters in length.
For strings of less than 8 characters, the user enters the exclamation mark (!) character to
indicate that no more characters will be entered. The exclamation mark is not saved as part of the
username.
The program then outputs each character of the username in the order input.
The program will use consecutive memory locations, starting at the address labelled USERNAME,
storing one character in each location.
The program in the table is incomplete. The comment column contains descriptions for some
program instructions.
Complete the program using the given instruction set.
Instruction
Label
Comment
Op code
Operand
LDR
#0
LDM
#0
// initialise LENGTH to 0
STO
LENGTH
LOOP:
IN
// is character = EXCLAMATION(!)?
// if TRUE, jump to OUTPUT
// store character in USERNAME +
contents of IX
INC
IX
// increment Index Register
// increment LENGTH
// is LENGTH = MAX ?
// if FALSE, jump to LOOP
OUTPUT:
LDR
#0
// initialise COUNT to 0
LDX
USERNAME
OUT
INC
IX
LDD
COUNT
INC
ACC
// increment COUNT
STO
COUNT
// is COUNT = LENGTH ?
JPN
OUTPUT
END
// end program
LENGTH:
EXCLAMATION:
B0010001
MAX:
8
COUNT:
USERNAME:
[8]
Question text
3
A declarative programming language is used to represent the following knowledge base.
01 person(jordan).
02 person(gita).
03 person(paolo).
04 person(cassie).
05 animal(cat).
06 animal(hamster).
07 animal(gecko).
08 animal(fish).
09 has_pet(cassie, gecko).
10 has_pet(paolo, fish).
11 has_pet(cassie, cat).
12 has_pet(jordan, cat).
These clauses have the following meanings:
Clause
Meaning
01
Jordan is a person
05
A cat is an animal
09
Cassie has a pet gecko
(a) Clive is a person who has a pet guinea pig and a pet gecko.
Write additional clauses to represent this information.
13 …
14 …
15 …
16 …
[4]
(b) Using the variable PetOwner, the goal:
has_pet(PetOwner, cat)
returns:
PetOwner = cassie, jordan
Write the result that is returned by the goal:
has_pet(cassie, PetAnimals)
PetAnimals = …
… [1]
(c) Z wants a pet Y, if Z is a person and Y is an animal and Z does not have a pet Y.
Write this as a rule.
wants_pet( … , … )
IF …
…
… [5]
Question text
4
A programmer wants to create a mobile application to record the number of calories a person eats
in a day.
The programmer has designed the class, FoodItem, to store details for each item of food.
The following diagram shows the design for the FoodItem class.
FoodItem
FoodID
: STRING // initialised in constructor to parameter value
Name
: STRING // initialised in constructor to an empty string
Calories : INTEGER // initialised in constructor to 0
Constructor()
//
method used to create an instance of the
//
FoodItem class and initialise its attributes
GetFoodID()
//
returns FoodID
GetName()
//
returns Name
GetCalories()
//
returns Calories
SetFoodID()
//
sets the FoodID to the parameter value
SetName()
//
sets the Name to the parameter value
SetCalories()
//
validates the parameter value to make sure
//
it is a positive integer less than 2000, and
//
then sets Calories to this value
(a) Write program code for the Constructor() method.
Use the appropriate constructor method for your chosen programming language.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
… [3]
(b) Write program code for the GetCalories() method.
Programming language …
Program code
…
…
…
…
…
… [2]
(c) The method SetCalories() validates the integer parameter value that is passed to it. It
checks that the value is positive and is less than 2000.
The method sets Calories to the parameter value and returns TRUE if the parameter value
is valid. It return
Question text
5
The following table shows part of the instruction set for a processor that 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.
Direct addressing. Load the contents of the location at the given
LDD
<address>
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
LDX
<address>
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.
Indexed addressing. Form the address from <address> + the contents
STX
<address>
of the Index Register. Copy the contents of ACC to this calculated
address.
ADD
<address>
Add the contents of the given address to 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.
Following a compare instruction, jump to <address> if the compare
JPE
<address>
was True.
Following a compare instruction, jump to <address> if the compare
JPN
<address>
was False.
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.
Consider the following pseudocode algorithm:
0
Length
INPUT Character
WHILE Character <> "."
Message
Message & Character
Length
Length + 1
INPUT Character
ENDWHILE
Complete the table by writing assembly language code for the algorithm, using the given instruction
set.
Instruction
Label
Comment
Op code
Operand
// initialise IX to zero
// initialise LENGTH
LOOP:
// input character
// is character a FULLSTOP (.) ?
// jump to ENDP if TRUE
// store character in MESSAGE +
contents of IX
// increment IX
// increment LENGTH
// jump to LOOP
ENDP:
END
// end program
LENGTH:
FULLSTOP:
B01100000
// ASCII code for a full stop (.)
MESSAGE:
[8]
Question text
1
Carlos is writing exception handling code for his program.
(a) State what is meant by an exception.
…
… [1]
(b) Give three situations where an exception handling routine would be required.
1 …
…
2 …
…
3 …
…
[3]
(c) Describe the benefits of using exception handling in a program.
…
…
…
… [2]
Question text
3
A declarative programming language is used to represent the following knowledge base.
01 person(jessica).
02 person(pradeep).
03 person(steffi).
04 person(johann).
05 sport(football).
06 sport(hockey).
07 sport(cricket).
08 sport(volleyball).
09 plays(johann, football).
10 plays(steffi, cricket).
11 plays(jessica, football).
12 will_not_play(pradeep, cricket).
These clauses have the following meanings:
Clause
Meaning
01
Jessica is a person
05
Football is a sport
09
Johann plays football
12
Pradeep refuses to play cricket
(a) Elle is a person who plays rugby but refuses to play hockey.
Write additional clauses to represent this information.
13 …
14 …
15 …
16 …
[4]
(b) Write the result returned by the goal:
plays(X, football).
X = … [1]
(c) Y might play X, if Y is a person, X is a sport and Y does not refuse to play X.
Write this as a rule.
mightplay(Y , X)
IF …
…
… [5]
Question text
4
Object-oriented programming has several features. These include inheritance, classes, methods
and properties.
(a) Describe what is meant by inheritance.
…
…
…
… [2]
(b) Identify two other features of object-oriented programming.
1 …
2 …
[2]
Question text
5
A tennis club is developing a program to store details of the lessons it offers. The programmer has
designed the class Lesson for the details of the lessons.
The following class diagram shows the design for the Lesson class.
Lesson
LessonType : STRING //
initialised in constructor to the parameter
//
value passed to the constructor
Instructor : STRING //
initialised in constructor to the parameter
//
value passed to the constructor
Constructor()
//
method used to create and initialise an
//
object
GetLessonType()
//
returns LessonType value
GetInstructor()
//
returns Instructor value
GetFee()
//
returns the cost of a lesson
SetLessonType()
//
sets the LessonType to the parameter value
SetInstructor()
//
sets the Instructor to the parameter value
(a) Write program code for the Constructor() method.
Use the appropriate constructor method for your chosen programming language.
Programming language …
Program code
…
…
…
…
…
…
…
… [3]
(b) Write program code for the GetLessonType() method.
Programming language …
Program code
…
…
…
…
…
… [2]
(c) Fee is the cost that a customer will pay for a lesson.
The method GetFee() validates the parameter value. The method is sent a parameter value
that represents the skill level of the customer: beginner, intermediate or advanced.
The parameter will be a character:
•
'B' for beginner
•
'I' for intermediate
•
'A' for advanced.
The method must check the parameter value is a valid character ('B', 'I' or 'A') and
return the correct fee. It must return -1 if it is not a valid character.
The fees are:
•
$45 for a beginner
•
$50 for an intermediate
•
$55 for an advanced.
Write program code for the GetFee() method.
Programming language …
Question text
1
Amar is alerted to a run-time error when he runs a program.
(a) A run-time error occurs when Amar attempts to open a file that does not exist.
State three other reasons why a run-time error may occur.
1 …
…
2 …
…
3 …
…
[3]
(b) A program should be written with exception handling routines to manage run-time errors.
A program reads data from the text file MyData.txt. The program needs to report an
exception if it attempts to open the file and the file does not exist.
Write program code to handle and report this exception.
Programming language …
Program code
…
…
…
…
…
…
…
…
… [2]