Preview

2 - Introducing Selection, Casting, more coding

 1. _________ is when you find an issue in the program that you have written and repair it.

  Demarking

  Defragmenting

  Debugging

  Decrementing

 2. Analyse the code below. Which statement is most accurate?
def robot():
  print("I am a Robot")
  name=input("Who are you?")
  print("Nice to meet you,",robot)
  robot=input("So how old are you?")

robot()

  There are four variables in this program. They are: robot,name,print and input.

  There are two variables in this program but one will not work.

  There are two variables in this program. The second 'robot' is poorly named.

  There is just one variable in this program and that is the name variable.

 3. Why, if at all, will the following program fail to run?
def knockknock():
  response=input("Knock Knock:")
  response2=input("Boo...")
  respons3=input("Oh no...that made you cry!?!?!")
  
  knockknock()
  
  

  Because line 6 should be calling the function but is instead indented inside the function.

  The program will actually run fine. There are no errors.

  Because line 6 should not have opening and closing brackets after it.

  There is an indentation error on line 2.

 4. Can you spot the error?
def coronachecker():
  Print("Hello there")

coronachecker()

  There are no errors

  Line 1 - should be: def coronachecker:

  Line 2 - it should be: print("Hello there") -no capital P

  Line 4 - should be: coronachecker:()

 5. Analyse the following code. If the user enters the number 4, the output is 54 instead of 5+4 = 9. Why?
def add():
  z=5
  secret_number=input("Enter number:")
  print(str(z)+secret_number)

add()

  Because z is a letter of the alphabet and not a number.

  Because line 3 is asking for a number, but it should be asking for an integer.

  Because you cannot have a variable name that is just a single letter.

  Because on line 4 we have cast the integer z into a string. The command puts them together and does not add.

 6. How would you describe what is written on lines 2 and 5?
def add():
  #z is a variable that holds the value 5
  z=5
  secret_number=input("Enter number:")
  #next, we have the output command
  print(str(z)+secret_number)

add()

 7. Name the data type that would be used for the following:
"Hello World"

  Integer

  Boolean

  Float/Real

  String

 8. Name the data type that would be used for the variable pocketmoney that stores the value: ?3.50

  Boolean

  Integer

  String

  Float/Real

 9. Steve is creating a cinema booking system. A seat can either be TRUE or FALSE. What data type should he use for seat?

  Float/Real

  String

  Integer

  Boolean

 10. There may be times when you want to specify a type on to a variable. This can be done with ______.

 11. Analyse the following code. If the input is 2, what is the output?
def example():
  number1=5
  number2=input("Enter number:")
  print(int(number2)+number1)

example()

  3

  25

  5

  7

 12. In the above code (previous question), on what line, if any, can you spot an example of casting?

  Line 2

  There is no casting in this example code

  Line 1

  Line 4

 13. The comparison (double equal sign) operator ( == ) is used to assign a value to a variable or a constant.

  TRUE

  FALSE

 14. Why will the following code fail to run?
def subtract():
  x==2
  y==3
  print(y-x)

subtract()

  Because you cannot print a subtraction command in the way shown.

  Actually, the code will run. It's a trick question!

  Because you cannot have variables that are just letters.

  The comparison == operator instead of the assignment operator = has been used.

 15. Analyse the following use of the / and // operators. Which statement is correct?
print(7/3)
print(7//3)

  Line 1 will output 2.333 and Line 2 will output 2

  Line 1 and 2 will both output 2.3333

  Line 1 and 2 will both output 2

  Line 1 will output 2 and Line 2 will output 2.333

 16. Analyse the following code which contains an if statement and a comparison operator - the greater than sign. What is the output?
a = 33
b = 2

if b > a:
  print("b is greater than a")
else:
  print("a is greater than b")

  ERROR

  if b>a:

  a is greater than b

  b is greater than a

 17. Consider the code in the question above. If you change the value contained in the variable b to 555, what will the output be?

  b is greater than a

  ERROR

  a is greater than b

  if b>a:

 18. In the following program, the secret agent enters the numbers 777 which should let him in, but instead there is an error. Why?
secret_code=777

passcode=input("Enter secret code:")
if passcode==code:
	print("Access granted")
else:
	print("Access denied")

  Line 1 should be: secret_code(777):

  Line 4 should be: if secret_code=777:

  Line 4 should be: if passcode==secret_code:

  Line 4 shoud be: if passcode=777:

 19. There is something quite small missing from this code that will prevent it from working. Can you spot the error?
def checker():
  cough=input("Do you have a cough - yes/no?:")
  if cough=="yes" or cough=="YES"
    print("Stay home please....")
  else:
    print("Great...stay safe")
checker()

  Line 3 - missing colon

  There are no errors

  Line 2 - extra bracket needed

  Line 4 - should not have an indent

 20. The three main programming constructs are 1) Sequence, 2)Selection and 3)Itereation. Selection refers to:

  Loops and repeating conditions

  Selecting the right function to create your program

  Selecting the right data type or operator

  the use of conditional logic in your code - e.g. IF ELSE statements