Further programming: paradigms, OOP, files and exceptions – A Level Computer Science (old syllabus, to 2021) (9608)
20 questions · page 1 of 2 · Clear filters
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]
Question text
5
A declarative programming language is used to represent the following knowledge base.
01
person(william).
02
person(deeraj).
03
person(ingrid).
04
person(meghan).
05
country(england).
06
country(spain).
07
country(bangladesh).
08
country(new_zealand).
09
country(malaysia).
10
country(mauritius).
11
visited(william, spain).
12
visited(ingrid, new_zealand).
13
visited(deeraj, spain).
14
visited(meghan, spain).
These clauses have the following meanings:
Clause
Meaning
02
Deeraj is a person
05
England is a country
11
William has visited Spain
(a) Gina is a person who has visited Cyprus.
Write additional clauses to represent this information.
15 …
16 …
17 …
[3]
(b) Write the result returned by the goal:
visited(X, spain).
X = … [2]
(c) P might visit C, if P is a person, C is a country and P has not visited C.
Write this as a rule.
mightvisit(P , C)
IF …
…
… [4]
Question text
6
Object-oriented programming has several features. These include containment, classes, methods
and properties.
(a) Describe what is meant by containment.
…
…
…
…
…
… [3]
(b) Identify two other features of object-oriented programming.
1 …
2 …
[2]
Question text
7
A programmer is creating a computer game. The programmer has designed the class, Character,
for the characters in the game.
The following class diagram shows the design for the Character class.
Character
Name : STRING
//
initialised in constructor to the parameter
value passed to the constructor
Skill : INTEGER
//
initialised in constructor to 0
Health : INTEGER
//
initialised in constructor to 50
Shield : INTEGER
//
initialised in constructor to a random value
between 1 and 25 (inclusive)
Constructor()
//
method used to create and initialise an object
GetName()
//
returns Name value
GetSkill()
//
returns Skill value
GetHealth()
//
returns Health value
GetShield()
//
returns Shield value
SetSkill()
//
increases Skill by the parameter value
SetHealth()
//
increases or decreases Health by the parameter
value
SetShield()
//
increases or decreases Shield value by 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
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
(b) Write program code for the GetSkill() method.
Programming language …
Program code
…
Question text
2
A computer games club wants to run a competition. The club needs a system to store the scores
achieved in the competition.
A selection of score data is as follows:
99, 125, 121, 97, 109, 95, 135, 149
(a) A linked list of nodes will be used to store the data. Each node consists of the data, a left
pointer and a right pointer. The linked list will be organised as a binary tree.
(i)
Complete the binary tree to show how the score data above will be organised.
RootPointer
The symbol ∅ represents a null pointer.
LeftPointer
RightPointer
99
97
125
∅
121
[5]
(ii)
The following diagram shows a 2D array that stores the nodes of the binary tree’s linked
list.
Add the correct pointer values to complete the diagram, using your answer from
part (a)(i).
RootPointer
Index LeftPointer
Data
RightPointer
0
99
0
1
125
2
121
3
97
FreePointer
4
109
5
95
6
135
7
149
8
[6]
(b) The club also considers storing the data in the order in which it receives the scores as a
linked list in a 1D array of records.
The following pseudocode algorithm searches for an element in the linked list.
Complete the six missing sections in the algorithm.
FUNCTION FindElement(Item : INTEGER) RETURNS …
… ← RootPointer
WHILE CurrentPointer … NullPointer
IF List[CurrentPointer].Data <> …
THEN
CurrentPointer ← List[ … ].Pointer
ELSE
RETURN CurrentPointer
ENDIF
ENDWHILE
CurrentPointer ← NullPointer
… CurrentPointer
ENDFUNCTION
[6]
(c) The games club is looking at two programming paradigms: imperative and object-oriented
programming paradigms.
Describe what is meant by the imperative programming paradigm and the object-oriented
programming paradigm.
(i)
Imperative …
…
…
…
…
… [3]
(ii)
Object-oriented …
…
…
…
…
… [3]
(d) Players complete one game to place them into a category for the competition. The games club
wants to implement a program to place players into the correct category. The programmer
has decided to use object-oriented programming (OOP).
The highest score that can be achieved in the game is 150. Any score less than 50 will not
qualify for the competition. Players will be placed in a category based on their score.
The following diagram shows the design for the class Player. This includes the properties
and methods.
Player
Sco
Question text
1
A company wants an online marking system for an examination.
(a) The following is a selection of data showing final marks.
36, 45, 21, 65, 66, 13, 54, 53, 34
A linked list of nodes will be used to store the data. Each node consists of the data, a left
pointer and a right pointer. The linked list will be organised as a binary tree.
(i)
Complete the binary tree to show how the data above will be organised.
RootPointer
The symbol Ø represents a
null pointer
LeftPointer
RightPointer
36
45
21
Ø
65
66
Ø
Ø
[5]
(ii)
The following diagram shows a 2D array that stores the nodes of the binary tree’s linked
list.
Add the correct pointer values to complete the diagram, using your answer from
part (a)(i).
RootPointer
Index
LeftPointer
Data
RightPointer
0
0
36
1
45
FreePointer
2
21
3
65
4
66
5
13
6
54
7
53
8
34
9
[6]
(b) The company wants to implement a program for the marking system. It will do this with
object-oriented programming (OOP).
Many candidates take the examination. Each examination paper is given a PaperID that is
made up of the centre (school) number followed by the candidate number.
Each examination paper is awarded a grade.
The following diagram shows the design for the ExaminationPaper class. This includes
the attributes and methods.
ExaminationPaper
FinalMark : INTEGER // maximum 2 digits, initialised to 0
Grade
: STRING // "Pass", "Merit", "Distinction"
// or "Fail", initialised to "Fail"
PaperID
: STRING // centre number followed by the
// candidate number, for example
// "ZZ00991001"
Create()
//
creates and initialises a new instance
//
of the ExaminationPaper class using
//
language-appropriate constructor
SetFinalMark()
//
checks that the mark parameter has a
//
valid value, if so, assigns it to
//
FinalMark
SetGrade()
//
sets Grade based on FinalMark
GetFinalMark()
//
returns FinalMark
GetGrade()
//
returns Grade
GetPaperID()
//
returns PaperID
(i)
The constructor receives the centre number and candidate number as parameter values
to create PaperID. Other properties are initialised as instructed in the class diagram.
Write program code for the Create() constructor method.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
… [5]
(ii)
Get and set methods are used to support the security and integrity of data in
object-oriented programming.
Explain how get and set methods are used to support security and integrity.
…
Question text
1
A declarative language is used to represent facts and rules about flights.
01
direct(edinburgh, paris).
02
direct(palma, rome).
03
direct(glasgow, palma).
04
direct(glasgow, vienna).
05
direct(glasgow, salzburg).
06
07
flies(paris, fly_jet).
08
flies(mumbai, british_air).
09
flies(palma, ciebe).
10
flies(vienna, fly_jet).
11
flies(salzburg, ciebe).
12
13
can_fly(X, Y) IF direct(X, Z) AND direct(Z, Y).
These clauses have the following meaning:
Clause
Explanation
01
There is a direct route from Edinburgh to Paris.
07
Fly Jet operates flights to Paris.
13
It is possible to fly from X to Y if there is a direct flight from X to Z and a direct
flight from Z to Y.
(a) More facts need to be included.
There is a direct flight from London to Rome and British Air flies to Rome.
14 …
15 …
[2]
(b) Using the variable Q, the goal
flies(Q, fly_jet).
returns
Q = paris, vienna
Write the result returned by the goal
flies(K, ciebe).
K = … [2]
(c) Use the variable M to write the goal to find where you can fly direct from Glasgow.
… [2]
(d) If an airline flies to an airport, that airline also flies every direct route out of that airport.
Write a rule to represent this condition.
flies(Y, X)
IF …
… [3]
(e) State what the following goal returns.
can_fly(glasgow, rome).
… [1]
Question text
5
A computer game is being developed using object-oriented programming. The following image is
a screenshot from the game.
There are scenery elements and animated elements. The player’s character is one of the animated
elements.
Each game element has the attributes:
Attribute
Description
Example value
The x coordinate of the game
PositionX
92
element.
The y coordinate of the game
PositionY
106
element.
The width of the game element.
Width
150
The height of the game element.
Height
200
The filename of the image file for
ImageFilename
GameElementFrame1.png
the game element.
Each game element has a method, GetDetails() that returns a string containing all the
element’s attributes.
The player’s character is one of a number of animated elements. All animated elements have the
attributes:
Attribute
Description
Example value
AnimationFrames
An array of GameElement
"Left"
A string giving the direction the
Direction
object is travelling in.
2000
A value for the strength that
Strength
indicates the power of the object.
100
A value for the health that
Health
indicates the health of the object.
The player’s character can either move left or right, or jump.
(a) Complete the following class diagram for the game.
You do not need to include any additional get or set methods.
GameElement
AnimatedElement
PositionX: INTEGER
AnimationFrames: ARRAY OF GameElement
PositionY: INTEGER
Width: INTEGER
…
Height: INTEGER
ImageFilename: STRING
…
…
Constructor()
Constructor()
GetDetails()
AdjustHealth()
AdjustStrength()
DisplayAnimation()
Scenery
Player
CauseDamage: BOOLEAN
DamagePoints: INTEGER
Constructor()
GiveDamagePoints()
…
…
…
…
[3]
(b) Write program code to define the GameElement class.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…
Question text
2
A declarative language is used to represent the following facts and rules about iguanas and
lizards.
01
has(reptile, cold_blood).
02
has(reptile, air_breathing).
03
has(reptile, scales).
04
05
is_a(squamata, reptile).
06
is_a(iguana, squamata).
07
is_a(lizard, squamata).
08
is_a(green_iguana, iguana).
09
is_a(cayman, iguana).
10
is_a(smooth_iguana, iguana).
11
12
maxsize(green_iguana, 152).
13
maxsize(cayman, 90).
14
maxsize(smooth_iguana, 70).
These clauses have the following meaning:
Clause
Explanation
A reptile has cold blood.
01
A cayman is a type of iguana.
09
The maximum size of a green iguana is 152 cm.
12
(a) More facts are to be included.
A gecko is a type of lizard. It has a maximum size of 182 cm.
Write the additional clauses to record these facts.
15 …
16 …
[2]
(b) Using the variable R, the goal
is_a(R, squamata).
returns
R = iguana, lizard
Write the result returned by the goal
is_a(T, iguana).
T = … [2]
(c) Write the goal, using the variable X, to find what a squamata is.
… [2]
(d) All iguanas and lizards are squamata. All squamata are reptiles.
Write a recursive rule to make all lizards and iguanas inherit the properties of reptiles.
has(X, Y)
IF
…
… [3]
(e) State what the following goal returns.
NOT(maxsize(cayman, 70)).
… [1]
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
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]