Programming Fundamentals & Data Types Flashcards
All 39 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.What are the steps to take a value from the user and display a message using it in OCR Exam Reference Language?
e.g. ask for a name then greet the user
- Assign the input to a variable:
name = input("Enter name") - Display output with print:
print("Hello " + name)
- Assign the input to a variable:
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.What are the steps to write a selection statement with more than two outcomes in OCR Exam Reference Language?
e.g. grade A if score is 80+, B if 60+, otherwise C
- Test the first condition:
if score >= 80 then print("A") - Add further tests:
elseif score >= 60 then print("B") - Catch the rest and close:
else print("C")thenendif
- Test the first condition:
What are the steps to write a count-controlled loop in OCR Exam Reference Language?
e.g. print the numbers 1 to 5
- Set counter, start and end:
for i = 1 to 5 - Put the repeated code inside:
print(i) - Close the loop:
next i
- Set counter, start and end:
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
… thenendifTrue 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.
What are the steps to write a
whileloop in OCR Exam Reference Language?
e.g. keep doublingtotal, which starts at 1, while it is under 50- Set the variable used in the condition before the loop:
total = 1 - Write
whileand the condition:while total < 50 - Write the code to repeat, then close with
endwhile:total = total * 2thenendwhile
- Set the variable used in the condition before the loop: