Additional Programming Techniques Flashcards

Card 1 of 42

All 42 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. given that the character code for "A" is 65: 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 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 is a record, as a way of storing data?

A data structure holding all the data about one entity, made up of two 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, how do you select more than one field?

e.g. the Name and Year fields

List the field names after SELECT, separated by commas: SELECT Name, Year (not AND). List only the fields asked for: SELECT * returns every field.

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 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].

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.

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 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.

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.

Give two benefits of writing code as sub programs.

Any two, e.g.: the program is better structured; the code is easier to read; a block of code is written once and called whenever needed instead of being repeated; each sub program can be tested on its own; the code is easier to maintain.

What is the difference between a parameter and an argument?
e.g. procedure showArea(width) called with showArea(8)

A parameter is the variable in the sub program's definition that receives data (width). An argument is the actual value passed in when it is called (8).

In OCR Exam Reference Language, how do you declare a global variable?
e.g. a variable lives starting at 3

Put the keyword global before the assignment: global lives = 3.

Why do programs store data in external files?

So data is kept after the program closes (e.g. a saved high score), and so the program can use data that isn't written into its own code.

Why should a file be closed with .close() when a program has finished with it?

Closing releases the file so other programs or users can open it and get the updated version. An unclosed file may stay locked.

In OCR Exam Reference Language, how do you declare an array and fill it with values in one line?
e.g. an array colours holding red, green and blue

List the values in square brackets: array colours = ["red", "green", "blue"] — the size (3) is set by the number of values.

What does random(1, 6) return in OCR Exam Reference Language?

A random integer from 1 to 6, with both 1 and 6 included.

What does random(2.5, 4.5) return in OCR Exam Reference Language?

A random real number from 2.5 to 4.5, both included. random() gives a real when given reals and an integer when given integers.