Additional Programming Techniques Flashcards
All 44 cards in this deck
In OCR Exam Reference Language, how do you join two strings together?
e.g. join
firstNameandsurnamewith a space between themWith the
+operator (concatenation).fullName = firstName + " " + surnameHow do you find the number of characters in a string?
e.g.
word = "computer"word.length— this gives8.What does
.substring(x , i)return?e.g.
subject = "computer",subject.substring(3,5)icharacters starting at indexx, where indexes start at 0 — returns"puter".What do
.left(i)and.right(i)return?e.g.
word = "computer", thenword.left(3)andword.right(2).left(i)gives the firsticharacters and.right(i)the lasticharacters —"com"and"er".What do
.upperand.lowerdo?e.g.
"Cat".upper.upperconverts every letter of the string to uppercase and.lowerconverts every letter to lowercase — gives"CAT".What do
ASC()andCHR()do?e.g.
ASC("A")andCHR(66)ASC()returns the character code of a character andCHR()returns the character for a code —65and"B".What are the steps to build a username from the first 3 letters of a surname plus an ID?
e.g. surname
"Watson", id"42"- Take the characters needed:
part = surname.left(3)→"Wat" - Convert the case:
part = part.lower→"wat" - Concatenate:
username = part + id→"wat42"
- Take the characters needed:
What does
myFile.readLine()do?Returns the next line (record) in the open file.
What does
.writeLine()do?e.g.
myFile.writeLine("Sam")Writes a line (record) into the open file — adds
Samas a new line.What does
.endOfFile()return?True once the last record in the file has been read, otherwise false.
True or false? Data can be read from a file without opening the file first.
False. A file must be opened before reading or writing, and closed with
.close()afterwards.What are the steps to read and output every line of a text file?
e.g. the file
data.txt- Open it:
myFile = open("data.txt") - Loop to the end:
while NOT myFile.endOfFile()…print(myFile.readLine())…endwhile - Close it:
myFile.close()
- Open it:
What are the steps to create a new file and store a name typed in by the user?
e.g. the file
names.txt- Create it:
newFile("names.txt") - Open and write:
myFile = open("names.txt")thenmyFile.writeLine(input("Name?")) - Close it:
myFile.close()
- Create it:
What is a record, as a way of storing data?
A data structure holding all the data about one entity, made up of one or more attributes (fields).
What is an attribute in a record?
A single item of data within a record, e.g. a name or an email address.
True or false? Every attribute in a record must be of the same data type.
False. Unlike an array, a record can hold attributes of different data types.
What is a database?
A persistent store of related data organised in a structured way, holding many records in files.
Give one advantage of storing data about one item in a record rather than in separate variables.
All the related data is kept together under one identifier, and attributes of different data types can be stored together.
In SQL, what does
SELECT *do?Retrieves all fields from the records that match the query.
In SQL, what does the
FROMcommand specify?Which table the data is retrieved from.
In SQL, what does the
WHEREcommand do?e.g.
WHERE Year = 11Filters the records so only those meeting the condition are returned — only records whose Year is 11.
In SQL, what does
LIKEwith the%wildcard do?e.g.
WHERE Name LIKE "S%"Matches incomplete words or patterns — returns records whose Name begins with S.
In an SQL
WHEREclause, what is the difference betweenANDandOR?ANDmeans all the conditions must be true;ORmeans at least one condition must be true.What are the steps to write an SQL query that searches for data?
e.g. the names of pupils in Year 11 from the table
Students- State the fields:
SELECT Name - State the table:
FROM Students - State the condition:
WHERE Year = 11
- State the fields:
What is an array?
A data structure that holds multiple values of the same data type in sequence, with a fixed size set when it is declared.
True or false? In the array
scores, the first element isscores[1].False. Array indexes start at 0, so the first element is
scores[0].What are the steps to declare a 1D array and store a value in it?
e.g. an array
scoresholding 3 numbers- Declare with identifier and size:
array scores[3] - Assign using an index:
scores[0] = 10 - Read it back with the same index:
print(scores[0])→10
- Declare with identifier and size:
What are the steps to total all the elements of a 1D array?
e.g.
actNumbersholding 10, 14, 6- Set a total variable to 0:
count = 0 - Loop over every index:
for x = 0 to 2…count = count + actNumbers[x]…next x - Output after the loop:
print(count)→30
- Set a total variable to 0:
An algorithm totals a 3-element array using
for x = 0 to 2. What must be changed so it works for a 5-element array?Only the FOR loop line:
for x = 0 to 4, keeping the same counter variable (or use the length of the array).How is a 2D array declared and how is one element accessed?
e.g. an array
scorewith 3 rows and 2 columnsDeclared with rows and columns:
array score[3,2]; an element is accessed with both indexes, e.g.score[0,1]is row 0, column 1.How do you process every element of a 2D array?
e.g. an array with 3 rows and 2 columns
Use nested loops — an outer loop counting through the rows and an inner loop counting through the columns.
How can a 2D array be used to emulate a database table?
Each row stores one record and each column stores one field (attribute) of every record.
What is the difference between a function and a procedure?
A function returns a value to the code that called it; a procedure carries out a task but returns no value.
What are the steps to write a function that takes a parameter and returns a value?
e.g.
calculatePrice, giving the cost ofticketsat £5 each- Define it with a parameter:
function calculatePrice(tickets) - Calculate using the parameter:
price = tickets * 5 - Return the value:
return pricethenendfunction
- Define it with a parameter:
How do you call an existing function and keep the value it returns?
e.g. use
calculatePrice()for 3 tickets and store the result intotalPricetotalPrice = calculatePrice(3)— the argument goes in the brackets and the return value is assigned with=(not==).What are the steps to write and then call a procedure?
e.g. a procedure
greetthat prints a welcome for a name- Declare it with any parameters:
procedure greet(name) - Write the task, then end it:
print("Welcome " + name)…endprocedure - Call it by name with arguments:
greet("Sam")
- Declare it with any parameters:
What is the difference between a local and a global variable?
A local variable can only be used inside the subprogram where it is declared; a global variable is declared in the main program and can be used by all subprograms.
True or false? A variable declared inside a procedure can still be used in the main program after the procedure ends.
False. It is a local variable, so it only exists within that subprogram.
What is a random number in a program?
A number generated within a specified range that cannot be predicted.
Give two uses of random number generation in programs.
Any two: games of chance such as dice rolls; generating secure passwords; avoiding data collisions in network transmission.
What are the steps to select a random element from an array?
e.g.
namesholding "Ali", "Bea", "Cai"- Generate a random index in range:
i = random(0,2) - Use it as the index:
chosen = names[i] - Use the value:
print(chosen)→ e.g.Bea
- Generate a random index in range:
True or false? Spaces and punctuation are counted by
.lengthwhen finding the number of characters in a string.True.
.lengthcounts every character in the string, including spaces and punctuation.How do you pass an array into a sub program?
e.g. pass the arrayscoresinto a procedureprintAllUse the array's name as the argument in the call:
printAll(scores)
The parameter is then used like an array inside the sub program.What are the steps to write a function that takes an array and returns an array?
e.g. a functiondoubleAllthat doubles each element of an array of numbers- Define it with an array parameter:
function doubleAll(nums) - Loop through and change each element:
nums[i] = nums[i] * 2 - Return the array:
return nums, called asnewList = doubleAll(scores)
- Define it with an array parameter: