Programming Fundamentals & Data Types Flashcards

Card 1 of 42

All 42 cards in this deck

What is a variable in programming?

A named memory location whose value can change while the program is running.

What is a constant in programming?

A named value that is set once and cannot change while the program is running.

Give one reason to use a constant instead of a variable in a program.

The value cannot be changed accidentally, and it only needs editing in one place.

What is an assignment statement?

e.g. in OCR Exam Reference Language

A statement that stores a value in a variable using ==.
e.g. score = 0

True or false? == can be used to assign a value to a variable.

False. == tests for equality; a single = is used for assignment.

Name the three basic programming constructs.

Sequence, selection (branching) and iteration.

Which construct do the keywords if, elseif and switch belong to?

Selection.

Which construct do the keywords for, while and do...until belong to?

Iteration.

What is the difference between count-controlled and condition-controlled iteration?

Count-controlled repeats a set number of times; condition-controlled repeats until/while a condition is met.

What is the difference between a while loop and a do...until loop?

A while loop checks the condition before the loop, so may run zero times; do...until checks after, so always runs at least once.

In OCR Exam Reference Language, how do you write a switch/case statement?

e.g. act on the value of choice being 1, 2 or anything else

switch choice: then case 1: … case 2: … default: … then endswitch

True or false? A for loop can only count up in steps of 1.

False. A step can be given, e.g. for i = 0 to 10 step 2.

What does the arithmetic operator MOD return?

e.g. 11 MOD 3

The remainder after integer division.
11 MOD 3 = 22

What does the arithmetic operator DIV return?

e.g. 11 DIV 3

The whole-number part of a division (remainder discarded).
11 DIV 3 = 33

What does the operator ^ do in OCR Exam Reference Language?

e.g. 2 ^ 3

Raises to a power (exponentiation).
2 ^ 3 = 88

List the six comparison operators used in OCR Exam Reference Language.

==, !=, <, <=, >, >=

What does the comparison operator != mean?

Not equal to.

True or false? The / operator always gives a whole-number answer.

False. / gives a real (decimal) result; DIV gives the whole-number part.

True or false? In x = 5 + 2 * 3, the multiplication is done before the addition.

True. Normal arithmetic precedence applies, so x is 11.

Name the three common Boolean operators.

AND, OR and NOT.

When is a condition joined by AND true?

Only when both conditions are true.

When is a condition joined by OR true?

When at least one of the conditions is true.

What does the Boolean operator NOT do?

e.g. NOT (age > 18)

Reverses the result of a condition.
NOT (age > 18) is true when age is 18 or less.

How do you write a selection statement that runs only when two conditions are both met?

e.g. age is at least 13 and a member is true

if age >= 13 AND member == true then
…
endif

True or false? if score > 5 OR score < 2 is true when score is 7.

True. OR only needs one side to be true, and 7 > 5.

True or false? Boolean operators can only be used in selection statements, not in loops.

False. Conditions in while and do...until loops can also use AND, OR and NOT.

Name the five data types you must know for OCR GCSE Computer Science.

Integer, real (float), Boolean, character and string.

What data is stored by the integer data type?

A whole number, positive or negative.
e.g. 11

What data is stored by the real (float) data type?

A number that can have a decimal/fractional part.
e.g. 18.2

What data is stored by the Boolean data type?

One of two values: true or false.

What is the difference between the character and string data types?

A character is a single letter, digit or symbol; a string is a sequence of characters.

What is casting?

Temporarily converting data from one data type to another.
e.g. int("25") converts a string to an integer.

Why is casting often needed after using input()?

input() returns a string, so it must be cast (e.g. to an integer) before it can be used in a calculation.

Which casting functions convert a value to an integer, to a real and to a string?

int(), float() / real() and str().

True or false? A telephone number should be stored as an integer.

False. It should be a string, because leading zeros must be kept and no arithmetic is done on it.

How is a switch/case statement more limited than if/elseif?

switch only compares the value of one variable against a list of possible values. if/elseif can test any conditions, e.g. ranges such as mark >= 70.

In a switch statement, when does the default case run?

When the variable matches none of the case values. It is written as the last case, just before endswitch.

When does a while loop, and when does a do...until loop, become an infinite loop?

A while loop never ends if its condition is always true. A do...until loop never ends if its condition is never true.

In what order are the Boolean operators evaluated in a condition?

Brackets first, then NOT, then AND, then OR.

True or false? In if total + 5 > 20 then, the comparison > is checked before the addition.

False. Arithmetic is done first, so total + 5 is worked out and then compared with 20.

Why is it important to choose the correct data type for a variable?

So the data behaves as intended and the program is robust. With the wrong type, code can crash or give wrong output, e.g. "4" + "2" joins two strings to give "42" instead of adding to 6.

What does the casting function bool() do?
e.g. bool(0)

Converts a value to a Boolean — bool(0) gives False (and bool(1) gives True).