Preview

14 - Final Test

 1. An algorithm is essentially the exact same thing as binary code. The terms are used interchangeably.

  FALSE

  TRUE

 2. What is the name of this searching algorithm?
array people[5] 
people = [“Imogen”, “Fletcher”, “Kirstie”, “Zoe”, “Gavin”]
found = False
x = 0
searchfor = input(“enter a name to search for : “)
while found = False AND x <5
	    if people[x] = searchfor then	        found = True
	        print “found at position” + x
	        x = x + 1
	    endif
Endwhile

  insertion search

  binary search

  linear search

  bubble search

 3. A binary search compares each value in turn starting at the first element.

  FALSE

  TRUE

 4. A car dealer uses the following algorithm to determine the price to charge for cars. What is the output if p = 1000 i=2 and a = 12?
01 p = input(“purchase price of car”		02 i = input(“number of improvements made”)
		03 a = input(“age of car in years”)
		04 s = p + (i*100)
		05 if a <= 10 then
		06     s = s + s
		07 endif
		08 print  “sale price is “ + s

  16000

  None of the above

  1200

  10600

 5. A car dealer uses the following algorithm to determine the price to charge for cars. What is the output if p = 5000, i = 3, a = 10
2 p = input(“purchase price of car”		02 i = input(“number of improvements made”)
		03 a = input(“age of car in years”)
		04 s = p + (i*100)
		05 if a <= 10 then
		06     s = s + s
		07 endif
		08 print  “sale price is “ + s

  1200

  None of the above

  10600

  16000

 6. A car dealer uses the following algorithm to determine the price to charge for cars. What is the output if p = 8000, i = 0, a = 5
3 p = input(“purchase price of car”		02 i = input(“number of improvements made”)
		03 a = input(“age of car in years”)
		04 s = p + (i*100)
		05 if a <= 10 then
		06     s = s + s
		07 endif
		08 print  “sale price is “ + s

  None of the above

  24000

  10600

  1200

 7. Algorithms are a great way of automating computer decisions. However, automating processes can lead to errors.
For example, the Amazon website uses algorithms to decide the price of products. 
In 2011, the price of a book called 'The Making of a Fly' (about the molecular
 biology of a fly) jumped to £14 million
 as the pricing algorithms used by Amazon to set and update prices started outbidding 
each other. This raised the book price.

  FALSE

  TRUE

 8. Complete the first two lines of the trace table for this algorithm.
Complete the trace table for the following algorithm. 
Test it with a user input of 5.


1.	number <--USERINPUT
2.	FOR i <--- 1 TO 10
3.	    OUTPUT (number * i)


number	i	OUTPUT
======================
5	1	5
	?       ?
        ?       ?

  1,10……..2, 20

  None of the above

  2, 20 …..3, 30

  2,10 ….. 3,15

 9. The programmer wants a variable inside a function to be treated as 'global' and the following code achieves this.
def func1():
 global some_global #use global keyword
 my_global = 20

  TRUE

  FALSE

 10. The following statement will evaluate to ……
(10 >= 1) and (1 < 2)

  FALSE

  TRUE

 11. The following statement will evaluate to …..
>>> (1>2) and (9>=1)

  FALSE

  TRUE

 12. In the following algorithm, the elif has _____ conditional tests.
limbs = 4
if limbs == 4:
 print('no one shall pass')
elif limbs > 0 <4:
 print('tis but a scratch')
else:

  1

  2

  3

  4

 13. What is the output of the following code?
n = 0
for num in range(5):
 n = n + num
print (n)

  5

  10

  15

  20

 14. Which of the following statements is correct, in reference to the following code?
while answer!=”computer”
 answer=input(“What is the
password?”)
endwhile
do
 answer=input(“What is the
password?”)
until answer == “computer”

  The while loop will keep looping while its condition is True.

  The while loop will end if the answer is = "open123"

  The while loop will stop if the condition is True

  None of the above

 15. Merge sort is known as a recursive algorithm. What is a recursive function?

  A recursive algorithm is an algorithm which calls itself with "smaller (or simpler)" input values, and which obtains the result

  A recursive algorithm is just an algorithm which uses a loop

  None of the above

  There is no such thing

 16. Can you spot the place in the code for merge sort where recursion is taking place (a function calling itself)
def mergeSort(alist):

   print("Splitting ",alist)

   if len(alist)>1:
       mid = len(alist)//2
       lefthalf = alist[:mid]
       righthalf = alist[mid:]

       #recursion
       mergeSort(lefthalf)
       mergeSort(righthalf)

       i=0
       j=0
       k=0

       while i < len(lefthalf) and j < len(righthalf):
           if lefthalf[i] < righthalf[j]:
               alist[k]=lefthalf[i]
               i=i+1
           else:
               alist[k]=righthalf[j]
               j=j+1
           k=k+1

       while i < len(lefthalf):
           alist[k]=lefthalf[i]
           i=i+1

  There is no recursion in this program - there's no such thing!

  Line 9 and 10

  Line 15 and 16

  Line 11 and 12

 17. Linear search runs in at worst linear time and makes at most n comparisons, where n is ….

  the number of the first element

  the letter 'n'

  the number of the second element

  the length of the list

 18. Analyse the code below - what is the search criteria here?
#demonstration of linear search

def linearsearch(x,mylist):
  found=False
  
  for i in range(len(mylist)):
    if x==mylist[i]:
      found=True
      break
    else:
      if x not in mylist:
        break

  if found==False:
     print("Not Found")
  else:
    print("Found in position",i)

     

mylist=[1,2,3,4,5]
x=int(input("Enter a number:"))
linearsearch(x,mylist)
  

  x' is input by the user and is the search criteria

  mylist' is the search criteria in the coded example

  found' is the input and is the search criteria

  int' is the search criteria

 19. In a linear search, each element of the list is checked sequentially (one after the other) starting …

  at the end of the list

  in the middle of the list

  at the beginning of the list

  at a random point in the list

 20. At each step of the algorithm an element is moved from the ..

  sorted portion into the unsorted portion until the list is unsorted

  unsorted portion into the unsorted portion

  unsorted portion into the sorted portion until the whole list is sorted

  None of the above

 21. Typically, the sorted numbers go to the _____of the unsorted numbers.

  infront

  right

  left

  behind

 22. In the following list what is '23'?
23,42,4,16,8,15

  The start and end of our unsorted portion

  The start and end of our sorted portion

  The end point of our unsorted portion

  None of the above

 23. 42 is the first element in the unsorted portion and we proceed in the algorithm to …..
23,42,4,16,8,15

  compare the 42 to the 15

  compare the 42 to the 23 (23 being the only element in our sorted element)

  compare the 42 to the 4

  None of the above

 24. If 42 is larger than 23, we can…..
23,42,4,16,8,15

  include 42 before the 23 (e.g. 42,23 / etc)

  None of the above

  remove 42 and take it to the end of the list as it is largest (e.g. 23,4,16,8,15,42)

  append 42 to the end of the 'sorted' list. (e.g. 23, 42 / 4,16,8,15

 25. What are the correct intermediate steps of the following data set when it is being sorted with the bubble sort? 15,20,10,18

  15,18,10,20 -- 10,18,15,20 -- 10,15,18,20 -- 10,15,18,20

  15,10,20,18 -- 15,10,18,20 -- 10,15,18,20

  10, 20,15,18 -- 10,15,20,18 -- 10,15,18,20

  All of the above are valid options

 26. It is possible to code a bubble sort with two 'for loops' nested in each other

  FALSE

  TRUE

 27. What is the maximum number of comparisons if there are 5 elements in array x?

  6

  5

  2

  10

 28. What is the max. number of comparisons that can take place when a bubble sort is implemented? Assume there are n elements in the array?

  None of the above

  (1/4)n(n-1)

  (1/2)n(n-1)

  (1/2)(n-1)

 29. In the following code, what is the line for i in range(n) doing?
def bubbleSort(arr):
    n = len(arr)
 
   
    for i in range(n):
 
        # Last i elements are already in place
        for j in range(0, n-i-1):
 
            # traverse the array from 0 to n-i-1
            # Swap if the element found is greater
            # than the next element
            if arr[j] > arr[j+1] :
                arr[j], arr[j+1] = arr[j+1], arr[j]

  It is performing the swap FOR every element that is smaller than the first element

  None of the above

  It is looping/traversing through all the elements in the array (or list)

  It is performing the swap FOR every element that is greater

 30. Can you analyse the flow chart and describe what it does?
flowchart-to-print-the-sum-of-50-natural-numbers.png

  Inputs the numbers 1 and 0 and then keeps repeating this until we have 50 1s and 0s

  Inputs 50 numbers and calculates the sum of these numbers

  Inputs the variables x and 0, until x =50 and 0 is no longer necessary

  None of the above

 31. What flow chart symbol is executed if the email ID and password are NOT correct?
facebook_login_flowchart.png

  End Program (Crash)

  Go back to 'Start'

  Display Account

  Log in error

 32. Analyse the following flow chart and predict what it does.
printhelloworldflowchart.png

  None of the above

  The flow chart is set up wrong as the count starts at 0 - so nothing will be printed

  Print "Hello World" 10 times

  Prints "Hello World" just one time

 33. In the following algorithm, how many INPUT/OUTPUT boxes (parallelograms) would be in use?
Enter www.facebook.com in your browser.  
facebook Home page loads  
Enter your Email ID and Password    
Is Email ID and Password Valid 
if NO then
Log in error  
go to step 3
else
Display facebook Account 
Stop

  1

  2

  4

  3

 34. The following algorithm is supposed to print out the times table of the number entered but there is an error. What is it?
input b
x = 1
while x <=10 
print b * x
endwhile

  The value of x should start at 10 in order for the stopping condition to work

  There is no incrementation (x = x+1) needs to go after print b*x

  while x <=10 should be changed to while x <=0, as otherwise the loop will not run

  There is no error - this code will work fine

 35. For the following algorithm, if the user enters '3', what will happen?
input age
for x = 1 to age
print “happy birthday”
next x 

  It will cause an error because x = 1, this needs to be changed to x = age

  It will print the message "happy birthday" three times

  None of the above

  It will print the message "happy birthday" infinite number of times until it crashes