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 = 0

True 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

  1. Assign the input to a variable: name = input("Enter name")
  2. Display output with print: print("Hello " + name)

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.

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

  1. Test the first condition: if score >= 80 then print("A")
  2. Add further tests: elseif score >= 60 then print("B")
  3. Catch the rest and close: else print("C") then endif

What are the steps to write a count-controlled loop in OCR Exam Reference Language?

e.g. print the numbers 1 to 5

  1. Set counter, start and end: for i = 1 to 5
  2. Put the repeated code inside: print(i)
  3. Close the loop: next i

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

What are the steps to write a while loop in OCR Exam Reference Language?
e.g. keep doubling total, which starts at 1, while it is under 50

  1. Set the variable used in the condition before the loop: total = 1
  2. Write while and the condition: while total < 50
  3. Write the code to repeat, then close with endwhile: total = total * 2 then endwhile