03 - Variables and Data Types

 1. A company that makes cakes stores data about the cakes it makes! What is the most appropriate data type for storing the following information?
INFORMATION                 EXAMPLE OF DATA                 DATA TYPE?
1.WEIGHT(KG)						1.23							?
2. FLAVOUR							APPLE							?

  1. String and 2.Float/Real

  1. String 2. String

  1. Float/Real and 2. String

  1. String 2. Integer

 2. A game user needs to press a button to request the game to stop. State the data type that you would use to record whether the button has been pressed or not.

  String (either Yes or No)

  Integer (an integer that records the strength of the button press)

  Real (time in minutes could be recorded)

  Boolean (e.g True or False)

 3. In the following code, if line 2 is executed out (the comment is removed), the output will be 46, instead of the current 2323. Why?

  because line 2 is converting the string variable to an integer that can be worked with

  because line 2 is converting the integer variable into a definite integer

  because line 2 is creating an integer which has the value of 46

  because line 2 converts every variable in the code into a string

 4. Mr Smith, a chemistry teacher, writes the following code to analyse the test scores (rounded to the nearest integer) of a pupil. Line 3 is missing. Complete line 3 so that the program calculates the total score of the pupil





 5. The program below calculates the cost of a delicious pizza in pounds(£) at a fast food restaurant. A standard plate costs £3.50 with additional costs for sauce and eating in the restaurant. List all the variables in this program.

CONST standard = 3.50
INT sauces = 0
BOOL eat_in = false
sauces = INPUT("How many sauces?:")
eat_in = INPUT("Is customer eating in?")
IF eat_in ==true THEN
	print(standard+0.5*sauces + 1)

  standard, sauces, eat_in, input

  standard, sauces, eat_in, 0.5

  sauces, eat_in, standard

  sauces, eat_in

 6. A coder has suggested that the 0.5 (attached the additional sauces) should have been declared as a constant. Why is this?
#1 Because it doesn't need to be changed as the program is running which makes
it a constant

#2 Because it would  make it easier to update the value. If the restaurant wanted
to increase the charge of sauces to 0.7, it would only need to be made in one place.

#3 It would provide more meaningful and maintainable code - a constant name of
saucesCost is clearer than just 0.5. 

  #1 and #3 are the two correct explanations

  #2 is the only correct explanation

  #1, #2 and #3 are all correct explanations

  #1 is the only correct explanation

 7. The following code seeks to calculate the value of an investment after one year. Can you spot any problems with the code?
#In the following program all constants are in UPPER CASE

rate as REAL
interest as INT
INVESTMENT = input("Enter investment:"))
rate=input("Enter rate:")
for x = 1 to 12
next x


  option 2) interest is declared as an integer but it should take non-integer e.g. 3.12

  option 1 and option 2 are valid problems with the code shown.

  option 3) There are no problems

  option 1) investment is declared as a constant but its value changes

 8. A swimming pool club issues a card to its customers that they swipe to get in and out. If the pool is full i.e. 20 members, then the card denies the customer access. What are two variables and one constant that could be used to code this program?
#option 1

CONSTANT (int) boolean variable - access granted/denied
VARIABLES 1. (int) max_allowed_people
          2. current_members_no_in_pool

#option 2

CONSTANT (int) max_allowed_people 
VARIABLES: 1. boolean variable - access granted/denied 
           2. current_members_no_in_pool

  #option 2

  '#both options could potentially be valid

  #neither option

  #option 1

 9. The following algorithms each use data values. Can you work out which of these is using fixed values?(some of them use variable data)
Algorithm A

Add 4 to 5
Print the result

Algorithm B

Ask the user to type in a whole number
Add 5 to that number
Print the result

Algorithm C

If 4 is less than 5
	Add 5 to 4
	Print the result
	Print "no"
Algorithm D

Ask the user to type in a whole number
If the number typed in is less than 5
	Add 5 to the number typed in
	Print the result
	Print "no"

  Algorithms B and C are using fixed values

  Algorithms A and B are using fixed values

  Algorithms A and C are using fixed values (4 and 5).

  Algorithms A and D are using fixed values (4, 5).

 10. Fill in the blanks below:
__________are whole numbers represented as binary values. Most programming 
languages provide a data type called 'int' for short.

Floating point numbers are numbers that have a ____________ part, usually 
represented using two components: the main number and the fractional part, 
each of which is a binary number. This is known as a floating-point
representation. Most programming languages provide one or more data 
types based on floating-point representations. They are usually 
given names like 'float', 'single', 'double', 'real' or 'longreal'.

  integers / fractional

  real numbers / integering

  integers / string

  fractional numbers / real number

 11. The term "Casting" refers to:

  Converting between variable data types (e.g. string to integer)

  Casting is another word for printing the output

  Boolean algebra

  Rounding up of real numbers

 12. The statement "A or B" implies that the variable type of A and B is:





 13. Fill in the blanks in the paragraph below.
______ variables are declared outside any function, and they can be accessed#
(used) on any function in the program. ________ variables are declared inside
a function, and can be used only inside that function. 
It is possible to have local variables with the same name in different functions.

  Constant / Local

  Global / String

  Global / Local

  Local / Global

 14. In Physics, we can look at an object falling from rest. The formula for its velocity after a time of t seconds is shown below. Which of the following could be set as a CONSTANT?
Formula for calculating velocity: 

v = g * t where g is the acceleration of gravity. 

  time, t could be declared as a constant T

  the * operator is a constant

  gravity, g could be declared as a constant G

  velocity, v, could be declared as a constant V

 15. Why would it be better to rename the variable "b2" below to "result"?

  because variable names (identifiers) should be meaningful and describe their values

  It would be better because b2 is too close to b1 and this could be confusing

  It wouldn't be better as 'result' is not describing the variable in any way at all

  It would be better because variables that output a result should start with 'r'