1a. Practice Quiz 2
1a. Practice Quiz 2
CLO2
linearList = [3,5,9,7,6,12,15,1]
target = 35
for x in linearList:
if x == target :
print("The number is " + str(x))
1. counter = 1
2. for x in range(1,10,1):
3 if x >= 5 :
4 print(counter)
5 else:
6 print(x)
sumVal=0
pg. 1
CIS 2203 Practice Quiz 2 202010
for x in range(1, 5):
sumVal = sumVal + x
print(sumVal)
a. 6
b. 10
c. 4
d. 3
8. What is the output when the following code is run?
counter = 0
for x in range(0, 20, 4) :
if (x%6 ==0):
counter +=1
print (counter)
a. 4
b. 2
c. 3
d. 0
9. In the ________ search, each element is compared with x till found or not found.
a. Binary
b. Linear / Sequential
c. Merge
d. None of the above
10. Which of the following sorting algorithm repeatedly selects the next smallest item?
a. Selection sort
b. Bubble sort
c. Merge sort
d. Insertion sort
11. On the third iteration of the selection sort for list 3, 5, 4, 1, 2 we get ___________
a. 1, 2, 4, 3, 5
b. 1, 2, 3, 4, 5
c. 1, 5, 4, 3, 2
d. 3, 5, 4, 1, 2
12. To sort a list with n elements, the insertion sort begins with the __________ element.
a. First
b. Second
c. Third
d. Fourth
13. Which of the following sorting algorithm works by repeatedly swapping the adjacent elements if they are in
wrong order.
a. Merge sort
b. Insertion sort
c. Selection sort
d. Bubble sort
14. Which of the following is a worst-case situation when using the linear search algorithm?
a. Item is somewhere in the middle of the array
b. Item is not in the array at all
pg. 2
CIS 2203 Practice Quiz 2 202010
c. Item is the last element in the array
d. Item is the last element in the array or is not there at all
15. The time complexity of the Linear Search algorithm is:
a. O(n)
b. O(log n)
c. O(n2)
d. O(n log n)
16. The time complexity of the Binary Search algorithm is:
a. O(n)
b. log2 (n)
c. 0
d. 1
17. The time complexity of the Bubble sort algorithm is:
a. O(n)
b. O(log n)
c. O(n2)
d. O(n log n)
18. What is the time complexity of the following code?
k=j*k+i
a. ϴ(log n)
b. ϴ(n)
c. ϴ(n2)
d. ϴ(n3)
19. What is the value of a when n=5 in the code snippet?
20. What is the value of the variable ‘ thesum’ after executing the following code
segment?
pg. 3
CIS 2203 Practice Quiz 2 202010
21. The following code snippet finds the summation of numbers divisible by 3 in the range (1,10). What will be the
output of this code segment execution?
22. The following code segment finds the summation of even numbers in the myNumbers Array. Select the best
answer that completes the “If” statement in the algorithm.
23. Consider the following code below and the list = [1, 4, 5, 7]. What will the output be?
24. Apply the following linear search code, then choose the correct output.
T/F The Insertion Sort & Selection Sort algorithms are similar.
T/F Algorithm time complexity means that the amount of extra memory or storage required depends on the size of
the input.
T/F It is not necessary for an algorithm to guarantee terminating and producing a result.
T/F The time complexity of an algorithm is usually more important than its space complexity.
pg. 4
CIS 2203 Practice Quiz 2 202010
T/ F Time complexity of binary search algorithm is constant.
5. The code above is using the Binary search algorithm to search for the integer (4), in the range (3, 12). Write
down the values of i, middle and j. (5 marks)
Iteration No midpoint i j
pg. 5
CIS 2203 Practice Quiz 2 202010
If the item=25 and the alist= [11, 12, 9, 8, 13, 12, 22, 23, 25] answer the following questions:
a. What will be the value of the variables (first, last, midpoint, index) after each iteration (pass)?
b. What will be the remaining numbers in the list after each iteration (pass)?
b. Write the list order for 3 iterations by using Insertion sort. (5 marks)
pg. 6
CIS 2203 Practice Quiz 2 202010
c. Write the list order for 3 iterations by using Bubble sort. (5 marks)
pg. 7