You are on page 1of 4

Select and circle the best answer:

1. Consider the following code below and the list = [1, 4, 5, 7]    

for n in range(len(list)):
if (list[n] % 5 == 0 ):
print(list[n])

- The output will be:

a) 5
b) 4
c) All elements
d) None of mentioned.

2. After executing the following code, the output will be: def selectionSort(nlist):
for i in range(len(nlist)):
a) 14 minPosition = i
b) 33 for j in range(i+1, len(nlist)):
if nlist[minPosition] > nlist[j]:
c) All elements of the list
minPosition = j
d) None of mentioned. temp = nlist[i]
nlist[i] = nlist[minPosition]
nlist[minPosition] = temp

nlist = [14, 33]


selectionSort(nlist)
print(nlist)

3. Whats the output of the following code?


def mult_fun (x, y, z):
a) 0.5
if(x > y):
b) 2.0
print(y / x)
c) 3
elif(x < y):
d) None of mentioned.
print(y / x)
else:
print(z)
# ------------------------
mult_fun(2, 2, 3)

4. The output (position) of calling the


def LinearSearch(data, key):  
following LinearSearch([0, 4, 2, 0], 4): 
index = 0  
while( index < len(data) ):    
a)  0 if(data[index] == key ):
b) -1    break 
c) 3 index=index+1 
d) None of mentioned. if( index < len(data) ): 
return index    
else:      
return -1  
5. Number of operations in the following code def LinearSearch(data, key):  
following LinearSearch([0, 4, 2, 0], 0):  index = 0  
while( index < len(data) ):    
a) 0 if(data[index] == key ):
b) -1    break 
c) 3 index=index+1 
d) None of mentioned. if( index < len(data) ): 
return index    
else:      
return -1  

Part 2 (SA Questions):

1. For the list = [1, 15, 4, 15, 3, 16, 17, 13, 11].

- Write the list order for 4 iterations by using bubble sort.

def bubbleSort(alist):
for passnum in range(len(alist)-1, 0 ,-1):
for i in range(passnum):
if alist[i]>alist[i+1]:
print(alist)
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
2. For the list = [1, 15, 4, 15, 3, 16, 17, 13, 11].
- Write the list order for 3 iterations by using Selection sort.

def selectionSort(nlist):
for i in range(len(nlist)):
minPosition = i
for j in range(i+1, len(nlist)):
if nlist[minPosition] > nlist[j]:
minPosition = j
temp = nlist[i]
nlist[i] = nlist[minPosition]
nlist[minPosition] = temp

- Write 3 midpoint values (indices-if any) by using binary search for the list below.
(target = 3). (Show all work).

def binarySearch(alist, item):


first = 0
last = len(alist)-1
found = False
while first <= last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return found

11 3 4 1 13 15 9 16 17
3. For the list = [1, 15, 4, 15, 3, 16, 17, 13, 11].
- Write the list order for 3 iterations by using Insertion sort.

def insertionSort(nlist):
for index in range(1,len(nlist)):
currentvalue = nlist[index]
position = index
while position>0 and nlist[position-1]> currentvalue:
nlist[position]=nlist[position-1]
position = position-1
nlist[position]=currentvalue

You might also like