Computational Thinking (II) - Programming Flashcards

1
Q

How many programming data types are there?

A

5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do data types do?

A

Store data as different types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens to memory with data types?

A

Each data type is allocated a different amount of memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is it important to use the correct data type?

A

The code is more memory efficient, robust (hard to break) and predictable when the correct data types are used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can programming languages be typed?

A

Weakly typed or strongly typed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the characteristics of weakly types programming languages?

A

Weakly try to convert data types to avoid errors

*Can lead to unpredictable results

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the characteristics of strongly types programming languages?

A

Strongly don’t convert data types so will produce more errors

*Give more predictable results

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the 5 main data types and what are their characteristics?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the memory requirements for the 5 data types?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is casting?

A

Manual conversion between data types

Can be done using int(), real() / float(), bool() and str() commands

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In casting, what would int(“1”) achieve?

A

Converts the string “1” to the integer 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In casting, what would real(1) achieve?

A

Converts the integer 1 to the real 1.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In casting, what would bool(1) achieve?

A

Converts the integer 1 to the Boolean value True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In casting, what would str(True) achieve?

A

Converts the Boolean value True to the string “True”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is casting used with ASCII and what techniques allow it to be used?

A

ASCII numbers and characters can be found easily using casting

ASC() and CHR() functions are used

E.g. CHR(77) converts ASCII number 77 to ASCII character M

E.g. ASC(b) converts ASCII character b to ASCII number 98

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What would be the best data types to use in the following:

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are operators?

A

Special characters that perform certain functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are the arithmetic operators?

A
Addition
Subtraction
Multiplication
Division
Exponentiation
Quotient (DIV)
Remainder / modulus (MOD)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does the exponentiation operator ^ do?

A

Raises a number to a power

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does the quotient (DIV) operator do?

A

Returns the whole number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does the remainder / modulus (MOD) operator do?

A

Returns the remainder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What do operators work on?

A

Integers / real data values (or a combination of the two)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What mathematical rule do computers follow?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Using BODMAS, calculate the following:

2 + 8 * 2

(2 + 8) * 2

A

2 + 8 * 2 = 18

(2 + 8) * 2 = 20

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
List the computing operators, their typical operator, an example and the result
26
What is the assignment operator?
=
27
What does the assignment operator do? Give some examples?
It is used to assign values to constants or variables, for example: ``` Total = 100 Goals = 3 Cost = Total * 5 n = n + 5 ```
28
What positioning should occur when using the assignment operator?
The name of the constant should be on the left of the = and whatever is being assigned should be on the right, for example Dog’s name = Rio
29
What does a comparison operator do?
Compares the expression on the left hand side to the expression on the right A Boolean value (True / False) is produced
30
What are the common comparison operators?
``` == <> or != < ><= >= ```
31
List the common comparison operators, what they mean and examples if they are True or False
32
What is the difference between age = 25 and age == 25?
age = 25 versus is age 25
33
What are the two ways data values can be stored?
Constants or variables
34
What is the name of a constant or variable linked to?
The name of a constant or variable is linked to a memory location that stores the data value *The size of the memory location depends on the data type
35
How are constants assigned?
Using the const command
36
What will happen if a constant value is manually changed
It will result in an error
37
When is a constant assigned a data value?
At the time of design
38
What is the benefit of a variable over a constant?
Variables can change their value
39
How do languages know the data type?
Some languages (e.g. OCR Exam Reference Language) assume the data type when it is assigned (e.g. pressure = 30) for the integer Other languages need it to be declared (e.g. int pressure = 30) for the integer
40
What naming conventions exist for constants and variables?
Lower case for the first letter followed by a mixture of letters, numbers and underscores Variable names must not contain spaces or start with a number
41
Write a program to calculate the number of points an athlete has (5x points for 1st, 2x points for 2nd and 0 points for anything else)
firsts = 0 seconds = 0 points = 0 firsts = input(“Enter the number of 1st places”) seconds = input(“Enter the number of 2nd places”) points = (5 * firsts) + (2 * seconds) print(points)
42
What are the benefits of assigning constants?
The constants remain constant(!) so don’t need to be changed when the program runs If they do change, at design level, they only need to be updated in one location
43
What are strings made up of?
Characters – alphanumeric (letters, numbers, spaces, symbols etc…) Ad1@£moT _R87
44
How are strings written? Give an example
In “double” or sometimes ‘single’ quotation marks string1 = “Print me, I’m a string” print(string1) Print me, I’m a string
45
How can strings be joined together, and what is this known as? Give an example
Strings can be combined by concatenation, often with the + operator string1 = “My favourite food is” string2 = “cheese” print(string1 + “ “ + string2) My favourite food is cheese
46
What is the position of the first character in a string usually numbered as?
0
47
What special function exist in manipulation (known as methods)?
``` upper lower length left right subString ```
48
How are methods used? Give an example
Methods act on a particular object (object name followed by a dot “.” and the method’s name), for example: string1. upper() string2. lower() string3. length
49
List some typical functions (methods), what they do and how they would effect the string x which contains “Hello”
50
Show some Python specific methods
51
Write a method to get the first three letters of the variable name and have them manipulated into uppercase
name.left(3).upper Python: name[:3].upper()
52
Write a method to get the last two letters of the variable name and have them manipulated into lowercase
name.left(2).lower Python: name[-2:].lower()
53
The following code does not read correctly – what needs to be added to it? ``` name = David surname = Boatswain ``` print(name + surname)
A space (“ “) needs to be added as it currently shows DavidBoatswain ``` name = David surname = Boatswain ``` print(name + “ “ + surname) David Boatswain
54
Write a method to get the first and last letters of the variable club
newclub = club.left(1) + club.right(1) Python: newclub = club[:1] + club[-1:] print(newclub)
55
What is program flow and what are the two types of selection statement?
The order that steps are carried out – IF and SWITCH selection statements allow for program flow
56
What structure do IF statements usually have?
IF-THEN-ELSE
57
What do IF statements allow you to check for?
If a condition is True or False – carry out different actions depending on the outcome
58
Draw an IF statement flow diagram to check if the password entered is correct
59
Write a piece of pseudocode for granting access depending on the password entered
password = input(“Enter your password”) ``` if password == storedpassword then: print(“Access granted”) else: print(“Access denied”) end if ```
60
What is a NESTED IF statement?
More complex IF statements – IF within another IF NESTED IF statements check more than one condition, once the previous condition has been established (True or False)
61
Write a piece of pseudocode for granting access depending on the password and the Year Group being 11
``` password = input(“Enter your password”) if user == Year11 then: if password == storedpassword then: print(“Access granted”) else: print(“Access denied”) end if else: print(“User not in Year 11 – access denied”) end if ```
62
What are ELSE IF statements (Python: elif)?
ELSE IF statements can check multiple conditions They only check more conditions is the previous one was False
63
Write a piece of pseudocode for granting access depending on the password and the Year Group being 11 (with functions for Year 10 and Year 9 also included via ELSE IF statements)
``` password = input(“Enter your password”) if user == Year11 then: if password == storedpassword then: print(“Access granted”) else: print(“Access denied”) end if elseif user == Year10 then: print(“User not in correct year to access”) elseif user == Year9 then: print(“User not in correct year to access”) else: print(“Access denied”) end if ```
64
What is a SWITCH statement (also known as a CASE statement)?
SWITCH (CASE) statements check the value of a variable (rather than checking if the statement is True or False)
65
When are SWITCH statements used?
SWITCH statements are used when you want a program to perform different actions for different values of the same variable
66
Draw a SWITCH statement flow diagram for counting the votes in an election
67
Write out the pseudocode for 3x candidates and count the votes cast for them
``` davidvote = 0 jessicavote = 0 rachelvote = 0 ``` ``` vote = input(“Please vote for your favourite person”) switch vote: case “David”: davidvote = davidvote + 1 case “Jessica”: jessicavote = jessicavote + 1 case ”Rachel”: rachelvote = rachelvote + 1 default: print(“Error with your vote, please try again”) endswitch ```
68
How does a SWITCH statement start and end?
Start with SWITCH parameter, e.g. switchvote End with endswitch
69
What is the advantage of a SWITCH statement?
They are a neater way to test different values of a variable (so are easier to maintain)
70
What is the disadvantage of a SWITCH statement?
SWITCH statements can only check the value of one variable (unlike IF-ELSEIF statements which can check if multiple conditions are True
71
What is iteration?
A programming construct – allowing a program to repeat certain steps until told otherwise or until a condition has been met Often referred to as looping
72
What are the benefits of iteration (looping)?
They greatly simplify a program
73
Using iteration (looping), simplify this code:
Run a loop six times
74
What are FOR loops? Give an example of one
FOR loops are examples of count-controlled loops, repeating the code inside them a fixed number of times, for example: for i = 1 to 10 print(“something”) next i
75
What is a WHILE loop?
WHILE loops are controlled by a condition at the start of the loop They keep going while the condition is True
76
Give an example of a WHILE loop
``` total = 0 target = input(“How much money do you want to save?”) ``` while total < target funds = input(“How much money do you have?”) total = total + funds endwhile
77
What are DO UNTIL loops?
DO UNTIL loops are controlled by a condition at the end of the loop They keep looping until the condition is True
78
Give an example of a DO UNTIL loop
``` total = 0 target = input(“How much money do you want to save?”) ``` do: funds = input(“How much money do you have?”) total = total + funds until total >= target
79
What does the following show?
80
What are the Boolean data type values?
True or False
81
How do electronic circuits make decisions?
Logic gates – two or more inputs are considered and outputs from the circuit are determined
82
What do logic gates allow for and what states are they found in?
Logic gates allow an electronic system to make a decision based on a number of input Inputs and outputs can be True (1 or ‘on’) or False (0 or ‘off’)
83
What are the common Boolean operators?
AND OR NOT *and XOR
84
What are truth tables?
Truth tables show the logic gate calculations (showing all possible input combinations of 1s and 0s and the corresponding output)
85
What does the following show?
AND gate
86
What does the following show?
OR gate
87
What does the following show?
XOR gate
88
What does the following show?
NOT gate
89
What is the truth table for the following AND gate?
Q = A AND B
90
What is the truth table for the following OR gate?
Q = A OR B
91
What is the truth table for the following XOR gate?
Q = A XOR B
92
What is the truth table for the following NOT gate?
Q = NOT A
93
What is needed for an AND gate to output a 1?
An AND gate can be used on a gate with two inputs. AND tells us that both inputs have to be 1 in order for the output to be 1
94
What is needed for an OR gate to output a 1?
The OR gate has two inputs. One or both inputs must be 1 to output 1, otherwise it outputs 0
95
What is needed for a XOR gate to output a 1?
The exclusive OR gate works the same as an OR gate, but will output 1 only if one or the other (not both) inputs are 1
96
What is needed for a NOT gate to output a 1?
A NOT gate has just one input. The output of the circuit will be the opposite of the input. If 0 is input, then the output is 1. If 1 is input, then 0 is output
97
What are complex logic gates?
Chains of logic decisions
98
How many inputs does the following complex logic gate show and what order does it need to be completed in?
3 inputs (eight possible outcomes) First find D Second find E Finally find Z
99
Complete the truth table for the complex logic gate:
Z = D OR E or Z = NOT A OR (B AND C)
100
What order is followed when combining Boolean operators?
Brackets NOT AND OR
101
Give an example of the Boolean operator AND, OR and NOT for an example that is True and and example that is False
102
How are the Boolean operators AND, OR and NOT sometimes written in programming languages?
AND sometimes written as && OR sometimes written as || NOT sometimes written as !
103
Write a Boolean containing code to keep score for Mike and Stu who are playing a best of 10 game match (ends when one wins 6 games, or they both win 5)
``` stu = 0 mike = 0 do: roundwinner = input(“Enter winner’s name”) switch roundwinner: case “Stu”: stunew = stunew + 1 case “Mike”: mikenew = mikenew + 1 endswitch until stunew == 6 OR mikenew == 6 OR (stunew == 5 AND mikenew == 5) ```
104
Write a Boolean containing code to keep a player alive (they die if any variable = 0, two are less than 20 or all are less than 40). Variables are food, water and bravery
if food == 0 OR water == 0 OR bravery == ) then: alive = false elseif (food < 20 AND water < 20) OR (food < 20 AND bravery < 20) OR (water < 20 AND bravery < 20) then: alive = false elseif food < 40 AND water < 40 AND bravery < 40 then: alive = false else: alive = true endif
105
How can random number generation be useful in coding?
When you don’t want a program to follow the same path each time They can be used to simulate random real-life events, e.g. rolling a dice, playing the lottery, flipping a coin etc…
106
What code is used to generate a random number (in the OCR Exam Reference Language)?
random(x, y) x and y can be integers or real numbers
107
Write some code to simulate a dice roll
roll = random(1, 6) | print(roll)
108
Write some code to simulate a dice roll that is performed three times
for i = 1 to 3 roll = random(1, 6) print(roll) next i
109
Write a random selection to generate another random event, i.e. if a coin flip is heads or tails
``` number = random(0, 1) if number == 0 then: print(“heads”) elseif number == 1 then: print(“tails”) endif ```
110
How can a random number help with an array?
The random number generated can pick an element in the position of the array number = random(0, 4) chosenfruit = fruit[number] Print(“today you should eat a “ + chosenfruit)
111
Write a piece of code in Python for a lottery code generator (6x numbers between 1 and 49 and ideally put back repeats / sort before the print numerically)
112
What is a more efficient way of storing data (e.g. 10x scores) than declaring 10x variables, e.g. score1, score2, score3 etc…?
Using an array (similar to a list (1D) or table (2D) which are much more efficient
113
What is an array?
A data structure that holds similar related data (consisting of elements) Each element has a position in the array and can hold a value (the data in an array must be the same type)
114
How can an array be declared so it can be used? Give an example for a score holding array
The array must be given an identifier and a size (the number of elements it will hold) For example, array scores[9] is an array called scores consisting of 10 elements
115
How are values assigned to an element within an array? Give an example
The array name, elements position and value are identified For example: array scores[0] = 100 array scores[1] = 98 array scores[2] = 88
116
How are values retrieved from an array? Give an example
The array and element’s position are referred to. For example in the following array: print(scores[0]) would output 100 print(scores[1]) would output 98
117
Give an example of a 1-dimensional array
118
Give an example of a 2-dimensional array
119
How is a 2-dimensional array declared? Give an example
The row and the column are declared, for example array scores[1,9] would give two rows and ten columns
120
How are values assigned and retrieved for a 2-dimensional array?
Assigned via array scores[0,1] = 98 Retrieved via print(scores[1,4]) which would give the value 111
121
What happens to the data when a program finishes or is closed?
The data is lost (unless it is stored in a file for access later)
122
What are the two modes of operation for a file?
Read from – file opened so data can be read from it Write to – file opened so data can be written to it
123
What is each item of data written to or from a file known as?
A record
124
How is a file opened? Give an example
To open a file, it must be referred to by its identifier, for example: file = openRead("scores.txt") This would open the file scores.txt and allow its contents to be read
125
How is a file opened for writing to? Give an example
To open a file, it must be referred to by its identifier, for example: file = openWrite("scores.txt") This would open the file scores.txt and allow it to have data written to it
126
How is a file closed?
file.close()
127
Data held in a record can be read into what?
A variable or an array
128
Give an example code to open scores.txt and read into a variable score
file = openRead("scores.txt") score = myFile.readLine() file.close()
129
Give an example code to open scores.txt and read into an array called scores
file = openWrite("scores.txt") for x = 0 to 9 scores[x]=myFile.readLine() next x file.close()
130
What statement is used to check to see if the last record has already been read?
endOfFile()
131
Give an example of a code to read each record in scores.txt, placing them in an array called scores and ceasing when the last record has been reached
file = openRead("scores.txt") while NOT file.endOfFile() scores[x]=myFile.readLine() x = x + 1 endwhile file.close()
132
How is data written to a file? Give an example
Data is written one line at a time via writeLine file = openWrite("scores.txt") for x = 0 to 9 file.writeLine(scores[x]) next x file.close()
133
What is a record?
A type of data structure, similar to an array Collections of data values are stored
134
What is the advantage of a record over an array?
Records can store values with different data types, such as strings, integers and Booleans
135
Within a record what is each item referred to and what are they given when created?
Each item in a record is called a field Each field is given a data type and field name when created (fixed in length at creation)
136
What are the advantages of records?
They keep related information in one place
137
Write a piece of pseudocode creating a record structure called ‘recipes’ with some different data types assigned to some example fields (e.g. recipe number, name, score etc…)
138
How would variables be assigned to a record (with a few fields)?
139
If there are multiple variables with the same record structure how can they be collected?
In an array
140
What is SQL?
Structured Query Language Used to search tables (usually databases) for specific data *Records and fields of a database table are the rows and columns
141
In SQL what follows the SELECT and FROM keywords?
SELECT followed by the names of the field (columns) that are required FROM followed by the name of the table(s) to be searched
142
How would all the planet names be returned from this database?
SELECT planetName FROM planets
143
How would all the planet names and the planet ratings be returned from this database?
SELECT planetName, planetRating FROM planets
144
How would all of the information be returned from this database?
Using the wildcard *
145
What keyword is used within SQL to filter? Give an example using the database below:
WHERE SELECT * FROM planets WHERE planetGravity > = 50 This would return Giants Deep
146
Give an example of a WHERE filter using a Boolean operator in the database below:
SELECT planetName FROM planets WHERE planetDistance <= 200 AND planetAtmosphere = “Hostile” This would return Brittle Hollow and Ash Twin
147
What are the two scopes for variables?
Local and Global
148
How is a variable made as global?
Global scope is added using the keyword global() They can be used anytime after their declaration
149
What is the benefit of using local variables (in sub programs)?
They do not affect, nor are affected by anything outside of the sub program Variables can have the same name in different sub programs
150
What are the two types of subprogram?
Procedures Functions
151
What is a subprogram?
Subprograms are small programs that are written within a larger, main program. The purpose of a subprogram is to perform a specific task. This task may need to be done more than once at various points in the main program.
152
What are the benefits of subprograms?
Usually small in size Can be saved separately as modules (and used again on other programs) Repeatedly used at various points in main program (code only needs to be written once)
153
What is a procedure?
A subprogram that performs a specific task When complete, the subprogram ends and the main program continues where it left off
154
How is a procedure created? Give the syntax
procedure identifier (value to be passed) procedure code endprocedure
155
Write a procedure to clear the screen by printing x number of blank lines
procedure clear_screen(x) for i = 1 to x: print(" ") endprocedure
156
How does a procedure run? What is needed for this to occur?
It is called The procedure name is needed + any values that the procedure will need
157
What is a function?
A function is a subprogram that performs a specific task Unlike a procedure, a function manipulates data and returns a result back to the main program
158
How is a function run?
It is called
159
What is needed to call a function?
Function identified Value to be passed into the function Variable for the function to return a value into
160
What does the following show? function f_to_c(temperature_in_f) temperature_in_c= (temperature_in_f –32) * 5/9 return temperature_in_c endfunction
A function *Fahrenheit into Celsius