Preview

Test Final #1

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

  False

  True

 2. Python was created and developed by ...

  an Israeli computer scientist called John Benjamin

  a Nigerian software engineer named Freddy Kalu

  a Dutch programmer named Guido Van Rossum

  an Australian woman by the name of Gelda Van Minch.

 3. Python has many applications and uses including:

  All of these options are valid!

  Game design, web development and machine learning

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

  the creation of desktop applications

 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. 

  True

  False

 5. What is the OUTPUT from the following program?
x=2
y=4
z=x+y
print(z)

  6

  x

  z

  0

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

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

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

  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?
x=2
y=8
z=0
total=x+y+z
print(total)

  3 variables: x,y and z

  2 variables: x and y

  4 variables: x,y,z and total

  There are no variables in the code

 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.
2,7,5,88,9,73,23,90,18

  False

  True

 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 beginning and then continue to look through each element in turn

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

  start at the middle as that is where 18 is

  start at the second element

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

  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 splits the list into two halves as part of the algorithm, and finds the mid point

  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.

  False

  True

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

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

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

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

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

 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 18 (the last element) and then 3(the first element)

  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

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

  "This is the main subroutine"

  "print"

  "This is subroutine2"

  "This is subroutine1"

 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:

  print(facebook())

  def facebook

  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
  seat1booked=True
  seat2booked=False
  seat3booked=False
  seat4booked=True
  
  print("Seat 2 is booked?:",seat2booked)

main()

  Seat 2 is booked?: seat2booked

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

  Seat 2 is booked?: True

  Seat 2 is booked?: False

 17. The output of this program will be 5.
def main():
  x=2
  y=3
  add(x,y)

def add(x,y):
  print(x+y)

main()

  False

  True

 18. Can you explain what is happening on line 4?
def main():
  x=2
  y=3
  add(x,y)

def add(x,y):
  print(x+y)

main()

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

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

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

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

 19. What is missing from line 8?
def main():
  x=10
  y=20
  z=30
  avg=average(x,y,z)
  print(avg)
 
def average():
  return (x+y+z)/3

main()

  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:"))
 area=width*height
 print(area)

areaOfRectangle()

  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 'float' which is an appropriate data type for this input

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

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

 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:"))
fahrenheit=(celsius*1.8)+32
print("Temperature in farenheit is:",f)

  Line 2: This line should be deleted

  Line 1: "celsius" should be in speech marks

  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

 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:")
  dollars=pounds*2
  print(dollars)

convert_to_dollars()

  Because 10*2 in python will always give 1010

  Because dollars are always provided in multiples of 10

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

  Because there is a mistake on Line 6

 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"
a="apple"
b="number"
c=1
d=5
e=True
f=False
g=202.27

  False

  True

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

  string

  integer

  boolean

  float

 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():
     test1=30
     test2=100
     test3=99
     #What needs to go here before calculating the average?
	 average=total/3     
     print("The average mark is:",average)
 
find_average()

  average=total

  total=test1+test2+test3

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

  test1=test2+test3

 26. What is Validation?

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

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

  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.

  True

  False

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

  False

  True

 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?
try:
    value=int(input("Type a number:"))
except ValueError:
    print("This is not a whole number.")

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

  It will print: "Type a number"

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

  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():
  accepted_choices=["S"]
  choice=input("Press S to play:")
  if choice in accepted_choices:
    print("LET'S START")
  else: 
    print("Sorry, please make a valid choice")
   
mainmenu()

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

  Delete line 2

  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 warn the user when the input is invalid and give them helpful suggestions

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

  will invalidate the program by making it crash

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

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

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

  creating a new function

  creating a loop

  ending a program without any fuss

 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")
else:
    print("Negative number")  

  Else

  0

  Negative number

  Positive or Zero

 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)
     else:
          print("You may want to check out these brands:",cheap_laptops)
     laptops()
 
laptops()

  The expensive_laptops list will be printed

  The cheap_laptops list will be printed

  4000 - 2000 = 2000. 2000 will be printed

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

 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")
     else:
     print("That doesn't appear to be a valid e-mail.")
 
email_check()

  Because emails do not have @

  Because there should be a bracket around the variable email

  Because line 5 and line 7 should be indented

  Because line 4 does not make sense

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

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

  The variable x can never ever be equal to zero

  There are no mistakes

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

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

  Line 3: blank space error

  Line 1: def main syntax error

  Line 4: blank brackets 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:")
  print(x)
 
main()

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

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

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

  Yes, in this case the error is on line 3

 39. SPOT THE ERROR:
score=20
if score>50
  print("You have passed!")
else:
  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")
     
     
     
    instagram()

  True

  False

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

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

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

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

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

 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?
x=2
y=6
z=x+y
q=x/y
r=x*y
print(r*3)

  x,y,z

  def, main, print

  +, - , /, *

  r,q

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

  1

  2

  0

  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':
		break
	else:
		radius = float(rad)
		area = 3.14 * radius * radius
		print("Area of Circle",area)

  area = Ï€ x radius x 2

  area = 3.14 * rad * rad

  area = 3.14 * radius * radius

  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':
		break
	else:
		number1 = int(num1)
		number2 = int(num2)
		#What code do you need to write here?
			largest = number1
		else:
			largest = number2
		print("Largest of entered two numbers is", largest,"\n")

  if number2 > number1:

  if number1 < number 2:

  if number1 > number2:

  if number2 =< or => number1