0% found this document useful (0 votes)
45 views6 pages

AT2 - 2203 - Prac - Solution

1. The document contains a practice exam with multiple choice and short answer questions on topics related to algorithms and data structures. 2. The multiple choice section contains 8 questions testing concepts like linear search, selection sort, bubble sort, time complexity analysis, and identifying outputs of code snippets. 3. The short answer section contains 3 questions: a) writing the output of bubble sort and selection sort iterations on a sample list, b) using a table to show that a function f(x) = x^2 + x is Θ(g(x)) where g(x) = x^2 + 2, and finding the values of constants C and K.

Uploaded by

maryam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views6 pages

AT2 - 2203 - Prac - Solution

1. The document contains a practice exam with multiple choice and short answer questions on topics related to algorithms and data structures. 2. The multiple choice section contains 8 questions testing concepts like linear search, selection sort, bubble sort, time complexity analysis, and identifying outputs of code snippets. 3. The short answer section contains 3 questions: a) writing the output of bubble sort and selection sort iterations on a sample list, b) using a table to show that a function f(x) = x^2 + x is Θ(g(x)) where g(x) = x^2 + 2, and finding the values of constants C and K.

Uploaded by

maryam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CIS 2203- Practice

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.

def mult_fun (x, y, z):


if(x > y):
2. Whats the output of the following code? print(y / x)
elif(x < y):
a) 1 print(y / x)
b) 2 else:
c) 3 print(z)
d) None of mentioned.
mult_fun(2, 2, 3)

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

5. Whats the output of the following code?


def mult_fun (x, y, z):
e) 0 if(x > y):
f) 1.0 print(y / x)
g) 2 elif(x < y):
h) None of mentioned. print(y / x)
else:
print(z)

mult_fun(1, 1, 2)

6. The output (position) of calling the following


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

7. Number of operations in the following def LinearSearch(data, key):  


code in the while loop block is:  index = 0  
while( index < len(data) ):    
if(data[index] == key ):
Page 2 of 6    break 
index=index+1 
if( index < len(data) ): 
CIS 2203- Practice
a) n+2
b) 2n + 1
c) 3n + 1
d) 3n + 2

8. The time complexity for the function (f(x) = 32) is:


a) Θ(0)
b) Θ(n2)
c) Θ(n3)
d) Linear

Part 2 (SA Questions):

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

a. Write the list order of 4 iterations by using bubble sort.

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

b. Write the list order for 4 iterations by using


Selection sort.
def selectionSort(nlist):
11 16 15 16 4 17 13 14 12 for i in range(len(nlist)):
4 16 15 16 11 17 13 14 12 minPosition = i
2 11 15 16 16 17 13 14 12 for j in range(i+1, len(nlist)):
2 11 12 16 16 17 13 14 15 if nlist[minPosition] > nlist[j]:
2 minPosition = j
Page 311
of 6 12 13 16 17 16 14 15
temp = nlist[i]
nlist[i] = nlist[minPosition]
nlist[minPosition] = temp
CIS 2203- Practice

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

f(x) > g(x) f(x) < 2g(x) f(x) Big O 2g(x)


X > 1   X > 3 f(x) big Ω g(x)
f(x) big Ω g(x) f(x) Big O 2g(x) f(x) Big θ g(x)

Page 4 of 6
CIS 2203- Practice

f(x) >= g(x)


when x>=2 and c=1
F(x) Big-omega g(x)

f(x) < 2g(x)


when x>=1 and c=2
f(x) Big-O 2g(x)
k values  1 & 2
since f(x) is big omega g(x) and f(x) is big O 2g(x), then f(x) big theta g(x)).
f(x) ϴ g(x).

3. The following Python code prints the counter values.

code Number of operations


s=1 1
c=1 1
while (s <= 10): n+1
    c = c + 1 n
    s+=1 n
print (s)    0

a. Write the number of operations for each line in the right column in the above
table (Number of operations) for each code line.

b. Express the number of operations in terms of a function f(n), where n is the


input size
F(n)= 3n+3

c. Write down the Time complexity of the algorithm


O(n) --- Linear
The time complexity of the algorithm is θ(n).
Note: if number of iteration is 10, then total number in this code = 3*10+3 = 33

Page 5 of 6
CIS 2203- Practice

4. Write the algorithm of Selection Sort (mention all steps).

-Get a list to sort


-Iterate elements of the list starting from index x
1. Set min to the element in the index x
2. Iterate unsorted list starting from x
1. Compare each element of unsorted list with the min value
2. Maintain the min value in the unsorted list
3. Maintain the min value location L
-Swap min found in location L with element in index x
-Repeat until list is sorted

1. Get a list L of n elements with values or records L0, L1, …, Ln-1.


2. Iterate elements of the list starting from index x.
3. Set min to the element in the index x.
4. Iterate unsorted list starting from next x.
5. Compare each element of unsorted list with the min value.
6. Swap if any element is less than value.
7. Repeat until list is sorted.

Page 6 of 6

You might also like