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 firstName and surname with a space between them

With the + operator (concatenation).

fullName = firstName + " " + surname

How do you find the number of characters in a string?

e.g. word = "computer"

word.length — this gives 8.

What does .substring(x , i) return?

e.g. subject = "computer", subject.substring(3,5)

i characters starting at index x, where indexes start at 0 — returns "puter".

What do .left(i) and .right(i) return?

e.g. word = "computer", then word.left(3) and word.right(2)

.left(i) gives the first i characters and .right(i) the last i characters — "com" and "er".

What do .upper and .lower do?

e.g. "Cat".upper

.upper converts every letter of the string to uppercase and .lower converts every letter to lowercase — gives "CAT".

What do ASC() and CHR() do?

e.g. ASC("A") and CHR(66)

ASC() returns the character code of a character and CHR() returns the character for a code — 65 and "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"

  1. Take the characters needed: part = surname.left(3) → "Wat"
  2. Convert the case: part = part.lower → "wat"
  3. Concatenate: username = part + id → "wat42"

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 Sam as 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

  1. Open it: myFile = open("data.txt")
  2. Loop to the end: while NOT myFile.endOfFile() … print(myFile.readLine()) … endwhile
  3. Close it: myFile.close()

What are the steps to create a new file and store a name typed in by the user?

e.g. the file names.txt

  1. Create it: newFile("names.txt")
  2. Open and write: myFile = open("names.txt") then myFile.writeLine(input("Name?"))
  3. Close it: myFile.close()

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 FROM command specify?

Which table the data is retrieved from.

In SQL, what does the WHERE command do?

e.g. WHERE Year = 11

Filters the records so only those meeting the condition are returned — only records whose Year is 11.

In SQL, what does LIKE with the % wildcard do?

e.g. WHERE Name LIKE "S%"

Matches incomplete words or patterns — returns records whose Name begins with S.

In an SQL WHERE clause, what is the difference between AND and OR?

AND means all the conditions must be true; OR means 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

  1. State the fields: SELECT Name
  2. State the table: FROM Students
  3. State the condition: WHERE Year = 11

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 is scores[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 scores holding 3 numbers

  1. Declare with identifier and size: array scores[3]
  2. Assign using an index: scores[0] = 10
  3. Read it back with the same index: print(scores[0]) → 10

What are the steps to total all the elements of a 1D array?

e.g. actNumbers holding 10, 14, 6

  1. Set a total variable to 0: count = 0
  2. Loop over every index: for x = 0 to 2 … count = count + actNumbers[x] … next x
  3. Output after the loop: print(count) → 30

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 score with 3 rows and 2 columns

Declared 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 of tickets at £5 each

  1. Define it with a parameter: function calculatePrice(tickets)
  2. Calculate using the parameter: price = tickets * 5
  3. Return the value: return price then endfunction

How do you call an existing function and keep the value it returns?

e.g. use calculatePrice() for 3 tickets and store the result in totalPrice

totalPrice = 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 greet that prints a welcome for a name

  1. Declare it with any parameters: procedure greet(name)
  2. Write the task, then end it: print("Welcome " + name) … endprocedure
  3. Call it by name with arguments: greet("Sam")

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. names holding "Ali", "Bea", "Cai"

  1. Generate a random index in range: i = random(0,2)
  2. Use it as the index: chosen = names[i]
  3. Use the value: print(chosen) → e.g. Bea

True or false? Spaces and punctuation are counted by .length when finding the number of characters in a string.

True. .length counts every character in the string, including spaces and punctuation.

How do you pass an array into a sub program?
e.g. pass the array scores into a procedure printAll

Use 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 function doubleAll that doubles each element of an array of numbers

  1. Define it with an array parameter: function doubleAll(nums)
  2. Loop through and change each element: nums[i] = nums[i] * 2
  3. Return the array: return nums, called as newList = doubleAll(scores)