Skip to content
Marlbridge

Flashcards · 9210 · Topic 2

Programming flashcards

47 cards on Topic 2, subtopics 3.2.1 to 3.2.13. Pick a subtopic or study them all.

Study mode needs JavaScript. Every card is listed below, so you can still cover the answers and test yourself.

All 47 cards

For a quick read-through or printing. Cover the right-hand side to test yourself.

3.2.1 Data types

What are the integer and real data types?
Integer: whole numbers. Real: numbers with a fractional part (some languages call this float).
What are the Boolean, character and string data types?
Boolean: True or False. Character: a single character. String: a sequence of characters.
Which data type would you use for a phone number, and why?
String: it may start with 0 or contain + or spaces, and no arithmetic is done on it.

3.2.2 Programming concepts

What is the difference between a variable and a constant?
A variable’s value can change while the program runs. A constant’s value is set once and cannot change.
What are the three basic programming constructs?
Sequence (instructions in order), selection (IF … THEN … ELSE) and iteration (repetition).
What is the difference between WHILE and REPEAT … UNTIL loops?
WHILE tests its condition before the loop body, so the body may run zero times. REPEAT … UNTIL tests after, so the body always runs at least once.
When should you use a FOR loop?
When the number of repetitions is known in advance (count-controlled iteration), e.g. FOR i ← 1 TO 10 … ENDFOR.
What is nesting?
Placing one construct inside another, such as an IF inside a FOR loop or one loop inside another.

3.2.3 Arithmetic operations in a programming language

What is the difference between real division and integer division?
Real division gives the exact result: 11 ÷ 2 = 5.5. Integer division gives only the whole-number part: 11 DIV 2 = 5.
What does the modulus (MOD) operation give?
The remainder after integer division: 11 MOD 2 = 1. Useful for testing whether a number is even (n MOD 2 = 0).
Name the basic arithmetic operations used in programs.
Addition, subtraction, multiplication, real division, integer division, modulus, and rounding or truncation.

3.2.4 Relational operations in a programming language

What are the relational operators?
Equal to, not equal to, less than, greater than, less than or equal to, greater than or equal to (=, ≠, <, >, ≤, ≥).
What type of value does a relational operation produce?
A Boolean: True or False. For example, 7 > 3 is True.
What is the difference between < 50 and ≤ 50 in a condition?
< 50 is False when the value is exactly 50; ≤ 50 is True for 50. Getting this wrong causes off-by-one errors at boundaries.

3.2.5 Boolean operations in a programming language

When is A AND B true, and when is A OR B true?
A AND B: only when both are true. A OR B: when at least one is true.
What does NOT do?
It reverses a Boolean value: NOT True is False.
Write a condition that is true when Score is at least 50 and Lives is more than 0.
Score ≥ 50 AND Lives > 0

3.2.6 Data structures

What is an array?
A data structure that holds a fixed number of items of the same data type under one name, each reached by its index.
What is a two-dimensional array?
An array arranged as a table of rows and columns; each item is reached with two indices, such as scores[2][3].
What is a record?
A data structure that groups related fields, which can be of different data types, about one item, such as a student’s name, age and form.
Why use an array instead of many separate variables?
One name holds all the values, so a loop can process every item, and the code is shorter and easier to change.

3.2.7 Input/output and file handling

Why do programs read from and write to files?
Data in variables is lost when the program ends. A file stores data permanently so it can be used next time the program runs.
What are the steps to read data from a text file?
Open the file for reading, read the data (often line by line in a loop until the end of the file), then close the file.
What is the difference between writing and appending to a file?
Writing replaces any existing contents of the file. Appending adds the new data to the end, keeping what is already there.

3.2.8 String handling operations in a programming language

What is concatenation?
Joining two or more strings together end to end, e.g. "Pass" and "word" give "Password".
What is a substring?
Part of a string taken from a start position for a given length, e.g. the first three characters of "London" are "Lon".
Name common string handling operations.
Finding the length, finding the position of a character, extracting a substring, concatenation, and converting between characters and their character codes or between strings and numbers.
Why might you convert a string to an integer?
Input is often read as a string; it must be converted to a number before arithmetic can be done on it.

3.2.9 Random number generation in a programming language

What is random number generation used for in programs?
Games (dice rolls, shuffling), simulations, and picking test data or quiz questions at random.
How would you simulate rolling a six-sided die?
Generate a random integer between 1 and 6 inclusive, using the language’s random number function.
Why are computer-generated random numbers called pseudo-random?
They are produced by an algorithm from a starting value (seed), so they only appear random; the same seed gives the same sequence.

3.2.10 Subroutines (procedures and functions)

What is a subroutine?
A named block of code that performs a specific task and can be called from elsewhere in the program.
What is the difference between a procedure and a function?
A function returns a value to the code that called it. A procedure carries out a task but does not return a value.
What are parameters?
Values passed into a subroutine when it is called, so the same subroutine can work on different data.
What is the difference between a local and a global variable?
A local variable is declared inside a subroutine and only exists there. A global variable can be used anywhere in the program.
What are the advantages of using subroutines?
Code is reused rather than repeated, programs are easier to read, test and maintain, and different programmers can work on different subroutines.

3.2.11 Structured programming

What is structured programming?
Designing a program as a set of modules (subroutines) using only sequence, selection and iteration, with clear interfaces (parameters and return values).
What are the advantages of the structured approach?
Modules are easier to understand, test and debug on their own, can be reused, and make the program easier to maintain.
Why does good code use meaningful identifiers, indentation and comments?
They make the code easier for other people (and the author later) to read, understand and maintain.

3.2.12 Robust and secure programming

What is data validation? Give three checks.
Checking that input is sensible and allowed before it is used. Examples: range check, length check, presence check, type check, format check.
What is authentication in a program?
Checking that a user is who they claim to be, for example by asking for a username and password.
What are normal, boundary and erroneous test data?
Normal: typical valid data, should be accepted. Boundary: on and just either side of the limits. Erroneous: invalid data, should be rejected.
What is the difference between a syntax error and a logic error?
Syntax error: breaks the rules of the language, so the program will not run. Logic error: the program runs but produces the wrong output.

3.2.13 Classification of programming languages and translators

What is the difference between high-level and low-level languages?
High-level: English-like, easier to write and debug, portable between processors. Low-level (machine code and assembly): close to the hardware, processor-specific, give direct control and can be very efficient.
What is machine code?
Instructions in binary that the processor can execute directly.
What is the difference between a compiler and an interpreter?
A compiler translates the whole program into machine code before it runs, producing an executable file. An interpreter translates and runs the program one line at a time, each time it runs.
What does an assembler do?
Translates assembly language (mnemonics such as ADD and LDA) into machine code.