Programming (PAPER 2) Flashcards
What are the five main data types
- What are the characteristics of each
Integer - Whole Numbers
Float / Real - Numbers with decimals
Boolean - One of two values, usually True or False
Character - A single letter, number or symbol
String - Used to represent text, is a collection of characters
How much memory is used for each main data type
Integer - 2 or 4 bytes
Float / Real - 4 or 8 bytes
Boolean - 1 bit needed but 1 byte is usually used
Character - 1 byte
String - 1 byte for every character in the string
What are the benefits of using the correct data type (3)
more memory efficient, robust, predictable
What is casting
- What are the five commands that do this
using functions to manually convert between data types
- int (), real (), float (), bool (), str ()
What function finds the ASCII number of characters (and vice versa)
ASC (), CHR ()
What are the 7 basic arithmetic operators (programming)
- What are the operators used for each
Addition, Subtraction, Division, Multiplication, Exponentiation, Quotient, Modulus
> +, -, /, *, ^ or **, DIV, MOD or %
What is the quotient
- What is the Modulus
the whole number value after diving a number e.g. 20 DIV 3 = 6
- the remainder e.g. 20 MOD 3 = 2
What two data types do Basic Arithmetic Operators work with
real or integer (or both)
What is the assignment operator
- What does it assign
=
- values to constants or variables
What do comparison operators do
compare the value on the left to that on their right and produce a True / False value (Boolean)
What are the 6 comparison operators
- What do they mean
== is equal to
<> or != is not equal to
< is less than
> is greater than
<= is less than / equal to
>= is more than / equal to
How can data values be stored
as constants or variables
Where is the name of a constant or variable linked to
- What does the size of this depend on
A memory location
- the data type
What is a constant
a value which has been assigned at design time and can’t be changed
What is a variable
can change value at any time
What are the standard naming conventions that programmers follow when naming constants and variables (2)
lower case first letter
no spaces
How are strings written
- What is the name for merging strings
- Which operator is this done with
- double quotes, but occasionally single quotes
- concatenation
- operator
How are the characters in a string usually numbered
starting from 0
What are the 6 common string manipulations
- What do each of them do
- What are these special functions known as
x.upper, x.lower, x.length, x.left(i), x.right(i), x.substring(a, b)
- Changes all characters in string x to upper case,
changes to lower case,
returns number of characters in string x,
Extracts first i characters from string x,
Extracts last i letters,
Extracts a string starting at position a with length b form a
- methods
What are substring, left and right methods also examples of
slicing - part of a string is extracted and returned as a new string
What does an IF statement allow for
allows you to check if a condition is true or false, and carry out different actions depending on the outcome
What is the usual structure for an IF statement
IF-THEN-ELSE
What is a nested If statement
- What do these allow you to do
an IF statement inside another one
- check more conditions once established that the previous one is true
What is the difference between IF-ELSEIF and nested IF statements
IF-ELSEIF statements only check more conditions if the previous condition is false
What is the alternate name for SWITCH statements
- What do these do
CASE SELECT
- check if a variable has specific values
What is written at the end of an IF and SWITCH statement
- endif
- endswitch
When are SWITCH statements used
when you want a program to perform different actions for different values of the same variable
What is written to introduce an IF or SWITCH statement
- if … then
- variable = …
switch variable:
What are the benefits and drawbacks of using Switch statements over IF statements
- Easier to maintain. neater to test different values of a variable
- only check the value of one variable, IF-ELSEIF statements can check if multiple conditions are true
What do FOR loops do
repeat the code inside them a fixed number of times
What three values do the number of times a code repeats depend on in a FOR loop
initial value, end value and step count
If no step count is provided for a for loop, what will it automatically be
1 step
With what code can the number of times the loop repeats can also be set as the program runs
for k = 1 to x
(x is a variable)
between which two words is the FOR loop repeated
for and next
What is a FOR loop concluded with
next k
What are the 4 differences between DO UNTIL and WHILE loops
DO UNTIL:
- controlled by condition at end of loop
- keeps going until condition is true
- always runs code within at least once
- infinite loop if condition is never true
WHILE:
- controlled by condition at start of loop
- keeps going while condition is true
- never run code inside if condition is initially false
- infinite loop if the condition is always true
When does a DO WHILE loop keep going
while the condition is true and always runs the code inside at least once
What are logic gates
- What are the three steps of what they do
special circuits built into computer chips
- receive binary data
- apply a Boolean operation
- output a binary result
What is written to show logic gates and circuits
logic diagrams
What are the three types of logic gate
AND, NOT, OR gates
How many inputs and outputs are taken and given by each type of logic gate
NOT - single input, single output
AND - 2 inputs, 1 output
OR - 2 inputs, 1 output
What is outputted through a NOT gate
the opposite value - if 1 is inputted, 0 is outputted and vice versa
What is outputted through an AND gate
if both inputs are 1, the output is 1
else the output is 0
What is a truth table
a table that shows all possible inputs and their respective outputs
What is outputted through an OR gate
if one or more inputs are 1, then the output is 1
else the output is 0
What are the expressions and notations for each type of logic gate
- What are the alternative notations for each logic gate
NOT | NOT A | ¬A
AND | A AND B | A^B
OR | A OR B | A V B
- NOT !
- OR ||
AND &&
What is the NOT gate symbol
a triangle pointed to the right followed by a small circle
Which side of the gate is the input shown
left side
What is the AND gate symbol
D but stretched
What is the OR gate symbol
similar to an arrowhead - point on the right and 2 points on the left
How can multiple logic gates combined be written as a logical statement
brackets and NOT, OR and AND
e.g. NOT (A & B)
What is a two level logic circuit
a logic circuit that requires the inputs to pass through a maximum of 2 logic gates to reach the output
How many rows are required for a truth table
2^n, where n is the amount of different inputs
In what order are Boolean operations carried out
brackets, NOT, AND, OR
What is the function to generate a random number (OCR Exam Reference Language)
random (x, y)
How could a game of heads or tails be simulated with code
random number generator between 0 and 1, if one then heads, if 0 then tails
How does the response of a random number generator depend on the bounds
if the bounds are integers, then the result will be an integer
if the bounds are real numbers then the result will be a real number
How do random number generators useful for arrays
a random number can be generated and the element in that position of the array
What is an array
a data structure that can store a collection of data values under one name
What are 4 types of data structure
Records
Arrays
Files
Databases
What is each piece of data in an array called
- How can they be accessed
an element
- using its position / index in the array
What are one-dimensional arrays the same as
lists
What are the lines of code required to create a one-dimensional array of 5 employees
array employees [5]
employees [0] = “abc”
employees [1] = “def”
employees [2] = “ghi”
employees [3] = “jkl”
employees [4] = “mno”
What is the line of code required to retrieve the name of the first employee in a list of employees in a one-dimensional array
print (employees[0])
How can you change an element in a one dimensional array
reassigning the array position to a different data value
arrayname [position] = “newvalue”
What are two dimensional arrays
a list of lists - a one-dimensional array where each element is also a one dimensional array
How is the position in a one-dimensional array written
[a, b] or [a][b] where a is the column and b is the position in the row
How can two-dimensional arrays be visualised
tables or grids
What are the three most common types of text files
.txt .csv .dat
What is the command for opening a text file and assigning a variable
- What is the command for closing the file
file = open(“file.txt”)
- file.close()
What is the command for creating a new file
newfile(“myFile.txt”)
What is the command for writing text to a file
- where is this written
file.writeLine()
- at the end
What is the command for reading lines of text from a file
- where does it start
- what happens at the end
file.readLine()
- at the first line
- keeps its place in the file - like a cursor
What does the endOfFile() command do
- How can it be used in code
- What is the code for this
signifies the end of the file
- to signify when a program should stop reading the file
- while NOT file.endOfFile()
What is a record
- what makes them useful over arrays
a type of data structure - a row of data
- they can store values with different data types
What is each item in a record called
- what is given to each item when it is created
a field
- a name and a data type
What is a con of records
they are fixed in length
What is the command for creating a record structure with the fields:
record name recipes
- recipe number
- recipe name
- tested
- What is the code for inputting:
- number 1
- chocolate cake
- tested
record recipes
int recipeNumber
string recipeName
bool tested
endrecord
recipe1 = recipes (1, “Chocolate Cake”, True, 3)
How can records with the same record structures be collected
in an array
What does SQL stand for
- what is it used for
Structured Query Language
- used to search tables, usually in a database, for specific data
What is the SELECT command followed by (SQL)
SELECT fieldname FROM nameOfTable/Tables
What is written as a wildcard to SELECT all the fields (SQL)
SELECT *
What is WHERE used for (SQL)
- Where is the WHERE statement used
filtering the results - specify conditions that a record must satisfy before it is returned
- after the database name
What are procedures
sets of instructions stored under one name
What is the difference between procedures and functions
functions always return a value
What are the benefits of using programs and functions (2)
- cut down on code needing to be written
- give the program more structure and readability
What are parameters
- What can be specified for each parameter
special variables used to pass values into a sub program
- name, data type, default value
What are arguments
actual values that the parameters take when the sub-program is called
How are procedures called
by typing their name and giving an argument if necessary
Do functions and procedures have to take parameters
Procedures - no
Functions - yes
What should be done to a function once it is called
- what will happen if it isn’t done
assigned to a variable or used in a statement
- the value returned will not be stored and will be lost
How are procedures begun and ended
procedure abc() or procedure abc (parameter)
- endprocedure
How is a function introduced and ended
function xyz
endfunction
What does the scope of a variable show
- what are the two scopes of a variable
which parts of the program the variable can be used in
- local and global
What are local variables
variables that can only be used within the structure they are declared in
What are global variables
variables that can be used any time after their declaration
What type of variable are ones that are declared inside a sub-programs
local variables
What are 3 benefits of using local variables
- can’t affect anything outside the sub-program
- not affected by anything outside the sub-program
- can use the same variable name as a local variable elsewhere in the program
How are variables in the main body of a program made into global variables
with the ‘global’ keyword