AT2 - 2203 - Prac - Solution
AT2 - 2203 - Prac - Solution
Part 1(MCQs):
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] % 2 == 0 ):
print(n)
a. The output will be:
a) 1
b) 4
c) All elements
d) None of mentioned.
3. The following code finds the summation of numbers divisible by 3 in the list. Select
the best answer that completes the “If” statement in the code.
listSum = 0
lst = [2, 1, 3, 11, 1, 4, 1]
for i in range(len(lst)):
if (------------):
listSum = listSum + lst[i]
print (listSum)
a) lst [i] % 1 == 0
b) lst [i] % 3 == 1
c) lst [i] % 2 == 3
d) lst [i] % 3 == 0
4. After executing the following code, the output will be: def selectionSort(nlist):
for i in range(len(nlist)):
Page 1 of 6 minPosition = i
for j in range(i+1, len(nlist)):
if nlist[minPosition] > nlist[j]:
CIS 2203- Practice
a) 12
b) 21
c) 9
d) All elements of the list
mult_fun(1, 1, 2)
1. For the list = [11, 16, 15, 16, 4, 17, 13, 14, 12].
def bubbleSort(alist):
11 16 15 16 4 17 13 14 12
for passnum in range(len(alist)-1, 0 ,-1):
11 15 16 16 4 17 13 14 12
for i in range(passnum):
11 15 16 4 16 17 13 14 12
if alist[i]>alist[i+1]:
11 15 16 4 16 13 17 14 12
print(alist)
11 15 16 4 16 13 14 17 12 temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
c. Write 3 midpoint values (indices-if any) by using binary def binarySearch(alist, item):
search for the same list above. (target = 0). (Show all first = 0
work). last = len(alist)-1
found = False
while first <= last and not found:
list = [11, 16, 15, 16, 4, 17, 13, 14, 12] midpoint = (first + last)//2
if alist[midpoint] == item:
0 1 2 3 4 5 6 7 8 found = True
else:
11 16 15 16 4 17 13 14 12 if item < alist[midpoint]:
last = midpoint-1
4 11 12 13 14 15 16 16 17 else:
first = midpoint+1
return found
M1: (0+8)/2=4=14
M2: (0+3)/2=1=11
M3; target not found
x f(x) = x2 +x g(x) = x2 + 2 2g(x) = 2x2 + 2
c=1 c=2
1 2 3 6
2 6 6 12
3 12 11 22
4 20 18 36
2. Let f(x) and g (x) be two functions
defined as f(x) = x2 + x and g(x) 5 30 27 54
= x2 + 2.
Show using a table of values that, f(x) is ϴ (g(x)). And what are the values of
“C and K”? (show all work)
TO BE THETA ϴ
SHOULD BOTH OMEGA OR O
2g(x) = 2( x2 + 2
Page 4 of 6
CIS 2203- Practice
a. Write the number of operations for each line in the right column in the above
table (Number of operations) for each code line.
Page 5 of 6
CIS 2203- Practice
Page 6 of 6