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 = 0True 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,elseifandswitchbelong to?Selection.
Which construct do the keywords
for,whileanddo...untilbelong 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
whileloop and ado...untilloop?A
whileloop checks the condition before the loop, so may run zero times;do...untilchecks after, so always runs at least once.In OCR Exam Reference Language, how do you write a
switch/casestatement?e.g. act on the value of
choicebeing 1, 2 or anything elseswitch choice:thencase 1:…case 2:…default:… thenendswitchTrue or false? A
forloop 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
MODreturn?e.g.
11 MOD 3The remainder after integer division.
11 MOD 3=What does the arithmetic operator
DIVreturn?e.g.
11 DIV 3The whole-number part of a division (remainder discarded).
11 DIV 3=What does the operator
^do in OCR Exam Reference Language?e.g.
2 ^ 3Raises to a power (exponentiation).
2 ^ 3=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;DIVgives 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
xis 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
…endifTrue or false?
if score > 5 OR score < 2is 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
whileanddo...untilloops 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. 11What data is stored by the real (float) data type?
A number that can have a decimal/fractional part.
e.g. 18.2What 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()andstr().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/casestatement more limited thanif/elseif?switchonly compares the value of one variable against a list of possible values.if/elseifcan test any conditions, e.g. ranges such asmark >= 70.In a
switchstatement, when does thedefaultcase run?When the variable matches none of the
casevalues. It is written as the last case, just beforeendswitch.When does a
whileloop, and when does ado...untilloop, become an infinite loop?A
whileloop never ends if its condition is always true. Ado...untilloop 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 + 5is 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)givesFalse(andbool(1)givesTrue).