OL P2 Quick Revision
OL P2 Quick Revision
OL P2 Quick Revision
Page | 2
Page | 3
7.1 Computational Thinking Skills
Q 1) What is meant by computational thinking?
Computational Thinking - is used to study a problem and formulate an effective solution that can
be provided using a computer. There are several techniques used in computational thinking,
including abstraction, decomposition, and pattern recognition.
Q 3b) Describe need for and benefits of using abstraction, Describe the purpose of abstraction.
Abstraction involves filtering out information that is not necessary to solve a problem.
Abstraction gives us the power to deal with complexity.
The benefits of abstraction:
• the time required to develop the program is reduced so the program can be delivered to the
customer more quickly
• the program is smaller in size so takes up less space in memory and download times are
shortened
• customer satisfaction is greater as their requirements are met without any extraneous features.
1. Analysis:
Diagnosis of problem.
Identification of problem.
Abstraction.
Decomposition.
Page | 5
Identification of requirements.
A series of steps followed by the developer include:
Collection of facts: Obtain end user requirements through documentation, client interviews,
observation, and questionnaires.
Scrutiny of the existing system: Identify pros and cons of the current system in-place, so as to carry
forward the pros and avoid the cons in the new system.
Analysis of the proposed system: Find solutions to the shortcomings described in step two and
prepare the specifications using any specific user proposals.
2. Design
Identifier table is developed, variables, constants, arrays and other data structures are
decided.
Algorithm flowchart and/or pseudo code is designed.
Structure chart and state-transition diagrams are also designed.
Methods to design and construct a solution
Structure Chart
Flowchart
Pseudo code
3. Coding
Writing program code
performing iterative testing.
4. Testing
Testing program code using test data.
The program is run many times with different sets of test data, to test that it
does everything it is supposed to do in the way set out in the program design.
A Analysis ✓
B Coding
C Design
D Testing
Analysis
Coding
Design
Evaluation
testing
Q 4) (a) Design and coding are stages of the program development cycle.
State two activities from each of these stages.
Design stage
Activity 1
Activity 2
Coding stage
Activity 1
Activity 2 [4]
Page | 8
Q5) (a) The following table lists some activities from the program development life cycle.
Complete the table by writing the life cycle stage for each activity. [4]
Q 8) What is transferable skills? Explain how it helps in work on not familiar programming
language.
Transferable skills, are the abilities programmer can transfer from one language to another.
Programmer should be able to recognise / understand in another language:
• Declaration, assignment, sequence, selection, repetition (iteration)
Subroutines, Parameters passed between modules
Page | 10
program structure, Input and Output
(c) State the difference between defining and calling a function. [1]
Define a function or a procedure means set-up it, writing code to create it, while CALL means
executing a function or procedure.
Procedures (or functions) are used to enable the programmer to write a collection of programming
statements under a single identifier .
Parameters are used to pass values from the main program to a procedure / function
Parameters are used so that they can be used in the procedure / function
Parameters allow the procedure / function to be re-used with different data.
7.2 Algorithm
Q 1) Define algorithm.
Algorithm is a step by step design of a solution. Flowchart and pseudo code are two different
representations of algorithm.
TOTALLING is used to calculate running total. We can use a variable such as Total or Sum to hold
the running total and assignment statements such as:
Total ← Total + Number
(new) (old)
i.e. ADD Number to (old) Total to obtain (new) Total
IF Number>Largest THEN
LargestNumber
ENDIF
When there are two options IF statements with an ELSE clause is written as follows:
IF Marks>=50 THEN
Result “Pass”
ELSE
Result “Fail”
ENDIF
CASE statements is a conditional statement to deal with many possible outcomes.
CASE statements allow one out of several branches of code to be executed, depending on the
value of a variable.
In case selection number of statements are reduced so code become more simplified.
Executed a set number of May not be executed even Executed at least ONCE
times ONCE
REPEAT…UNTIL
Page | 23
0b Specimen P2b Q2) Programming Concepts
2 Four programming concepts and four descriptions are shown.
Draw one line to connect each programming concept to the most appropriate description. [3]
Programming concept Description
Library routine A subroutine that does not have to return a value.
Function
0b Specimen P1b-Q12) Loops – convert loop
12 (a) Rewrite this pseudocode algorithm using a WHILE … DO … ENDWHILE loop. [4]
B ← FALSE
INPUT Num
FOR Counter ← 1 TO 12
IF A[Counter] = Num
THEN
B ← TRUE
ENDIF
NEXT Counter
(c) Explain the difference between a WHILE … DO … ENDWHILE and a REPEAT … UNTIL loop. [3]
Page | 24
1b fm23 P22- Q2) Basic Concepts
2 (a) Four descriptions and five pseudocode statements are shown.
Draw one line to link each description to its most appropriate pseudocode statement. Not all
pseudocode statements will be used. [4]
Description Pseudocode statement
a statement to count FOR Count 1 TO 10
REPEAT
Using a procedure
Q 2) What is validation?
Validation is an automatic computer check to ensure that the data entered is sensible and
reasonable. It does not check the accuracy of data.
However, this does not guarantee that the number typed in is correct. For example, a student's
age might be 14, but if 11 are entered it will be valid but incorrect.
A validation check is a rule that is built into a database to check that the data entered is:
Sensible
Reasonable
Within acceptable boundaries
Complete
Page | 27
It does NOT mean that the data is actually correct, that requires verification.
Q 3) Describe different types of validation checks.
There are a number of different validation rules:
1 Type Checks – Checks that the data entered is of a given data type.
For example,to ensure only integer type of data is entered:
DECLARE Value: REAL
INPUT “Enter a value”, Value
WHILE Value <> ROUND(Value,0) DO
OUTPUT “Error! Real numbers are not allowed here, try again”
INPUT “Re-enter a value”, Value
ENDWHILE
string data entered in integer field or variable will be rejected.
2 Range checks - these are used to limit the range of data a user can enter. The 'day' part of a
date must be in the range 1 to 31. An exam grade should be in the range 'A'...'E' or 'U'.
INPUT Marks
WHILE Marks<0 OR Marks>100 DO
PRINT “Error! Re-enter marks”
INPUT Marks
ENDWHILE
3 Limit Check - Similar to Range Check but the rule involves only one limit.
For example, >=0 means reject negative numbers. Date of birth must not be later than a date.
4 Character Check - Checks that when a string of characters is entered it does not contain any
invalid characters or symbols.
For example a name would not contain digit or special character.
5 Format Check - Checks the data is in the right format. Values must conform to a specific
pattern, for example, two letters followed by six digits followed by a single letter.
A candidate number, PK-500-1234 or National Insurance number is in the form LL 99 99 99 L
where L is any letter and 9 is any number. for Date dd/mm/yyyy or mm/dd/yyyy
6 Presence checks - these simply check that an entry has been made in a particular field i.e. a
null value (empty field) is not permitted.
For example to make sure that some data has been stored in the name, it is not left empty:
INPUT "Enter name ", Name
WHILE Name="" DO
PRINT “Error! Enter name ”
INPUT "Enter name again ", Name
ENDWHILE
Page | 28
7 Length Checks - ensures that such data is either an exact length or does not exceed a
specified number of characters.
For example to make sure that password is at least 8 characters long:
INPUT "Enter password, at least 8 characters long: ", Pass
WHILE LENGTH(Pass) < 8 DO
OUTPUT "Error! Try again"
INPUT "Re-enter password, at least 8 characters long: ", Pass
ENDWHILE
8 Lookup - A lookup check takes the value entered and compares it against a list of values in a
separate table.
9 Check digits - The last one in a code are used to check the other digits are correct.
Bar code readers in supermarkets use check digits.
length check – e.g. only 30 characters in name field
character check – e.g. name doesn’t contain numeric chars
range check – e.g. day of month in date is between 1 and 31
format check – e.g. date in the form xx/yy/zz
check digit – e.g. end digit on bar code to check if it is valid
type check – e.g. integer, real
presence check = field is not left blank
2. Abnormal/Erroneous Data is the data that should cause the system to tell the user that
there is a problem with data entered into the system.
This test data should be rejected as the values are not suitable.
This type of test data is called ERRONEOUS or ABNORMAL TESTDATA; it should be rejected
by the solution.
For example to input positive numbers:
Erroneous/abnormal data:–12, eleven
Expected results: these values should be rejected
3. Extreme Data are the largest and smallest values that normal data can take.
Extreme data: 0, 100
Expected results: these values should be accepted
4. Boundary Data is the test data that is established either on the limit of acceptability or just
outside the limit of acceptability.
For example, for percentage marks in the range 0 to 100, the algorithm should be tested with
the following boundary data; at each boundary two values are required, one value is accepted
and the other value is rejected.
Boundary data for 0 is –1, 0 and for 100 is 100, 101
Expected results: –1 and 101 are rejected, 0 and 100 are accepted
Rogue Values A value that stops input is called Rogue Value.
A rogue value lets the computer know that a sequence of input values has come to an end.
Page | 31
10 Data Types and structures
Records, Arrays Files, ADT
Data type – a classification attributed to an item of data, which determines the types of value it
can take and how it can be used.
Identifier – a unique name applied to an item of data.
Record (data type) – a composite data type comprising several related items that may be of
different data types.
Composite data type – a data type constructed using several of the basic data types available in
a particular programming language.
Linear search – a method of searching in which each element of an array is checked in order.
Bubble sort – a method of sorting data in an array into alphabetical or numerical order by
comparing adjacent items and swapping them if they are in the wrong order.
File – a collection of data stored by a computer program to be used again.
Page | 32
Arrays
Arrays are data structure used to store multiple data items of same data type (homogenous)
under one identifier name.
Square brackets are used to indicate the array index.
Each element in the array is identified using its subscript or index number. The largest and
smallest index numbers are called the upper bound and lower bound of the array.
For illustration, let's take array declaration to store marks of 10 students.
Marks[10 Marks[1:10 Marks[0:9
] ] ]
Q 1) Describe The terms associated with Arrays
Name: The identifier of the array is called Array Name. E.g. StudentName[]
Element: Each data item stored in array is called element. Array can store only single types of
elements.
Size: The number elements the array can store. E.g. StudentName[1:30] can store 30 names
while StudentName[30] can store 31 names as by default it is 0 to 30.
Index: The position of each element is referred as Index Number. In the above diagram , ndex of
90 in 1D array is [3], while in 2D is [3,1].
Type: Data type of all elements in a single array have same data types.
Dimension: Dimension is the organisational structure of array. It may be 1D that has single index
or 2D that has two indexes.
Lower Bound – the smallest index number
Upper Bound – the largest index number
Page | 33
Q 2) The following diagram shows four data structures and four descriptions. [3]
Draw a line to connect each data structure to the correct description.
Data structure Description
Constant A collection of related data
Linear Search
Linear Search - is a method in which each element of the array is checked in order, from the lower
bound to the upper bound, until the item is found or the upper bound is reached.
For example, to search marks in an 1D array Marks[1:30], user has to input data to be searched.
Algorithm compares this search data with all elements of the array, starting from lower bound. If
search data matches with the element, then it displays the location of data first found and if data is
not found, it is searched at next location in the conditional loop until the data is found or it reaches
at the upper bound. Example Code for Single Searching using Linear Search
DECLARE SearchData: REAL
DECLARE Found: BOOLEAN
INPUT SearchData
Found FALSE
Index 1
WHILE Index <=30 AND Found = FALSE
IF SearchData = Marks[Index] THEN
Found TRUE
OUTPUT “Found at “, Index
ELSE
Index Index + 1
ENDIF
ENDWHILE
IF Found = FALSE THEN
OUTPUT “Not found”
ENDIF
Page | 34
For example to read 1st line from text file Teachers.txt and to store in a variable “TextLine”
OPENFILE “Teachers.txt” FOR READ
READFILE “Teachers.txt”, TextLine
PRINT TextLine
CLOSEFILE “Teachers.txt”
CLOSEFILE “Teachers.txt”
Two Files: Read data from 1st file and store in another file, for example from Teacher.txt to
staff.txt:
OPENFILE “Teachers.txt” FOR READ
OPENFILE “Staff.txt” FOR WRITE
CLOSEFILE “Teachers.txt”
CLOSEFILE “Staff.txt”
Page | 39
Binary Logic
INPUT OR AND NAND NOR XOR
A B
X=¿
X= A +B X= A . B X= A . B X= A +B
( A . B ) +( A . B)
0 0 0 0 1 1 0
0 1 1 0 1 0 1
1 0 1 0 1 0 1
1 1 1 1 0 0 0
XOR Gate gives output only when BOTH input are different.
If A = 1 OR B = 1 BUT NOT BOTH then output will be 1.
IF BOTH input are 0 then output will be 0.
IF BOTH input are 1 then output will be 0.
IF BOTH input are same output will be 0.
Page | 40
Draw a logic circuit corresponding to the logic statement: [3]
X = (A is ON OR B is ON BUT NOT BOTH) OR (NOT(C is ON AND A is ON))
A B C Working space X
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Page | 41
Write logic expression and complete the truth table for the following logic circuit:
A B C Working space X
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Page | 42
7 Consider the truth table:
A B C X
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
1 1 1 1
(a) Draw a logic circuit to represent the given truth table.
Each logic gate should have maximum of two inputs.
Do not simplify the logic circuit. [6]
B X
An attribute is a feature of that entity. For example, a hotel room might have an attribute about
whether it has a view or whether it is single or double. A student might have a date of birth and an
address.
An entity is stored as a table in a database and an attribute becomes a field in a table.
All the data about a particular entity is stored in a single table. Each data item about the entity is a
field.
Page | 44
Data types
Different data types are identified so that a computer can store and process the data appropriately.
Data types include:
Text/Alphanumeric (STRING or CHAR in programming and pseudo code)
Character (a singe character)
Number (numeric) may include:
o Auto number
o Currency
Date/Time
Boolean (or Yes/No).
Primary Keys
Each table has a primary key. This is a field chosen so that it can uniquely identify each record.
SQL - Structured Query Language (pronounced as siːkwəl or "seequel") is the most common
language for extracting and organising data that is stored in a relational database.
SQL is the language of databases. It facilitates retrieving specific information from databases that
are further used for analysis.
SELECT Command
SELECT Command tells the database what information user wants to retrieve followed by FROM to specify which
table.
WHERE statement specifies a condition that has to be met before record is returned
ORDER BY DESC places data in descending order...
The SELECT statement allows you to ask the database a question (Query it), and specify what
data it returns. We might want to ask something like
Tell me the name and class of all the students.
The structure of SELECT Command is given below:
SELECT <Field names>
FROM <Table name>
WHERE <condition>
ORDER BY <ASC or DESC>
Take a look on following Student table.
RollNo FirstName LastName DoB Class Section Points
101 Osman Zaheer 12/05/2005 AS K 10
102 Wajid Ali 05/08/2000 A2 G 8
103 Rehan Ghayas 07/02/2003 A2 S 10
104 Jane Oliver 22/08/2005 AS T 5
105 Abdullah Patel 11/11/2009 OL K 10
106 Muzna Marea 14/07/2006 AS K 10
To select all the items from this table we can use:
SELECT *
FROM <Table>
Page | 45
SELECT *
FROM Student;
This would display all the Name and class of students who are in section K and class is AS.
(g) Write SQL script to count number of books whose stock level is more
than 100.