Preview

04 - Variables Types Constants Assignment

 1. You can think of a variable as a ….
Note: The following video from www.teachyourselfpython.com provides an overview of variables and data types

  storage box for values

  storage box that stores multiple functions

  value that is attached to a function

  value for storage boxes

 2. In the following code, can you point out the variables?
def main():
  print("Here are your test scores - each one was out of /10")
  name="Joe Bloggs"
  test1=9
  test2=5
  test3=2
  print("Welcome",name)
  print("Test1:",test1)
  print("Test2:",test2)
  print("Test3:",test3)
  print("Your tests added together give a total of:", test1+test1+test3)


main()

  test1

  name

  print and main

  name,test1,test2,test3

 3. In the following variable declaration, what 'type' of variable is 'name'?
name="Jonathan"

  float

  boolean

  string

  integer

 4. What variable type (data type) would telephone number be?

  Options 1 and 2 are both valid

  String, because it may contain brackets or the '+' sign for international codes

  String, because telephone numbers may have a leading zero which we don't want cut off

  Integer data type as a telephone number is obviously all numbers

 5. In languages like VB.Net you would declare a variable like this: Dim age as integer = 20. In Python this would be:
Note:The "variable declaration" or "variable initialization" happens automatically when we assign a value to a variable.

  Dim age 20

  age=20

  age integer=20

  age as integer = 20

 6. Which items on the following list are NOT valid data types?
boolean, string, integer, float, print, def

  integer

  float and print

  boolean

  def and print

 7. Run or trace the logic of this program. It should give us 16 but it gives us 20 instead. Why?
def main():
  print("Here are your test scores - each one was out of /10")
  name="Joe Bloggs"
  test1=9
  test2=5
  test3=2
  print("Welcome",name)
  print("Test1:",test1)
  print("Test2:",test2)
  print("Test3:",test3)
  print("Your tests added together give a total of:", test1+test1+test3)


main()

  None of the above

  The error occurs because you cannot have variable integers that are above 16

  The error occurs because the wrong variables are being added up. test1+test1 instead of test1+test2+test3

  There is no error

 8. The following code would come up with an error. Can you say why?
x=1
name="Joe"
print(x+name)

  Python cannot add integers of 1 to names, as this is too difficult a calculation

  Python does not compute how to ADD a string and an integer data type

  There is no error so this assumption is incorrect

  None of the above

 9. Another variable type you may have learned about (usually decimal numbers) is F _ _ _ _

  Fontu

  Flipe

  Float

  Fixer

 10. _______________data types store only TRUE or FALSE values

  Boolean

  Bodelan

  Button

  Bontean

 11. If x = 2, and y = 3, and z=x+x, what would be printed with the command: print(z)

  3

  2

  4

  1

 12. In the following code, the equal sign is assigning a particular value to x. so that the final value of x will always be 3.
if x==3:
	print(x)

  True

  False

 13. Analyse the slightly peculiar lines of code below. What is the final output?
a = 1
b = a
a = b
c=2
a = b = c = c
print(a)
print(b)
print(c)

  1,2,2

  a,c,1

  2,2,2

  1,1,1

 14. Read the following excerpt / definition of constants below and decide whether it is true or false
A constant is a type of variable whose value cannot be changed. It is helpful to think of constants as containers that hold information which cannot be changed later.

  FALSE

  TRUE

 15. If a constant was a bag in which you stored some books, then …

  those books cannot be replaced once inside the bag

  those books can be replaced with bags, so as to expand the program

  those books can be replaced at any time

  those books cannot be replaced by other books once inside the bag, but can be replaced by bags

 16. Python convention is that constants are written in ______________ and underscores if there is more than one word.

  uppercase

  numerical values

  lowercase

  ASCII

 17. In a video game, gravity and score are going to be used to completed the mechanics and functionality of the game. Which statement is true?
variables_types_constants_assignment_gravity.jpg

  Gravity is best declared as a constant and score as a variable

  Gravity is best declared as a variable and score as a constant

  Gravity and score are both best off being declared as variables

  Gravity and score can both be declared as constants as they stay the same

 18. In Python the "=" sign is used for _____________ and the "==" sign (double equals) means 'is equal to'

  boolean functionality

  None of the above

  bending

  assignment

 19. The following python code is for finding the area of a circle. Why is PI a constant?
PI = 3.14
radius = float(input(' Please Enter the radius of a circle: '))
area = PI * radius * radius
circumference = 2 * PI * radius
 
print(" Area Of a Circle = %.2f" %area)
print(" Circumference Of a Circle = %.2f" %circumference)

  This is a random decision

  None of the above

  Because PI is a variable that will change and so it is better to 'fix' it

  Because PI is unchanging and has a value that is fixed

 20. Note that in the following line the 'radius' variable is turned into a float data type. Why?
radius = float(input(' Please Enter the radius of a circle: '))

  None of the above

  Because the float data type allows decimal values (e.g. 3.32) and the radius is likely to be a float, not integer

  There is no particular reason for this - the integer data type could have been used

  This is an error - the string data type should have been used

 21. Why does the code stop at print(x)?
def function1():
  x=3
  
  
y=2
def function2():
  print("this is function2")
  
  
print(y)
print(x)

  because x is a global variable and cannot be accessed by printing

  because x is a local variable declared inside function1 and therefore cannot be accessed. y is global

  None of the above

  because you cannot name functions with the word functions in them. Additionally, y is a local variable.

 22. In the following example, x is a local variable.
x = "testandtrackio"

def foo():
    print("x inside :", x)

foo()
print("x outside:", x)

  FALSE

  TRUE

 23. In the following example 'username' is a local variable but can be accessed outside of its function because …
def welcome():
  username="joebloggs"
  mainmenu(username)

def mainmenu(username):
  print("Welcome,",username)


welcome()

  it has been re-declared as a global variable

  None of the above

  it has been re-assigned to the new function

  it has been passed as a parameter to the mainmenu function

 24. An iphone app about recipies has the fields: flavour,weight(in KG), quantity in stock, and Gluten-Free?. What data types would be best suited?
Note: please note that it is extremely important to have meaningful identifier names (identifier = variable names)

  Flavour = String; Weight = Integer; quantity in stock = Boolean;Gluten-Free = Integer

  Flavour = String; Weight = Real/Float; quantity in stock = integer;Gluten-Free = Boolean

  Flavour = Integer; Weight = Real/Float; quantity in stock = string;Gluten-Free = Boolean

  Flavour = Real/Float; Weight = Integert; quantity in stock = integer;Gluten-Free = String

 25. One advantage of declaring a constant is that …

  it can hold far more data than a variable and is therefore more efficient

  it is similar to a variable but far more powerful (speed efficiency)

  It is compiled quicker by the CPU so the use of constants is preferred to variables

  you can update its value once and it will update everywhere in the program

 26. In reference to the image below, fill in the blanks for 1 to 5.
variables_types_table_image.png

  1-Real;2-Integer;3-Char;4-String;5-Boolean

  1-Boolean;2-Real;3-Char;4-String;5-Integer

  1-Integer;2-Real;3-Char;4-String;5-Boolean

  1-Integer;2-Real;3-String;4-Char;5-Boolean

 27. In what situation might you use the 'char' data type to store data?
Note: char is short for 'character'

  If the answer in a multiple choice quiz was just 8 words long (e.g. a small sentence)

  None of the above

  if the answer in a multiple choice quiz was just 8 letters long (1 byte)

  If the answer in a multiple choice quiz was just required to hold 1 byyte of data (e.g. a single letter answer like 'A', or 'B')

 28. In which of the following situations might you use a Boolean data type?

  a variable to store just the age of a cinema member

  a variable to see if a seat in a cinema is booked or not

  a variable to store the username as well as the password of cinema members

  a variable to store the ID number of a cinema member

 29. str(x) would ….

  None of the above

  convert the variable x to string

  convert the string x to integer

  convert the variable string to x

 30. int(x) would …

  convert the variable x to integer

  convert the integer x to string

  convert the variable x to string

  None of the above