COMP2101 Final SP24 (With Solution)
COMP2101 Final SP24 (With Solution)
TIME DURATION
2 Hours and 30 Minutes
FORM-A
GRADING TABLE
Question Mark
Tables ______________ /6
Page 1 of 10
Question 1: [10 points] Multiple Choice Questions (1 point each)
def power(x):
x = x**x
return
x=5
power(x)
print(x)
a. 25 b. 5 c. 5**5 d. Error
a. round(1, d. abs(-1, -
2)
b. min(1, 2) c. max(1, 2)
2)
4. Does the following Python code calculate the average speed of a car that travels a distance of 150
kilometers in 3 hours?
distance_kilometers = 150
time_hours = 3
average_speed = distance_kilometers % time_hours
print("Average speed:", average_speed, "km/h")
Page 2 of 10
5. What exception will be raised by the following code?
string ='helo'
try:
print(string[10])
except __________________ as e:
print('Error', e)
a. 4 b. 5 c. 6 d. 10
Page 3 of 10
7. Which code matches this flowchart?
a. x = 12 b. x = 12
x = 12
while x<30: while x<30:
x += 10 x += 10
print(x) print(x)
c. x = 12 d. x = 12 x < 30 print(x)
if x<30: if x<30:
x += 10 x += 10
print(x) print(x)
x += 10
8. Which import statement correctly allows the execution of the following Python statement?
print(math.sqrt(25))
a. 1 b. 2 c. 11 d. [‘hello’, ‘world’]
Page 4 of 10
Question 2: [10 points] True/False
State true/ false to the following questions (1 point each)
Note: Writing T or F considered as invalid and will be given zero to that answer.
Comments in Python are always required and must be included for the
1. False
program to run correctly.
Global variables in Python can be accessed from any function within the
4. True
same file.
5. The index of the last element in a list x is always equal to len(x) False
The following Python statements are valid and do not result in any errors:
9. date = ('May',7,24) True
print(date[0])
Page 5 of 10
Question 3: [14 points] Functions
A. (10 points) Complete the following program that reads number of terms n and computes and prints the
series 𝐺(𝑛) defined as follows:
1 2 3 𝑛
𝐺(𝑛) = 1 + + + +⋯+
2! 3! 4! (𝑛 + 1)
You are requested to write the missing code indicated by the nine numbered bolded comments inside
the program code: (Refer to Figure 1 that shows a sample run of the program)
#(4)Print current series term and stay on same line (See Figure1) (2 Point)
total += i / fact(i+1)_______
print(' = %.2f' %total)
def main():
#(6)Read number of terms n (See Figure1) (1 Point)
G(n)____________________________
main()_____________
Page 6 of 10
B. (4 points) A prime number is a positive integer greater than 1 that has exactly two divisors (1 and itself).
Complete the implementation of the following Python function to determine if a given number is a prime
number or not.
def isPrime(num):
Sample output
if number <= 1:
return False 17 is a prime? True
for i in range(2, (number//0.5) + 1): 25 is a prime? False
if number % i == 0:
return False
return True
x = 17; y = 25
print(x, 'is a prime?', isPrime(x))
print(y, 'is a prime?', isPrime(y))
Alternate sol:
if num <= 1:
return False
for in range(2, num):
if num % i == 0:
return False
return True
Page 7 of 10
Question 4: [10 points] Lists and Strings initiate
A. (6 points) Complete the following Python program that creates a second list called list2 containing only
the 'light' numbers from list1. A number in list1 is considered 'light' if it is smaller than both of its
adjacent numbers. Make sure that list2 does NOT contain any repeated numbers.
2 95 6 7 8
list1= [6, I4, 5, 34, 7, 1, 6, 4, 3, 8]
91
# Create list2 containing only the "light" numbers from list1
list2=[]
#loop through list1
199
for i in range(1, len(list1) - 1):
#check if light number exists in list1
4314181614
if list1[i] < list1[i-1] and list1[i] < list1[i+1]:
#print the light number (see sample output)
if len(list2)>0:
print("list2:", list2)
o
else:
print("no light elements in list 1")
B. (4 points) Complete the following program that reads an input string containing any type of characters.
The program should build a new string without vowels and numerical characters from the input string.
does
inputStr = input("Input a string : ") newsmins
#create an empty string in
newSt = ""
#loop through each character in the input string, adding it to the new
#string if it's neither numerical nor a vowel
for ch in inputStr: [1 point]
AEOUI
if not ch.lower() in "aeoui": [1 point]
newst nest i
newSt+=ch [1 point] Sample run
#Display new string
print("New String is = ",newSt)
New String is =
i
Input a string : COMP2101 @Final Exam?
Page 8 of 10
Question 5: [6 points] Tables
Complete the following python program that display the content of the table as n x n matrix. Then finds and
prints the sum of diagonal elements (see Figure 2 showing diagonal elements).
for i in range(len(row)):
print()
diagonal_sum = 0 #initialize sum value
#find summation of the diagonal elements:
for i in range(len(matrix)):
Matrix:
1 2 3 4
4 5 6 3
7 8 9 6
8 6 2 4
sample run
Page 9 of 10
Question 6: [10 points] Files:
A. (6 points) What is the output of the following python code, given the input values in the ‘file.txt’ as shown
in Figure 3:
B. (4 points) Using a while loop, Complete Python code to generate random integers in the range [0,100]
inclusive. Ensure that your code continues generating random numbers until it has stored five even
numbers in the output file called ‘out.txt’. Assume the random library is already imported as shown below.
Sample solution:
Page 10 of 10