2.2 Programming Techniques COMPLETE Flashcards
Identifier
Describe the data being stored
Short Identifier
Easier to spell correctly each time they are used
Long Identifier
Can be used if they are more descriptive e.g. firstName
True or False: Identifiers can be the same as reserved words
False, identifiers cannot be the same as reserved words such as print or while
Can variables change whilst a program is running?
Yes
Can constants change whilst a program is running?
No
How are values assigned to variables?
Values are assigned in assignment statements using the = symbol e.g. firstName = “David”
How are values assigned to constants?
Constants are assigned as cost Pi = 3.142 for example. They can then be used in calculations
Can identifiers for variables and constants change throughout a program?
No - they must be consistent throughout the program
What is used in camel case?
Upper and lower case characters (FirstName or PricePerKilo). The first word can be lower case
What is used in snake case?
Underscores are used to link words (first_name or price_per_kilo)
What is an assignment?
The association of a piece of data with a variable or constant, e.g. index = 0
Can an assignment be done as an input?
Yes (e.g. name = input(“Please enter your name”).
When can a variable be output?
At any time throughout the program
What is the function of the operator +?
Addition
What is the function of the operator -?
Subtraction
What is the function of the operator *?
Multiplication
What is the function of the operator /?
Division
What is the function of the operator MOD?
Modulus division. Returns the remainder after the division of one number by another (26 MOD 4 = 2)
What is the function of the operator DIV?
Quotient division. Returns the quotient or the lowest integer (26 DIV 4 = 6)
What is the function of the operator ^?
Exponential powers of (3^3 = 27)
MDIBAS - This is an anagram for the order of operations in a calculation. What is the correct order and what does each letter stand for?
B - Brackets I - Indices D - Division M - Multiplication A - Addition S - Subtraction
What is the function of the comparison operator ==?
Equal to - checks if two values are equal. Two equal signs are used to distinguish it from assigning a value to a variable
What is the function of the comparison operator !=?
Not equal to - checks if two values are not equal to each other
What is the function of the comparison operator
Less than - checks if one value is less than another
What is the function of the comparison operator <=?
Less than or equal to - checks if one value is less than or equal to another
What is the function of the comparison operator >?
Greater than - checks if one value is greater than another
What is the function of the comparison operator >=?
Greater than or equal to - checks if one value is greater than or equal to another
True or False: Comparison operators can only be used to compare numbers
False, these operators can be used with strings as well as numbers. The strings are compared alphabetically
Boolean Operator: AND
Ensures that the overall statement is true only of *all* the individual statements are true
Boolean Operator: OR
Ensures that the overall statement is true if *any* of the individual statements are true
Boolean Operator: NOT
Used to reverse the logical state of the other operators
In programming, what does sequence do?
Ensures that the commands are executed in the correct order
In programming, what does selection do?
Chooses between two or more options. Involves the use of combinations of if, else and elseif statements
When can if/else be used in programming?
if/else statements can be used if there are only two possible outcomes.
Why are elseif statements more efficient in programming?
As soon as the correct condition is found, none of the rest of the options are checked
Why are else statements used in programming?
To state what should happen if none of the options in the if or elseif statements are true
What are nested if statements?
If statements that are completely within another if statement
Every if statement always needs its own _____ statement
endif
_________ is the process of repeating a set of instructions for a fixed number of times or until there is a desired outcome. It is executed by program constructs called loops
Iteration
What is count controlled iteration?
Iteration used when the number of iterations is known before the loop is started
What is a condition controlled iteration?
Iteration used when the number of times a loop is executed is determined by a condition
Are while loops an example of count controlled iteration, condition controlled iteration or can they be either?
While loops can be count controlled or condition controlled
Are for loops an example of count controlled iteration or condition controlled iteration?
Count controlled
Are do…until loops an example of count controlled iteration or condition controlled iteration?
Condition controlled
What do for loops do?
Instruct the loop to be executed for a set number of times
What do condition controlled while loops do?
Continue while a condition remains true and stops when it becomes false
What makes a count controlled while loop less efficient than a for loop?
More lines of code are needed
What do do… until loops do?
Continue until a condition becomes true
In a while loop the variable to be tested must be ________ ___ _____ _ _____ before the loop is started
Declared and given a value (initialised)
What must happen each time a while loop is ran?
Must be incremented or decremented
What is the difference between a while loop and a do… until loop?
In a while loop the condition is tested at the start of the loop whereas in a do… until loop it is tested at the end and will therefore be run at least once
What does concatenated mean?
Combined
Why must computers be aware of data types of values stored in variables?
So that data is interpreted and manipulated correctly
What is an integer?
A number without a fraction or decimal. Integers can be positive or negative
Give an example of an integer
Any whole number eg 0, 3, 100, 10369
What is a real number?
All numbers that exist and their fractions and decimals
Are integers real numbers?
Yes
Why is it more efficient to declare integers as integers?
So that less memory is needed
What are real numbers declared as in pseudocode/python?
‘float’
Give an example of a real number
Any number, including decimals, such as 2, 3.9, 6.632, 9.37352
What is a character?
A single letter, number or symbol
Give an example of a character
Any letter, number or symbol eg C, 3, !
What can a string hold?
A list of characters of any length
What are strings enclosed in?
Single or double quotation marks
Give an example of a string
Anything enclosed in single or double quotation marks eg “Hello user3 your password is ?1pass69word”
Data used by a computer are presented in strings of…
1s and 0s
What happens if a computer is not told the data type?
It will not be able to use it correctly eg it may try to multiply text and display numbers as images
Data types must be declared in all/some programming languages
Some
What would int age declare?
That the variable “age” is to store integer numbers
What happens in languages that the data type does not have to be stated?
The type is inferred from the type of data first stored (eg age = 13 would imply that the variable age stores integers)
What is casting?
The conversion of one data type into another
What would str(integerVariable) do?
Convert an integer into a string
Casting must be done in all/some programming languages when concatenating variables and text in a print statement
Some
What would int(stringVariable) or float(stringVariable) do?
Convert a string into an integer or a real number
What is a string?
A string is a sequence of characters than contain letters, numbers and symbols
What does a string literal consist of?
Characters enclosed in quotation marks such as “A13!”
What is a string variable?
A string that can be declared, such as password = “A13!”
Is print(“A13!”) a literal string or a variable string?
A literal string
Is print(password) a literal string or a variable string?
A variable string
password = “A13!” Do print(“A13!”) and print(password) produce the same output?
Yes
password = “A13!” Why do print(“A13!”) and print(password) produce the same output?
Because the variable “password” is a string variable and it has a value of “A13!”
What does concatenation mean?
Joining
Strings can/cannot be concatenated
Can
Strings can be concatenated end to end to form larger strings using the _ symbol
+
The position of a character in a string is given by its…
Index number
What is the first index in a string?
0
“STRING” What is the index number of the letter N in this string?
4
What is string traversal?
The process of moving through a string one character at a time.
What can string traversal be used to do?
See if a string contains a particular character or group of characters
The ______ of a string must be found in order to create a loop to examine each character
Length
True/False: A loop can be set up to move through a string
True
Why must a loop be set up to run from 0 to the length of the string minus one?
Because strings use index numbers. eg The string “Rosie and Jack” has a length of 14, so the indices are 0 to 13. If there was no -1 there would be an error message as there is not an index 14
What can be snipped out of a string?
Substrings or portions
myString = “RosieandJack” snip = myString.substring(2, 4) print(snip) What does the numbers 2 and 4 do in this string? What will this print?
The first number is the starting point and the second number is the number of characters. This would print the word “siea” as the substring starts at index 2 (the letter “s”) and has a length of 4 characters
What is the difference between an array and a variable?
Variables store one item of data that can change whereas arrays can store multiple items of data, not just one
What is an array?
A data structure that can store multiple items of data, called elements
What are items of data in an array called?
Elements
Elements are all of the same ____ ____ under the same _________
data type and identifier
How is an array created?
By specifying the elements stored in the array (declaring)
What does declaring an array mean?
Specifying the elements stored in an array.
array names = [“Alice”, “Catherine”, “David”] array scores = [10, 13, 17] What is this array doing?
Storing 3 people’s names and their scores
What are elements in an array enclosed within?
Square brackets
What are elements in an array separated with?
Commas
What is an element? (Computer Science)
An item of data at a particular index
If the elements in an array are at index positions 0 to 4, what is the length of the array?
5
What is a two-dimensional array?
In a 2D array there is a second array at each index position of a one-dimensional array
What do two-dimensional arrays form?
A matrix
In a two-dimensional array, each element has ___ indices to indicate its position
Two
What would an array with the structure as shown be declared as?

array arrayName [5, 4]
Why would an array with the structure as shown be declared as array arrayName [5, 4]?
There are 5 indexes in the original array, and now 4 items of data in each position. For each of the elements 0 to 4, there are four items of data, 0 to 3.
How is data assigned to an element in an array?
Index position
True/False: Programming languages allow users to store data in files on storage decvices so that they are not lost when a program is closed
True
Before a file can be accessed or opened, it must be given a…
File handle
What is a file handle?
A reference assigned to a file so that the program can access it.
eg myFile = openWrite(“Sample.txt”)
True/False: When a file is opened in write mode, a new file will be created with that tilename if it does not already exist
True
If you save a file and a file with that name already exists, what happens to the existing file?
It will be overwritten and the existing data will be lost
What does OpenAppend(“filename.txt”) do?
Prevent data being lost due to it being overwritten
Once a file has been read from or written to, it must be…
Closed (eg myFile.close())
True/False: A user can’t open a file and write data directly to it from the keyboard
False - they can
True/False: A program can be coded to save data stored in variables to a text file
True
What does myFile. openRead(“filename.txt”) do?
Opens a file to read data
True/False: Data stored in a text file can be loaded into the variables of a program
True
What does the endOfFile() function do?
If the number to read in is not known, the endOfFile() function can be used
What is a record? (Computer Science)
A collection of data objects relating to a particular subject or entity
True/False: The data items stored in each record must be of the same data type
False, they can be of different data types eg integers and strings
What does DBMS stand for?
Database management system
Large volumes of data are usually stored in d________
databases
How are databases created?
Using a DBMS (database management system)
How is data stored in a database?
Tables
What do column headings in a table in a database define?
Fields
Each record in a database must have a ___ _____ that is an item of data that is unique to that record
key field
Each Each column in a table in a database is a _____
Field
What is stored in each record of a database?
An item of data
Some programming languages allow users to create a data structure similar to that in databases where the items of data can be different/are the same data types
can be different
What are records sometimes referred to as?
Structs
When structs are set up, the data type for each element is usually stipulated as the elements, unlike those stored in arrays, can be different/are the same data types
Can be different
What does SQL stand for?
Structured Query Language
What is SQL used for?
Creating, maintaining and accessing databases
What does SQL allow users to do?
Search for records that contain a particular item or items of data.
What keyword is used for searching with SQL?
WHERE
What keyword is used to display records with SQL?
SELECT
Are commands in SQL case-sensitive?
No - eg both select and SELECT will work
What is the wildcard symbol % used for in SQL?
Used as a substitute for one or many characters
What is the wildcard symbol * used for in SQL?
To represent ‘all fields’, for example WHERE TUTORgROUP like “%x”; would return all records with a tutor group ending with an X
SELECT FirstName, Surname FROM Students;
What is important about the syntax of this command?
There is a semi-colon at the end of the command
Why are commands written on a seperate linewhen searching with SQL?
To make them easier to understand
What is a sub-program?
A self-contained sequence of program instructions that performs a specific task
What does using sub-programs allow?
The production of more efficient code
When are sub-programs called?
Whenever it is needed to carry out that function
What is another name for sub-programs?
Subroutines
True/False: Sub-programs allow the production of structured code
True
True/False: Sub-programs make programs longer
False - the code only needs to be written once and can be called as many times as needed
True/False: Sub-programs make program code easier to read and test
True
True/False: Sub-programs make the development time of a program longer
False, they shorten the development time of a program
Do sub-programs make testing easier or harder?
Easier
Sub-programs make code more/less readable
More
What is a function?
A sub-program that returns a value to the main program when it is called
What is a procedure?
A sub-program that carries out a specific task but does not return values to the main program
Values passed to the function from the main program are stored in the sub-program’s…
parameters
True/False: Sub-programs need to be defined
True
To create a sub-program, do you need to define it with its type and indicate where it ends?
Yes
What does “return velocity” do in a function?
The value of velocity is returned to the main program
What are variables used only within a sub-program and not in a main program called?
Local variables
What needs to be stated that stores the values that a function will use?
Parameters
True/False: Values of parameters are not passed to them by the main program when the function is called
False, they are
True/False: Sub-programs cannot be called from within the main program
False, they can
If the value of the local variable “velocity” is the result of a calculation performed in a function, what is it passed back to?
The specified global variable in the main program
averageVelocity = vspeed(distanceTravelled, tiemTaken)
What does this line of code do?
Calls the function and passes the values of two gloval variable sto it as arguments in the correct order. In the sub-program these values are passed into the corresponding parameters
Why is it best that you don’t use the same names for local and global variables?
You could get into a muddle with the logic and mix them up
What are global variables?
Variables that exist within the main program and can be referenced in other lines of code
What do procedures do?
They do not return a value to the main program. They could print out a message for the user, for example
Parameters in a procedure are/are not passed from the main program
Are