Preview

2 - Introducing Selection, Casting, more coding

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

  Decrementing

  Demarking

  Defragmenting

  Debugging

 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 is just one variable in this program and that is the name variable.

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

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

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

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

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

  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 4 - should be: coronachecker:()

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

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

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

 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

  String

  Float/Real

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

  String

  Float/Real

  Integer

  Boolean

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

  Boolean

  String

  Float/Real

  Integer

 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

  7

  5

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

  Line 1

  Line 4

  There is no casting in this example code

  Line 2

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

  FALSE

  TRUE

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

subtract()

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

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

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

  Because you cannot have variables that are just letters.

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

  a is greater than b

  ERROR

  b is greater than a

  if b>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?

  ERROR

  b is greater than a

  if b>a:

  a is greater than b

 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 4 should be: if secret_code=777:

  Line 4 should be: if passcode==secret_code:

  Line 4 shoud be: if passcode=777:

  Line 1 should be: secret_code(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()

  There are no errors

  Line 4 - should not have an indent

  Line 2 - extra bracket needed

  Line 3 - missing colon

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

  Selecting the right data type or operator

  Selecting the right function to create your program

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

  Loops and repeating conditions