M110 Final Spring 23 24 Key
M110 Final Spring 23 24 Key
Python Programming
Answer Key
Final Examination
Spring Semester 2023/2024
Date: ?? /May/2024
Instructions:
Total Marks:100
This exam consists of 3 parts.
ALL questions must be answered in the External Answer booklet.
Be sure you write your name and ID on the External Answer booklet.
Calculators are not allowed.
a. Properties c. Methods
b. Attributes d. Any of the choices.
a. x= “trik” c. x=”tr”
b. x= “triking” d. Error
a. “vldtd” c. “dtdlv”
b. “tdlv” d. Error
9- When you use the 'a' mode to open an output file, then:
a. Variable. c. Object.
b. Method d. Class.
Question 1: [6 marks]
Observe the following Python code very carefully and find the output.
i=0
x= [1, 2, 3, 4, 5, 3]
while i<len(x)-1:
x.remove(x[i])
i+=1
print("test")
print(x)
Question 2: [6 marks]
What is the output of the following code if the entered number of rows is 4?
rows = int(input("Enter number of rows: "))
number = 5
for i in range(1, rows+1):
for j in range(1, i+1):
print(number, end=" ")
number += 2
#print()
print()
print("Completed")
Question 3: [6 marks]
Observe the following Python code very carefully and find the output.
a=16
def challenge(a):
print("a is now:",a)
a=10
for ctr in range(1,a+1,1):
if (a/ctr - a//ctr)==0.0:
print(ctr, end=" ")
challenge(a)
print("\n",a, sep="")
Question 4: [6 marks]
When creating a Label widget, you can use the borderwidth and relief arguments.
What does each one specify? Define briefly.
Question 5: [8 marks]
Given the problem specification below, you are asked to answer the corresponding
questions:
Problem specification: Prompt the user to enter a positive number n, then print the
count of all the odd numbers from 0 to n.
N.B: Assume that the entered number is a positive integer.
a- Write down the pseudocode of the above problem. (4 marks)
b- Write the Python code of the above problem. (4 marks)
b- Python code: 4 marks (The tutor can allocate marks based on the correctness of the
implemented code)
count=i=0
n=int(input("Enter a positive number: ")) # assuming integers are entered
while i<=n:
if i%2==1:
count+=1
i+=1
print(" You have", count, "odd numbers from",0,"to",n) # or just print(count)
Question 6: [8 marks]
Given a Python program that compares two lists, list1 and list2, to find and print
elements in list2 that are doubles of elements in list1. Additionally, the program
includes an operation to square each element in list1 and then append the squared
values to list1.
Here's the initial code with intentional four errors. Identify and correct the errors to
achieve the specified objective:
Answer: (8 marks)
You are requested to write the following hidden commands, after carefully reading
the whole program , so that you properly use the variables:
1- Display the title.
2- Create two frames, one for the top of the window, and one for the bottom.
3- Pack the labels that are in the top frame.
4- Pack the frames.
5- Create the instance my_gui of the class.
N.B: Just write the code after indicating the part that you are answering.
1- (2 marks)
self.root.title("Grades")
2- (2 marks)
self.top_frame = Frame(self.root)
self.bottom_frame = Frame(self.root)
3- (2 marks: the answer is accepted if the student did not include side {default is top})
self.label1.pack(side='top')
self.label2.pack(side='top')
self.label3.pack(side='top')
4- (2 marks)
self.top_frame.pack()
self.bottom_frame.pack()
5- (2 marks)
my_gui = Grades()
b. (5 marks)
Solution #1:
# Open the file in read mode
myfile = open('Std_Name.txt', 'r') # 1.5 marks
# Initialize an empty list to store lines
lines_list = []
# Read the lines # 2.5 marks
line = myfile.readline()
while line:
# Append the line to the list
lines_list.append(line.rstrip('\n'))
# Read the next line
line = myfile.readline()
# Close the file
myfile.close()
# Print the list of lines # 1 mark
print("List of Lines from the File:")
print(lines_list)
Solution #2:
# Open the file in read mode
myfile = open('Std_Name.txt', 'r') #1.5 marks
M110 / Final Page 8 of 10 2023/2024 Spring
# Initialize an empty list to store lines
lines_list = []
# Read the lines # 2.5 marks
for i in range(4):
line = myfile.readline()
# Append the line to the list
lines_list.append(line.rstrip("\n"))
# Close the file
myfile.close()
# Print the list of lines # 1 mark
print("List of Lines from the File:")
print(lines_list)
Solution #3:
f=open('Std_Name.txt',"r") # 1.5 marks
content_list = f.readlines() # 1.5 marks
# remove new line characters # 1 mark
for i in range(len(content_list)):
content_list[i]= content_list[i].strip()
# Print the list of lines # 1 mark
print("List of Lines from the File:")
print(content_list)
End of questions