1/82
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is a variable?
A named memory location that stores a value that can change during program execution — declared by giving it a name and assigning a value — eg x = 5
What is a constant?
A named memory location that stores a value that cannot change during program execution — declared using const — eg const pi = 3.14
Why use constants instead of variables?
Makes code easier to read and maintain — prevents accidental changes to important values — if the value needs updating you only change it in one place
What is an assignment?
Giving a value to a variable or constant using the = operator — eg name = "Sam" or score = 0
What is the difference between a variable and a constant?
A variable can change its value during the program — a constant cannot change once declared
What is sequence in programming?
Statements are executed one after another in the order they are written — the simplest and most fundamental programming construct
What is selection in programming?
The program chooses between different paths based on a condition being true or false — implemented using IF THEN ELSE or SWITCH CASE statements
What is iteration in programming?
Repeating a block of code — can be count-controlled using a FOR loop — or condition-controlled using WHILE or DO UNTIL loops
What is a count-controlled loop?
A loop that repeats a fixed known number of times — uses a FOR loop — the number of repetitions is determined before the loop starts
What is a condition-controlled loop?
A loop that repeats based on whether a condition is true or false — the number of repetitions is not known in advance — uses WHILE or DO UNTIL
What is the difference between a WHILE loop and a DO UNTIL loop?
WHILE — checks the condition before entering the loop — may never execute if condition is immediately false. DO UNTIL — checks the condition after the loop body — always executes at least once regardless of the condition
Write a FOR loop in OCR ERL that prints the numbers 1 to 10
for i=1 to 10. print(i). next i
Write a FOR loop in OCR ERL that prints even numbers from 2 to 10
for i=2 to 10 step 2. print(i). next i
Write a FOR loop in OCR ERL that counts down from 10 to 1
for i=10 to 1 step -1. print(i). next i
Write a WHILE loop in OCR ERL that keeps asking for a password until the user enters "letmein"
password = input("Enter password"). while password != "letmein". password = input("Enter password"). endwhile
Write a DO UNTIL loop in OCR ERL that keeps asking for a number until the user enters a number greater than 10
do. num = int(input("Enter a number")). until num > 10
What are the comparison operators in OCR ERL and what do they mean?
== equal to. != not equal to. < less than.
What are the arithmetic operators in OCR ERL?
What does MOD do and give an example?
Returns the remainder after division — 10 MOD 3 = 1 because 10 divided by 3 is 3 remainder 1 — useful for checking if a number is even or odd
What does DIV do and give an example?
Returns the whole number quotient after division ignoring the remainder — 10 DIV 3 = 3 because 3 goes into 10 three times — useful when you need integer division
What does ^ do and give an example?
Raises a number to a power — 2^3 = 8 because 2 to the power of 3 is 2×2×2 = 8
What are the Boolean operators AND OR and NOT?
AND — both conditions must be true for the overall result to be true. OR — at least one condition must be true. NOT — reverses the Boolean value — NOT true = false
Write an IF statement in OCR ERL that prints "pass" if score >= 50 and "fail" otherwise
if score >= 50 then. print("pass"). else. print("fail"). endif
Write an IF ELSEIF ELSE statement in OCR ERL that grades a score
if score >= 70 then. print("Distinction"). elseif score >= 50 then. print("Pass"). else. print("Fail"). endif
Write a SWITCH CASE statement in OCR ERL for days of the week
switch day :. case "Mon": print("Monday"). case "Tue": print("Tuesday"). default: print("Other day"). endswitch
When would you use SWITCH CASE instead of IF ELSEIF?
When checking a single variable against many possible specific values — SWITCH CASE is cleaner and easier to read than many ELSEIF statements
What is nesting and give an example?
Placing one construct inside another — eg an IF statement inside a FOR loop — for i=1 to 10. if i MOD 2 == 0 then. print(i). endif. next i — this prints even numbers from 1 to 10
Write a nested loop in OCR ERL that prints a 3x3 grid of asterisks
for row=1 to 3. for col=1 to 3. print("*"). next col. next row
What is an integer data type?
Stores whole numbers with no decimal point — eg 5 — 42 — -7 — used for counting discrete items like scores or ages
What is a real data type?
Stores numbers with a decimal point — eg 3.14 — 2.5 — -0.7 — used when precision beyond whole numbers is needed like prices or measurements
What is a Boolean data type?
Can only hold one of two values — True or False — used in conditions and logical operations
What is a character data type?
Stores a single character — eg 'A' — '3' — '!' — enclosed in single quotes
What is a string data type?
Stores a sequence of characters — eg "Hello" — "Computer Science" — enclosed in double quotes — strings can be manipulated using string operations
What is casting?
Temporarily converting a value from one data type to another — useful when combining different data types or performing operations that require a specific type
Give examples of casting functions in OCR ERL
str(345) — converts integer to string. int("3") — converts string to integer. float("4.52") or real("4.52") — converts string to real. bool("True") — converts string to Boolean
Why would you need to cast user input to an integer?
User input is always received as a string — if you want to perform arithmetic on the input you must cast it to an integer first — eg int(input("Enter a number"))
What happens if you try to add a string and an integer without casting?
A type error occurs — the program crashes because you cannot perform arithmetic between incompatible data types
Choose an appropriate data type for storing a person's age
Integer — age is a whole number
Choose an appropriate data type for storing a person's name
String — a name is a sequence of characters
Choose an appropriate data type for storing whether a user is logged in
Boolean — the answer is either True or False
Choose an appropriate data type for storing a product price
Real — prices include decimal values
Choose an appropriate data type for storing a student's grade letter
Character — a single letter like A B or C
2.2 ADDITIONAL PROGRAMMING TECHNIQUES
What is string concatenation?
Joining two or more strings together using the + operator — eg "Hello" + " " + "World" gives "Hello World"
Write OCR ERL code that asks for a first name and last name and prints the full name
firstName = input("First name"). lastName = input("Last name"). print(firstName + " " + lastName)
What does .length do in OCR ERL?
Returns the number of characters in a string — eg subject = "ComputerScience". subject.length returns 15
What does .substring(x,i) do in OCR ERL?
Returns a substring starting at index x with i characters — 0 indexed — eg "ComputerScience".substring(3,5) returns "puter"
What does .left(i) do in OCR ERL?
Returns the first i characters from the left of a string — eg "ComputerScience".left(4) returns "Comp"
What does .right(i) do in OCR ERL?
Returns the last i characters from the right of a string — eg "ComputerScience".right(3) returns "nce"
What does .upper do in OCR ERL?
Converts all characters in a string to uppercase — eg "hello".upper returns "HELLO"
What does .lower do in OCR ERL?
Converts all characters in a string to lowercase — eg "HELLO".lower returns "hello"
What does ASC() do and give an example?
Returns the ASCII numerical value of a character — eg ASC('A') returns 65 — ASC('B') returns 66
What does CHR() do and give an example?
Returns the character corresponding to an ASCII value — eg CHR(65) returns 'A' — CHR(97) returns 'a'
What is a 1D array?
A fixed length list of elements all of the same data type — accessed using a single index — 0 indexed in OCR ERL — like a single row of boxes each with a numbered position
Declare a 1D array called scores with 6 elements in OCR ERL
array scores[6]
Declare a 1D array in OCR ERL with values already assigned
array colours = ["Blue","Pink","Green","Yellow","Red"]
How do you access the third element of an array called names in OCR ERL?
names[2] — because arrays are 0 indexed so the third element is at index 2
How do you assign a value to the first element of an array called scores?
scores[0] = 100
What is a 2D array?
An array with rows and columns — like a grid or table — accessed using two indices — eg gameboard[row,col] — useful for representing grids boards or database tables
Declare a 2D array called gameboard with 8 rows and 8 columns in OCR ERL
array gameboard[8,8]
Assign the value "Pawn" to row 1 column 0 of a 2D array called gameboard
gameboard[1,0] = "Pawn"
Write OCR ERL code to print all elements of a 1D array called scores with 5 elements
for i=0 to 4. print(scores[i]). next i
What are the file handling operations in OCR ERL?
open() — opens a file. .close() — closes a file. .readLine() — reads the next line. .writeLine(text) — writes a line to end of file. .endOfFile() — returns true if end of file reached. newFile() — creates a new file
Write OCR ERL code to open a file read every line and print it
myFile = open("data.txt"). while NOT myFile.endOfFile(). print(myFile.readLine()). endwhile. myFile.close()
Write OCR ERL code to open a file and write a line to it
myFile = open("data.txt"). myFile.writeLine("New line of text"). myFile.close()
What is a record in programming?
A data structure that stores a collection of related fields which can be different data types — similar to a row in a database table — used to represent real-world entities
What is SQL used for in programming?
Structured Query Language — used to search for and retrieve data from a database — can filter and select specific records
What are the three SQL commands you need to know?
SELECT — specifies which fields to retrieve. FROM — specifies which table to retrieve from. WHERE — filters records based on a condition
Write a SQL query to select all students from a table called Students where grade equals "A"
SELECT * FROM Students WHERE grade == "A"
Write a SQL query to select just the name field from a table called Customers where age is greater than 18
SELECT name FROM Customers WHERE age > 18
What is a procedure in programming?
A named block of code that performs a specific task — can accept parameters — does not return a value — called by its name
Write a procedure in OCR ERL that prints a greeting with a name parameter
procedure greet(name). print("Hello " + name). endprocedure
How do you call a procedure in OCR ERL?
greet("Sam") — write the procedure name followed by any parameters in brackets
What is a function in programming?
A named block of code that performs a task and returns a value — can accept parameters — the returned value can be stored or used directly
Write a function in OCR ERL that squares a number and returns the result
function squared(number). squared = number^2. return squared. endfunction
How do you call a function in OCR ERL and use its return value?
result = squared(4) — stores the returned value in a variable. Or print(squared(4)) — uses the returned value directly
What is the difference between a procedure and a function?
A procedure performs a task but does not return a value. A function performs a task and returns a value that can be used elsewhere in the program
What is a local variable?
A variable declared inside a subprogram — only accessible within that subprogram — cannot be used outside it — destroyed when the subprogram ends
What is a global variable?
A variable declared outside all subprograms — accessible from anywhere in the program — declared using the global keyword in OCR ERL
When should you use local variables instead of global variables?
Local variables are preferred — they prevent accidental changes from other parts of the program — make code easier to debug and maintain — global variables should only be used when the variable genuinely needs to be accessed throughout the entire program
How do you generate a random integer between 1 and 6 in OCR ERL?
myVariable = random(1,6)
How do you generate a random real number between 0.0 and 1.0 in OCR ERL?
myVariable = random(0.0,1.0)
Write OCR ERL code that simulates rolling a dice and prints the result
result = random(1,6). print("You rolled: " + str(result))