# 13 - Practice 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
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```

bubble search

insertion search

binary search

linear search

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

TRUE

FALSE

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

FALSE

TRUE

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

TRUE

FALSE

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

FALSE

TRUE

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

3

4

2

1

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

10

20

5

15

9. Which of the following statements is correct, in reference to the following code?
```while answer!=”computer”
endwhile
do

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

The while loop will stop if the condition is True

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

None of the above

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

There is no such thing

A recursive algorithm is just an algorithm which uses a loop

None of the above

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

11. 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 15 and 16

Line 11 and 12

Line 9 and 10

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

the length of the list

the letter 'n'

the number of the first element

the number of the second element

13. 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:
else:
print("Found in position",i)

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

```

found' is the input and is the search criteria

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

int' is the search criteria

mylist' is the search criteria in the coded example

14. In a linear search, each element of the list is checked sequentially (one after the other) starting …
`If you are able, watch the video and try and code this yourself in order to fully master it.`

at the beginning of the list

at a random point in the list

at the end of the list

in the middle of the list

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

None of the above

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

sorted portion into the unsorted portion until the list is unsorted

unsorted portion into the unsorted portion

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

right

left

infront

behind

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

The end point of our unsorted portion

None of the above

The start and end of our unsorted portion

The start and end of our sorted portion

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

None of the above

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

compare the 42 to the 15

compare the 42 to the 4

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

None of the above

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

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

20. 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,10,20,18 -- 15,10,18,20 -- 10,15,18,20

All of the above are valid options

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

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

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

FALSE

TRUE

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

6

2

10

5

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

(1/2)n(n-1)

(1/2)(n-1)

None of the above

(1/4)n(n-1)

24. 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]```

None of the above

It is performing the swap FOR every element that is greater

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

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

25. In the following flow chart, if the numbers 1,2,3,4,5 are entered what is the output? 15

12

10

5

26. Indicates any type of internal operations like initialization, calculation etc.

Process

Decision

Terminal

Connector

27. Used for asking questions that can have either TRUE or FALSE (YES or NO) as an answer.

Process

Terminal

Decision

Connector

28. Indicates the starting or ending of the algorithm.

Terminal

Connector

Decision

Process

29. The following pseudocode will accept a password string as a parameter passed into the function, returning _____ if the password is a valid length or ______ if it is not valid.
```function checkpassword(password)
return True
else
return False
endfunction```

True / True

True / False

False / True

False / False

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

None of the above

16000

1200

10600