Test Final #1

 1. Python is a high level programming language that is growing rapidly in popularity and use.



 2. Python was created and developed by ...

  an Australian woman by the name of Gelda Van Minch.

  a Nigerian software engineer named Freddy Kalu

  an Israeli computer scientist called John Benjamin

  a Dutch programmer named Guido Van Rossum

 3. Python has many applications and uses including:

  Game design, web development and machine learning

  the creation of desktop applications

  All of these options are valid!

  use in the backend systems of companies like Youtube, Facebook and Google

 4. One of the simplest things you can do in Python is print "Hello World" to the screen. The following code has been written in python and produces the output: "Hello World"
Python, please print the words "hello world" to the screen. 



 5. What is the OUTPUT from the following program?





 6. A variable can be thought of as ...

  a value box for storage e.g. 2 = x, where 2 is the variable

  a varying piece of code that changes all the time e.g. print(x)

  a storage box for values e.g. x = 2, where x is the variable

  a number, or word that can change itself automatically

 7. What are the variables in the code below?

  2 variables: x and y

  There are no variables in the code

  4 variables: x,y,z and total

  3 variables: x,y and z

 8. Consider the following list of elements and that you are looking for number 7. A linear search would be quicker than a binary search in this example.



 9. In a LINEAR SEARCH, if you were looking for the number 18 in the following list, you would:
9,	15,	13,	18,	11,	8, 7,

  start at the second element

  start at 7 because the term 'linear' means 'last'

  start at the middle as that is where 18 is

  start at the beginning and then continue to look through each element in turn

 10. The Binary search algorithm is often referred to as a 'divide and conquer' algorithm because ...

  it splits the list into two halves as part of the algorithm, and finds the mid point

  it was first suggested by Napoleon who believed in the slogan: "Divide and Conquer"

  it conquers the problem much faster than any other searching algorithm

  it is called 'Binary Search' and the word 'Binary' means divide

 11. The disadvantage of a Binary Search algorithm (as compared to a linear search) is that the list being searched must be SORTED (i.e. in order). A Binary search would not work if the list was unsorted.



 12. Which searching algorithm is more efficient on a sorted list? Linear Search or Binary Search?

  A Linear search is definitely quicker especially for lists that are longer than a million elements

  A Binary search is more efficient because Binary means 'two' and 'two' is better than 'one'

  A Linear search is always much quicker, especially if the list is sorted and very long

  Usually, a Binary search is more efficient on a sorted list, except in the case of a very short list

 13. Suppose you have the following sorted list (see list below) and are using the binary search algorithm. What are the first two numbers you would 'find' if you were looking for the number 8?
[3, 5, 6, 8, 11, 12, 14, 15, 17, 18]

  First 12 (the midpoint) and then the midpoint of the first half of the list which is 6

  First 3 (the first element) and then the midpoint of the list which is 12

  First 18 (the last element) and then 3(the first element)

  You would find 8 immediately if you were using a Binary search algorithm

 14. Follow the logic in the program below. What will the output be on running this program?
def main()
  print("This is the main subroutine"

def subroutine1():
print("This is subroutine1")

def subroutine2:
  print("This is subroutine2")

  "This is subroutine2"

  "This is subroutine1"

  "This is the main subroutine"


 15. The following program will fail to run. What do you need to add on line 4?
def facebook():
	print("Welcome to your facebook profile")

#what do you need to add here to make this program run?



  call facebook:

  def facebook

 16. The following code uses Boolean variables that can be either TRUE or FALSE. What is the output of the following program?
def main():
  #Set Seat to True if booked, False if available
  print("Seat 2 is booked?:",seat2booked)


  Seat 2 is booked?: False

  Seat 2 is booked?: True

  Seat 2 is booked?: seat2booked

  print("Seat 2 is booked?:",seat2booked

 17. The output of this program will be 5.
def main():

def add(x,y):




 18. Can you explain what is happening on line 4?
def main():

def add(x,y):


  Bonding: Here the variables x and y are being encouraged to be close

  Parameter passing: the variables x and y are being passed to the function 'add'

  Addition: This line is adding up the variables x ad y

  Joining: This line is simply joining together x and y using a comma

 19. What is missing from line 8?
def main():
def average():
  return (x+y+z)/3


  The parameter 'avg' is missing. It should be: def average(avg):

  The parameters x,y,z! It should be: def average(x,y,z):

  Nothing is missing!

  The variables a,b,c! It should be: def average(a,b,c):

 20. An architectural company would like a program that calculates the area of a given piece of rectangular land, given its width and height. What would happen on entering 100.01 and 200.23 into the program below?
def areaOfRectangle():
 width= int(input("Enter width:"))
 height=int(input("Enter height:"))


  It will round off the answer to provide one whole number answer

  It will work perfectly and output the area of the piece of land

  Error! the 'int' on line 2 and 3 should be changed to 'str' (string)

  Error! the 'int' on line 2 and 3 should be changed to 'float' which is an appropriate data type for this input

 21. The code below seeks to take the temperature in celsius and convert it into fahrenheit. Can you spot the error in the code below?
celsius=int(input("Enter the temperature in celcius:"))
print("Temperature in farenheit is:",f)

  Line 2: "fahrenheit" should be "f" or Line 3, "f" should be "fahrenheit" so they match

  There are no errors - the code will work fine

  Line 1: "celsius" should be in speech marks

  Line 2: This line should be deleted

 22. A user wishing to convert pounds to dollars inputs '10' into the program below, and the output is: 1010. Can you explain why?
def convert_to_dollars():
  pounds=input("Enter no. of pounds:")


  Because the user input on line 2 is string. It simply puts '10' together twice

  Because dollars are always provided in multiples of 10

  Because there is a mistake on Line 6

  Because 10*2 in python will always give 1010

 23. In the code below the two variables with 'string' data types are 'a' and 'b'. Strings are typically always put in speech marks. e.g. name="Joe"



 24. A education company in the UK is creating a program in which they need to store the names of each student. What is the most appropriate data type for 'first name'?





 25. The following program seeks to find the average of three test scores. How would you complete the code on line 5?
def find_average():
     #What needs to go here before calculating the average?
     print("The average mark is:",average)


  You don't need to do anything on line 5. The code will work fine to calculate the average



 26. What is Validation?

  It is code that looks, sounds and feels both elegant and valid

  It is code that would ensure that only 'valid' input goes into the program

  A type of programmer that typically goes without valid conversation for months

  A type of computerised disease that causes great harm

 27. Another example of Validation is ensuring that a 'password' entered by the user is at least 6 characters long and contains both numbers and special characters to make it secure.



 28. It is not at all important to add validation in programs, as most programs manage just fine without it.



 29. One method of validation is shown below using the 'try' and 'except' coding method. What will happen if you try to enter: "3fx" into the program below?
    value=int(input("Type a number:"))
except ValueError:
    print("This is not a whole number.")

  It will print: "Type a number"

  It will print: "This is not a whole number"

  It will accept '3f' and say 'Thank you'

  It will print: 0

 30. Another simple method of coding validation is to use a LIST to store the accepted inputs. If we wanted lowercase 's' as well as uppercase 'S' to be accepted what would you do?
def mainmenu():
  choice=input("Press S to play:")
  if choice in accepted_choices:
    print("LET'S START")
    print("Sorry, please make a valid choice")

  Delete line 2

  Change line 2 to: accepted_choices=["S" or "s"]

  Change line 2 to: accepted_choices=["s"]

  Change line 3 to: choice=input("Press S and s to play:")

 31. A good programmer will not only build in validation(to prevent invalid and silly input) but may also add validation messages which ...

  will tell the user they are useless and refuse to let them input data again

  will invalidate the program by making it crash

  will warn the user when the input is invalid and give them helpful suggestions

  will scare the life out of the user by coding a hardware explosion.

 32. The if elif else statement is used in Python for ...

  ending a program without any fuss

  decision making, conditional logic and controlling the flow of a program

  creating a new function

  creating a loop

 33. In the following code that uses selection (if statements) what is the output? Note, that the input is num=7.
num = 7
if num >= 0:
    print("Positive or Zero")
    print("Negative number")  

  Positive or Zero


  Negative number


 34. The program below asks the user for their budget and according to the input, presents them with a list of expensive or cheap laptops. If the user enters 4000, what would the output be?
def laptops():
     expensive_laptops=["HP Envy 100","Macbook Air","Microsoft Surface Pro"]
     cheap_laptops=["Asus primary 01","Toshiba X1","ChromeBook 12.2"]
     budget=int(input("How much can you spend on a laptop?"))
     if budget>=2000:
          print("Here are some laptops you could consider",expensive_laptops)
          print("You may want to check out these brands:",cheap_laptops)

  Both lists will be printed because 4000 is double 2000 which is the budget cut off

  The cheap_laptops list will be printed

  4000 - 2000 = 2000. 2000 will be printed

  The expensive_laptops list will be printed

 35. The code below checks an input email for an @. Why will the code fail to run?
def email_check():
     email=input("We would like your email address. Please enter it:")
     if "@" in email:
     print("That is a valid email, Thank you")
     print("That doesn't appear to be a valid e-mail.")

  Because line 4 does not make sense

  Because line 5 and line 7 should be indented

  Because there should be a bracket around the variable email

  Because emails do not have @

 36. In the below code, can you spot and point out the mistake on lines 2 and 3?
def main():
    if x=0:
        print("x is greater than 2")

  There are no mistakes

  The variable x can never ever be equal to zero

  0 is not really a number so cannot be used in Python

  Line 2 is giving x a value of 0 so should be x=0, and line 3 should be: if x==0:

 37. SPOT THE ERROR QUESTION: Spot the error (which line?) and explain what it is.
def main():
print("This is the main function")

  Line 4: blank brackets error

  Line 1: def main syntax error

  Line 3: blank space error

  Line 2: indentation error

 38. SPOT THE ERROR: Sometimes, the error is not on the line that your IDLE tells you it is on, but on the line before. Error checking would tell you there is an error on line 3. Is it right?
def main():
  x=int(input("Enter a number:")

  Yes, in this case the error is on line 3

  No, the error is on line 2 (missing bracket at the end)

  No, the error is that the line before is missing a colon from the end

  No, the error is that the line before is too long

if score>50
  print("You have passed!")
  print("You have failed")

  Line 1 should be score==20 instead of score=20

  Line 2 is missing a colon at the end: e.g. if score > 50:

  Line 4 should be indented further along to the right

  Line 3 should not be indented

 40. The following program will NOT run because the call to the instagram subroutine (at the bottom on line 6) is not to the extreme left (which will cause an error)
def instagram():
    print("Welcome Insta users")



 41. What is wrong with the following code snippet?
if age>15:
  print("You are eligible for a bank account")
    print("Sorry, too young")

  if and else should be on the same level of indentation (else should be moved back)

  There should not be a colon after the if statement in line 2

  There is no problem. Indentation doesn't really matter in Python

  The problem is that the program discriminates against users that are over 15

 42. In programming, operators are symbols that tell the computer to perform specific mathematical, relational or logical operations on data. What operators can you spot in the code below?


  +, - , /, *

  def, main, print


 43. Trace the logic in the code below to predict the output of score on line 4.
def main():
    score=score+1 #this changes the value of score





 44. The formula to calculate the area of a circle is: Area=PI times radius squared. PI=3.14. What would you need to add to line 8 to get the program working correctly.
while True:
	print("Enter 'x' for exit.")
	rad = input("Enter radius of circle: ")
	if rad == 'x':
		radius = float(rad)
		area = 3.14 * radius * radius
		print("Area of Circle",area)

  area = 3.14 * radius * radius

  area = 3.14 * rad * rad

  area = Ï€ x radius x 2

  area = 3.14 * rad(2)

 45. Analyse the code below to see what it does. If you entered two numbers: 2, and 66, it should produce the result: ('Largest of entered two numbers is', 66). What needs to go on line 11?
while True:
	print("Enter 'x' for exit.")
	print("Enter two numbers: ")
	num1 = input()
	num2 = input()
	if num1 == 'x':
		number1 = int(num1)
		number2 = int(num2)
		#What code do you need to write here?
			largest = number1
			largest = number2
		print("Largest of entered two numbers is", largest,"\n")

  if number2 =< or => number1

  if number2 > number1:

  if number1 > number2:

  if number1 < number 2: