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

  value that is attached to a function

  value for storage boxes

  storage box for values

  storage box that stores multiple functions

 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

  name,test1,test2,test3

  print and main

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

  boolean

  integer

  string

  float

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

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

  Integer data type as a telephone number is obviously all numbers

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

  Options 1 and 2 are both valid

 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.

  age as integer = 20

  age integer=20

  Dim age 20

  age=20

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

  boolean

  integer

  def and print

  float 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()

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

  None of the above

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

  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 does not compute how to ADD a string and an integer data type

  None of the above

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

  There is no error so this assumption is incorrect

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

  Fontu

  Fixer

  Flipe

  Float

 10. _______________data types store only TRUE or FALSE values

  Bontean

  Button

  Boolean

  Bodelan

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

  4

  1

  2

  3

 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)

  a,c,1

  2,2,2

  1,1,1

  1,2,2

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

  those books cannot be replaced once inside the bag

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

  uppercase

  ASCII

  lowercase

  numerical values

 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 variable and score as a constant

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

  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'

  bending

  boolean functionality

  assignment

  None of the above

 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)

  Because PI is unchanging and has a value that is fixed

  None of the above

  This is a random decision

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

 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: '))

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

  None of the above

  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 you cannot name functions with the word functions in them. Additionally, y is a local variable.

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

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

  None of the above

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

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

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

  TRUE

  FALSE

 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-assigned to the new function

  it has been re-declared as a global variable

  it has been passed as a parameter to the mainmenu function

  None of the above

 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 = Real/Float; Weight = Integert; quantity in stock = integer;Gluten-Free = String

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

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

 25. One advantage of declaring a constant is that …

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

  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)

  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-Integer;2-Real;3-String;4-Char;5-Boolean

  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

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

  None of the above

  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')

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

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

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

  a variable to store the ID number of a cinema member

  a variable to store just the age of a cinema member

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

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

 29. str(x) would ….

  None of the above

  convert the string x to integer

  convert the variable string to x

  convert the variable x to string

 30. int(x) would …

  None of the above

  convert the integer x to string

  convert the variable x to string

  convert the variable x to integer