Computer Science Paper 1 #RS421 (By Ryan Sutton)
Computer Science Paper 1 #RS421 (By Ryan Sutton)
4.1.1 Programming -
4.1.1.1 Data types (AS) -
● Understand the concept of a data type. ✓
Data type
Determines what sort of data is being stored and how it will be handled by the program.
● Variables declared as a pointer or reference data type are used as stores for memory addresses
of objects created at runtime, ie dynamically. Not all languages support explicit pointer types, but
students should have an opportunity to understand this data type.
● Define and use user-defined data types based on language-defined (built-in) data types.
User defined data type
A customizable data type which is used to extend current data types or create new ones.
4.1.1.2 Programming Concepts (AS) -
● Use, understand and know how the following statement types can be combined in programs:
○ Variable declaration assignment
Variable Declaration - A data item whose value will change during the execution of the
program. A variable declaration is setting such variable, declaring its name and contents -
changing (or creating) a state for it.
○ Constant declaration
Constant Declaration - An item of data whose value does not change with the program. The
declaration of such variable is initial, and final.
○ Assignment
Assignment - The process of giving a value to a variable or constant.
○ Iteration
Iteration - ‘For’ or ‘While’ Loops. Repeating the same process several times in order to
achieve a result.
○ Selection
Selection - ‘If Statement’ - The principle of choosing what action to take based on certain
criteria.
○ Subroutine (procedure/function).
Subroutine (Procedure/function) - Function is fun, returns a value. Procedures don’t return
values. A named block of code designed to carry out a specific task.
● Use definite and indefinite iteration, including indefinite iteration with the condition(s) at the start
or the end of the iterative structure. A theoretical understanding of condition(s) at either end of an
iterative structure is required, regardless of whether they are supported by the language being
used.
● Use meaningful identifier names and know why it is important to use them
Meaningful identifier names = Understandable names that name what they store. E.g ‘Number’
for an integer.
Importance -
● Variables easily traced through scopes
● Code is easily understood - Variables names detail what could be stored and potentially
its use.
All in python 3.
● Be familiar with and be able to use:
○ Addition
Num.1 + Num.2
○ Subtraction
Num.2 - Num.1
○ Multiplication
Num.1 * Num.2
○ Real/float division
Num.2 / Num.1
○ Exponentiation
Num.1 ** Num.2
○ Rounding
round(Num.1)
Import math
math.floor (3.5) #Output 3.0
math.floor (-3.5) #Output -4.0
math.ceil(3.5) # Output 4.0
math.ceil(-3.5) # Output -3.0
○ Truncation.
import math
math.trunc(3.5) # Output of 3
Runtime Error -
Occurs when the program is running.
● Know how to use exception handling in a programming language with which students are
familiar.
Exception Handling -
Part of a robust program. In order to disallow crashing of programs through unexpected inputs,
this is used.
If no other way of testing exception handling can be used; the ultimate method is...
In python:
Try
Except
Exception handling can allow for error messages, to inform and possibly, to avoid fatal crashes.
(a) Look at the main program. Explain what the following statement does:
IF option = "1"
saveToFile("write")
If the user inputs 1 as their option. The program will run the saveToFile subroutine with “write” as
it’s openMode parameter being passed in. IWhen running the subroutine, it will open the
studentNamesfile.txt in write mode, the user will be asked for a name of a student and then to
enter their mark, saving this name and mark as a record within the file opened previously. There is
a while loop to iterate this process until the user enters “xxx”. The user also has the option to enter
“xxx” as the original input and go back to the main menu.
(b) In the subroutine saveToFile, what mode is the file opened in? Where is the file saved?
As said before, the file is opened in write mode and the file is saved as “studentNamesfile.txt”
● Know that a subroutine is a named ‘out of line’ block of code that may be executed (called) by
simply writing its name in a program statement.
● Be able to use local variables and explain why it is good practice to do so.
For these reasons:
● It reduces the complexity of the code - allowing variables to be more easily traced as there
is only one iteration of it within the code - rather than many iterations used in many places.
● It adds ‘complexity’ over the whole program; instead of over one subroutine.
● It stops the big problem where changing one subroutine causes damage to another.
● Be able to explain how a stack frame is used with subroutine calls to store:
○ Return addresses
○ Parameters
○ Local variables.
Stacks can Push, Pop, Peek or top, Test for empty stack, Test for stack full.
Parameters and return addresses. 2/2
A single stack can be used through the usage of stack frames. The first action (1) is stored in a
stack with it’s parameters, return address, it will then run the subroutine and do it’s function,
storing local variables on the way. When another subroutine is called, it will push (1) this onto the
stack and run it’s respective code, storing all the same variables in the stack frame.
For instance, if the user chooses to run repeat, repeat, undo, repeat. The stack frame will push 1
stack of repeat, run it’s code and store variables, then run another repeat, then when undo is
called, the code will run for its respective subroutine and push it onto the stack, however what can
be done is that the undo function can traverse the stack frame or peek at it and refer back to 2
subroutines ago (the first repeat call) and alter the state of the software in order to go back to that
first stack, or it can pull the top subroutine call in the stack essentially undoing anything that the
second repeat call did. The software then runs another repeat call, pushing it onto the stack on
top of the first, leaving the code with two repeat calls altogether in the stack frame. The code will
then run the two repeat calls accordingly and pull them off the stack when they’re operation is
done. x
1)
a) Hierarchical structure ~
No Loops v
b) A loop, e.g a corridor that leads back to the start of the maze (e.g one of the 6 corridors
leading back to 1). v
c)
4.1.2 Programming paradigms
4.1.2.1 Programming paradigms
● Understand the characteristics of the procedural- and object-oriented programming paradigms,
and have experience of programming in each.
Inheritance = ‘is a’
Plane ‘is a’ form of transport
Association = ‘has a’
Student ‘has a’ Teacher
Player ‘has a’ Coach
Independent in association.
○ Polymorphism
The change of a property that has been inherited within a subclass.
○ Overriding.
(The action of polymorphism)
Dynamic polymorphism, subclass provides a specific implementation of a method that is
already provided by a parent class
Object:
Student
Attributes:
- Name
- Age
- Gender
- Height
- Nationality
- Weight
Methods:
- Raise()
- Walk()
- Sit()
- Toilet()
- Sing()
- Eat()
■ Polymorphism.
Data Structures - A common format for storing large volumes of related data, which is an
implementation of an abstract data type.
Mindmap
Data Structure - A common format for storing large volumes of related data, which is an
implementation of an abstract data type.
Abstract data type - A conceptual model of how data can be stored and the operations that can
be carried out on the data.
● Be able to distinguish between static and dynamic structures and compare their uses, as well as
explaining the advantages and disadvantages of each.
Static - Size doesn’t change.
4.2.2 Queues
4.2.2.1 Queues
G-Site
Presentation
● Be able to describe and apply the following to linear queues, circular queues and priority queues:
○ Add an item
○ Remove an item
○ Test for an empty queue
○ Test for a full queue.
Queue - An abstract data structure that follows a first in first out (FIFO) order. Data leaves in the
order it arrives.
A queue may be used when a peripheral wants to send data to a CPU. However, it may not be
ready for such information and therefore may temporarily store the data in a queue.
Actions:
● enQueue(item)
○ Always check to see if queue is full → return error.
● deQueue
○ Always check to see if queue is empty → return error.
● isEmpty
● isFull
Linear Queue
● Two pointers: one at the front and one at the end (pointing to the index of the last stored item).
● Has a maximum size.
● Typically implemented as a 1D array.
Circular Queue
- A FIFO data structure implemented as a ring where the front and rear pointers can wrap
around from the end to the start of the array.
Commonly used when buffering, when items of data need to be stored temporarily while they are
waiting to be passed to/from a device.
A circular queue is full when the tail pointer is 1 less than the front pointer.
Priority Queue
- A variation of a FIFO structure where some data may leave out of sequence where it has a
higher priority than other items.
● Adds a further element to the queue which is the priority for each item.
For example if documents are being sent to print on a network printer then it might be possible for
the user or systems manager to control the queue in some way.
Depending on how the priority queue is implemented, the enqueue and dequeue may pick an
element in the middle of the array due to priority.
4.2.3 Stacks
4.2.3.1 Stacks
G-Site
Presentation
● Be able to describe and apply the following operations:
○ Push
○ Pop
○ Peek or top
○ Test for empty stack
○ Test for stack full.
■ Peek or top returns the value of the top element without removing it.
Stack - A LIFO (Last in, First out) structure where the last item of data added is the first to leave.
One pointer on the top of the stack
Stacks can overflow (full + adding 1 more) and underflow (empty + taking away 1).
Actions:
● push(item) – adds item to the top of the stack.
○ Always check to see if stack is full → report an error.
● pop() – removes and returns the item on the top of the stack.
○ Always check to see if stack is empty → report an error.
● isFull() – checks if the stack is full.
● isEmpty() – checks if the stack is empty.
● peek() – return the top item without removing it.
● size() – return the number of items on the stack.
Stack Frames
Stack Frame - A collection of data about a subroutine call - a system level data structure.
4.2.4 Graphs
4.2.4.1 Graphs
G-Site
Presentation
● Be aware of a graph as a data structure used to represent more complex relationships.
● Be familiar with typical uses for graphs.
● Be able to explain the terms:
○ Graph
○ Weighted graph
○ Vertex/node
○ Edge/arc
○ Undirected graph
○ Directed graph.
● Know how an adjacency matrix and an adjacency list may be used to represent a graph.
● Be able to compare the use of adjacency matrices and adjacency lists.
Graph - A mathematical structure that models the relationship between pairs of objects.
Weighted Graph - A graph that has a data value labelled on each edge.
Vertex/node - An object in a graph.
Edge/Arc - A join or relationship between two nodes.
Undirected graph - A graph where the relationship between vertices is two-way
Directed graph - A graph where the relationship between vertices is one way.
Uses of graphs:
● Modelling:
○ Human networks (social, family, work). E.g LinkedIn.
○ Transport Networks.
○ Internet and web.
○ Computer science: Latency is a big factor so a graph could be used to calculate
the quickest path to send data around a microprocessor.
○ Medical Research: Diseases.
○ Project management.
○ Game theory.
○ Djikstra’s algorithm.
Graph (Undirected)
Weighted Graph
Directed Graph
Adjacency matrix
0’s where the blanks are.
● Advantage: an adjacency matrix is convenient to work with, and adding an edge is simple
● Disadvantage: a sparse graph with not many connections (edges) will leave most of the cells
empty, wasting a lot of memory space
Adjacency list
● It only uses storage for the connections that exist, so it is more (memory) space-efficient.
● It is a good way of representing a large, sparsely connected graph.
4.2.5 Trees
4.2.5.1 Trees (including binary trees)
G-Site
Presentation
● Know that a tree is a connected, undirected graph with no cycles.
○ Note that a tree does not have to have a root.
● Know that a rooted tree is a tree in which one vertex has been designated as the root. A rooted
tree has parent-child relationships between nodes. The root is the only node with no parent and
all other nodes are descendants of the root.
● Know that a binary tree is a rooted tree in which each node has at most two children.
○ A common application of a binary tree is a binary search tree.
● Be familiar with typical uses for rooted trees.
Tree - A data structure similar to a graph, with no loops (hierarchical). A tree does not have to have
a root.
Root - The starting node in a rooted tree structure from which all other nodes branch off.
Parent - A type of node in a tree where there are further nodes below it.
Child - A node in a tree that has nodes above it in the hierarchy.
Leaf - A node that does not have any other nodes beneath it.
Uses:
● Store data that has an inherent hierarchical structure. OS may use trees for directories,
files and folders in it's file management system.
● Process the syntax of statements in natural and programming languages, so they are
commonly used when compiling programming code.
Advantages:
● Dynamic, easy to delete and add nodes.
● Easy to search with standard traversal algorithms.
Disadvantages:
● Trees need to be balanced (height of the tree is kept to a minimum( to conserve space.
Rooted Tree
Binary Tree
Hashing algorithm - Code that creates a unique index from given items of key data.
The Address is calculated with the hashing algorithm and not the data itself. Large data sets can
be organised
Uses:
● Databases - used to create indices for databases enabling quick storage and retrieval of
data.
● Memory addressing: Used to generate memory addresses where data will be stored. E.g
in cache
● Operating Systems: Storage of exe files, programs and utilities.
● Encryption: Used to encrypt data ← hashing algorithm must be complex.
● Checksums.
● Programming: Keywords and identifiers for the compiler.
Collisions - When a hashing algorithm produces the same index for two or more different keys.
Clustering - When a hashing algorithm produces indices that are not randomly distributed.
A hashing algorithm must also be able to generate as many indices are there are elements of
data.
Load factor - The ratio of how many indices are available to how many there are in total. Number
of keys divided by the number of slots.
Chaining - A technique for generating a unique index when there is a collision by adding the
key/value pair to a list stored at the same index. A linked list can be used.
Rehashing - The process of running the hashing algorithm again when a collision occurs (as an
alternative, another subsidiary hashing algorithm can be used.
4.2.7 Dictionaries
4.2.7.1 Dictionaries
G-Site
Presentation
For example, the document 'The green, green grass grows' would be represented by the
dictionary:
Dictionary - A data structure that maps keys to data. Also known as an associative array.
The dictionary address for each key:value pair can be calculated using a hashing algorithm - it
most times, is.
Uses:
● In a computer system: ASCII/ Unicode tables.
● In a translation program: foreign language dictionary containing words and meanings.
● Other data structures may be implemented using dictionaries.
Actions:
● Add a new key:value pair to the dictionary
● Delete a key:value pair from the dictionary
● Amend the value in a key:value pair
● Return the value associated with key k
● Return True or False depending on whether the key is in the dictionary
● Return the length of the dictionary
4.2.8 Vectors
4.2.8.1 Vectors
G-Site
Presentation
● Be familiar with the concept of a vector and the following notations for specifying a vector:
○ [2.0, 3.14159, -1.0, 2.718281828].
○ 4-vector over ℝ written as ℝ4.
○ Function interpretation
■ 0 ↦ 2.0
■ 1 ↦ 3.14159
■ 2 ↦ -1.0
■ 3 ↦ 2.718281828
■ ↦ means maps to
○ That all the entries must be drawn from the same field, eg ℝ.
■ A vector can be represented as a list of numbers, as a function and as a way of
representing a geometric point in space.
Vector - A mathematical quantity that has both magnitude (size) and direction.
Vectors can be represented and applied in various ways. Mathematically and geometrically. E.g:
● As a data structure.
● As a method for mapping one value to another
● As a method of defining geometric shapes.
Uses:
● Vector graphics and 3D graphics.
● Computer games / simulations.
● Multiple dimensions.
2-vector-
3 - vector -
Vector Addition -
Just doing a + b will get the vector in between the two (from 0) - which also happens to be the
co-ordinate.
Scalar-vector multiplication -
In essence multiplying a vector by x amount of times. Increasing the length of that vector by x
amount of times.
Dot product -
A . B = AxBx + AyBy
Above only applies for 2-vectors - The same repetition applies for larger vectors.
Only produces 1 number.
Convex Combination -
A method of multiplying vectors that produces a resulting vector within the convex hull (grey
space).
w = αu + βv
Where:
● α,β >= 0
● α+β=1
Parity bit
Vectors can be used to calculate parity bits with a bitwise AND and XOR combination.
4.3.2 Tree-traversal
4.3.2.1 Simple tree-traversal algorithms
Recursion
n! = n x (n-1)!
Even with a stopping condition, the recursive routine can only be called a limited number of times
or the stack will overflow the maximum memory capacity.
Infix notation:
A+B
‘ + ‘ part (the notation) is ‘fixed in’ between the operands
2*A+B
is ambiguous
3*(1+2)
● Be able to express the solution to a simple problem as an algorithm using pseudo-code, with the
standard constructs:
○ Sequence
For loop
○ Assignment
Variable declaration/ assignment
○ Selection
If statement
○ Iteration.
While loop
● Be able to convert an algorithm from pseudocode into high level language program code.
● Be able to articulate how a program works, arguing for its correctness and its efficiency using
logical reasoning, test data and user feedback.
● Be familiar with the concept of abstraction as used in computations and know that:
○ Representational abstraction is a representation arrived at by removing unnecessary
details
○ Abstraction by generalisation or categorisation is a grouping by common characteristics
to arrive at a hierarchical relationship of the 'is a kind of' type.
Explains itself.
● Be familiar with the process of hiding all details of an object that do not contribute to its essential
characteristics.
Writing procedures and passing parameters so that variables are locally stored and taken away
from the main program.
● Know that for functional abstraction the particular computation method is hidden.
○ The result of a procedural abstraction is a procedure, not a function. To get a function
requires yet another abstraction, which disregards the particular computation method.
This is functional abstraction.
Using a function does not need specific details on how the computation works, only information
on how to call the function and then what parameters to pass - and therefore that is the ‘further
abstraction’.
● Know that details of how data are actually represented are hidden, allowing new kinds of data
objects to be constructed from previously defined types of data objects.
○ Data abstraction is a methodology that enables us to isolate how a compound data
object is used from the details of how it is constructed.
○ For example, a stack could be implemented as an array and a pointer for top of stack.
Q1-
An algorithm is to be written to find the shortest route between two points. How can
abstraction be applied to this problem?
What applications could such a program be useful for?
Abstraction can be applied by finding parts of code which extend the distance between the two
points or unneeded bits of coding that make the program function irregularly. The most significant
variables can be put into a stack or procedural abstraction can be used to allow for the inputting
of the parameters and sorting in subroutines.
Q2 -
Give examples of some problems which can be “solved” by computer simulations
Virtual viewings of buildings. Crime scene rebuilding. Some simulations of trajectories can be
visualised as well as graphing quickly can be solved and previewed easily with computer
programs.
Q3 -
What factors would be relevant in a financial model which calculates the likely annual profit
in a new coffee shop?
The parameters given to the code by the user - such as price of coffee, number sold, cost of
supplies/ resources/ bills/ tax, etc. Other things that allow for the calculations to happen and the
structuring of the code would also be relevant in the program.
Irrelevant factors would be things such as colour of coffee cup or variables such as that. As well
as environmental factors such as weather.
Q4-
You have been asked to write a procedure to count the number of vowels in a sentence.
How can you ensure that the procedure will work for any length of sentence?
Make an open, unlimited length capable array and treat is like a stack. Append each character
within the sentence to the array and check for ‘a’,’e’,’i’,’o’ or ‘u’. This will work for any length of
sentence.
Q5 -
To implement a stack, you would need three procedures: InitialiseStack(stack, pointer),
AddToStack(stack, pointer, item), RemoveFromStack(stack, pointer, item).
The identifiers in brackets are parameters defined in the main program and passed to the
Procedure.
What procedures and parameters would you need to be able to implement a queue?
Q6-
A car dealer might use a procedure for displaying different models of a particular make of
car. The user can specify what model they are interested in, how many doors the car
should have, the paint colour, wheels, and interior specifications.
See http://www.volkswagen.co.uk/configurator
Draw a hierarchy chart to show how the procedure for displaying a particular model might be
broken down into separate tasks and subtasks.
What type of abstraction is being used in this problem?
Procedural Abstraction is being used as procedures are utilised accepting human inputs in them.
Accept(ing) State -
The state that identifies whether an input has been accepted.
● Possibly one or more.
● Usually double circles.
Transition Arrows -
● Join Transitions, allowing movement
● Directional arrows
Transition Condition -
● Each arrow must have a condition.
● This is the input which causes a move in states. (Following the direction of the arrow)
● 0,1 means 0 or 1 accepted.
Mealy Machine -
A finite state machine with an output.
● Outputs are determined by both: current state and current input.
● As with standard FSM for each given state and input there can only be one possible
transition.
4.4.2.2 Maths for regular expressions
Link
● Be familiar with the concept of a set and the following notations for specifying a set:
A = {1, 2, 3, 4, 5 }
or set comprehension:
A = {x | x ∈ ℕ ∧ x ≥ 1 }
where A is the set consisting of those objects x such that x ∈ ℕ and x ≥ 1 is true.
Know that the empty set, {}, is the set with no elements.
○ A set is an unordered collection of values in which each value occurs at most once.
x ∈ ℕ means that x is a member of the set ℕ consisting of the natural numbers, ie {0, 1, 2,
3, 4, … }.
The set of natural numbers, ℕ and the set of real numbers, ℝ are examples of infinite sets.
A countably infinite set is one that can be counted off by the natural numbers.
The set of real numbers is not countable. The cardinality of a finite set is the number of elements
in a set. Cartesian product of two sets, X and Y, written X x Y and read 'X cross Y', is the set of all
ordered pairs (a, b) where a is a member of A and b is a member of B.
⊆ includes both ⊂ and =, for example {0, 1, 2, 3 } ⊆ {0, 1, 2, 3 } is also true, because
{0, 1, 2, 3 } = {0, 1, 2, 3 }. A countable set is a set with the same cardinality (number of
elements) as some subset of natural numbers.
● Be familiar with the set operations:
○ Membership
○ Union
○ Intersection
○ Difference.
■ The set difference A\B (or alternatively A-B) is defined by
A\B = {x : x ∈ A and x ∉ B}.
Where a set is finite it has cardinality, meaning that it can be counted using natural numbers.
Cardinality - The number of elements in a set.
Countably infinite sets - Sets where the elements can be put into a one-to-one correspondence
with the set of natural numbers.
Difference - describes which elements differ when two sets are joined together
Proper subset - Where one set is wholly contained within another and the other set has additional
elements.
● Be able to form and use simple regular expressions for string manipulation and matching.
○ Students should be familiar with the metacharacters:
○ * (0 or more repetitions)
○ + (1 or more repetitions)
○ ? (0 or 1 repetitions, ie optional)
○ | (alternation, ie or)
○ ( ) to group regular expressions. Any other metacharacters used in an exam question will
be explained as part of the question.
a|b|c a or b or c a
b
c
(a|b)c a or b and c ac
bc
. Effectively a wildcard and matches any .ole would match to mole, hole, vole,
character etc.
[] Matches to a single character within [mh]ole would match mole and hole but
the brackets not vole.
[^] Matches to any character except those [^m}ole would match hole and vole but
in brackets not mole.
* Matches the preceding characters m*ole would match ole, mole, mmole,
zero or more times mmmole, etc.
[m,n] Matches the preceding character at a[2,5] would match aa, aaa, aaaa, and
least m but not more than n times aaaaa.
2 a’s to 5 a’s inclusive of both 2 and 5
Backus-Naur Form (BNF) - A form of notation for describing the syntax used by a programming
language.
Set - A collection of symbols in any order that do not repeat.
Terminal - in BNF, it is the final element that requires no further rules.
Syntax Diagram - A method of visualising rules written in BNF or any other context-free language.
BNF -
BNF produces a set of acceptable strings, which effectively describe the rules of the language.
This shows that an integer is defined as either a digit or a digit followed by another integer.
A digit ends on a terminal; numbers 0 to 9 as there is no further rule needed to define digits.
This expression would be recursive.
Syntax Diagrams -
Map directly to BNF.
Syntax diagrams are modular so there are likely to be many diagrams required to represent a
whole language.
Another example
4.4.4 Classification of algorithms
4.4.4.1 Comparing algorithms
● Understand that algorithms can be compared by expressing their complexity as a function
relative to the size of the problem. Understand that the size of the problem is the key issue.
Procedural abstraction is used to keep the actual values used in a computation separate from the
overall design.
• Procedural abstraction is used to keep the actual values used in a computation separate from the
overall design
• In simple terms, this involves writing procedures and passing parameters (parameter is an input
variable used in a subroutine, which refers to a piece of data.)
• A car dealer might use a procedure for displaying a particular model of a car, and pass parameters
for colour, number of doors, wheels etc.
A function is used to stop the need to reuse a line of code/many to ‘calculate a square root’ or
‘generate a random number’
Data that is key to solving the program is kept within an abstract data structure - array, stack or
queue.
A stack could be implemented as an array and a pointer for the top of stack.