Midterm Solutions
Midterm Solutions
Midterm Solutions
LONDON, CANADA
MIDTERM EXAMINATION
2 Hours: 3:00pm-5:00pm
Name:________________________________________________________
Student Number:_______________________________________________
Lecture Section (circle one): Tu-Th 9:30 (AH-1R40) Tu-Th 12:30 (MC-110)
Tu-Th 3:30pm (AH-1R40)
Section 1: Scantron
1. True/False 15
2. Multiple Choice 40
Section 2: On Exam
3. Logic Errors 12
4. A Little Code 33
Total 100
2) Python can have variables that hold integer values. True False
5) The first position in a string in Python has the index 0. True False
11) To divide two integers to get an integer result, you can use % . True False
12) The keyword elif can be used in if-statements in Python. True False
13) The operator + can be used to concatenate two strings together. True False
16) Which of the following statements is/are TRUE about the CPU?
a. CPU stands for Central Processing Unit
b. The CPU is what performs computation
c. The CPU processes machine language
d. At least two of the above statements are true
e. None of the above are true
18) Which statement(s) allows us to initialize the list numbers with 10 elements all set to zero?
a. numbers = [0]*10
b. numbers[10] = 0
c. numbers = [10] * 0
d. numbers[10] = [0] * 10
19) Which of the following subtracts a variable x from a variable y, divides their difference by 3 and
adds 11 to the result:
a. (x - y) / 3 + 11
b. x - y / 3 + 11
c. y - x /3 + 11
d. 2*(y - x) / 3 + 11
e. None of the above are true
20) What will be the values of the variables num1 and num2 after the execution of the following
assignments?
num1 = 23
num2 = 18
num1 = num1 + num2 // 2
num2 = num1
a. num1 is 32, num2 is 32
b. num1 is 30, num2 is 30
c. num1 is 30, num2 is 21
d. num1 is 30, num2 is 18
e. None of the above.
a. values[4]
b. values = [0]*4
c. value[4] = [1, 2, 3, 4]
d. values == list.[4]
22) The following code snippet contains an error. What is the error?
cost = input("Enter the cost: ")
if cost > 100:
cost = cost - 10
print("Discounted cost:", cost)
23) Which of the following for loops will run the loop body 5 times?
a. for i in range(13, 9, -1) :
b. for i in range(14, 9, -1) :
c. for i in range(15, 9, -1) :
d. for i in range(14, 10, -1) :
24) Which one of the following instructions checks to see if there is a comma anywhere in
the string variable name?
a. if name.contains(",") :
b. if "," not in name :
c. if name.startswith(",") :
d. if "," in name :
25) Which of the following statements is true about functions and strings:
a. A function can be called with a string as an argument.
b. A function can return a string.
c. Only a. is true.
d. Only b. is true.
e. Both a. and b. are true.
fun(a,b)
print(b,a)
a. 9 8
b. 8 9
c. 8 8
d. 8 7
e. None of the above
27) What is the value of names after the following code segment has run?
names = []
names.append("Amy")
names.append("Bob")
names.pop()
names.append("Peg")
names[0] = "Cy"
names.insert(0, "Ravi")
names.insert(4, "Savannah")
a. zx
yw
b. zy
wv
c. zv
yw
d. zx
yv
a. 24-17-10-
b. 24-17-10-3
c. 24 17 10
d. 24-17-10-3
a. 10.0
b. 30.5
c. 1000.0
d. 300.5
e. 700.5
Use the following code for the next three questions (i.e. questions 32 – 34 )
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
if not (num1 > num2 and num1 >= num3) :
print(“First num is”, num1)
elif not(num2 > num1 and num2 > num3) :
if num2 % 10 == 0:
print(“The value is”, num2)
elif num1 % 10 == 0:
print(“The value is”, num1)
else:
print(“The value is”, num3)
elif not (num3 > num1 or num3 > num2) :
print(num3)
32) Assuming a user enters 5, 10, and 15 as the input, what is the output of the above code snippet?
a. The value is 10
b. First num is 5
c. 15
d. The value is 15
a. First num is 15
b. The value is 10
c. 5
d. The value is 15
34) Assuming a user enters 7, 7, and 6 as the input, what is the output of the following code snippet?
a. First num is 7
b. The value is 7
c. 6
d. The value is 6
def myCalculator(n):
i = 5
x = 3
y = 2
while i > 0:
y = y + n
x = x + y % 3
i = i-1
return x
print(myCalculator(3))
a) 10
b) 11
c) 12
d) 13
The function is syntactically correct but has four (4) incorrect lines of code which contain logic
errors that prevent it from computing correctly. Identify the lines and correct them Note: a line
may contain more than one logic error. Examples of a main program that uses the function, when
it is correctly implemented, and sample output are also provided. (12 Marks)
#
# main program
str = "abab"
s = "ab"
lst = substring(str,s)
print(lst)
str = "abbbaab"
s = "bb"
lst = substring(str,s)
print(lst)
Output
[0, 2]
[1, 2]
4.1) Create a function countNines that counts the number of nines in an integer. For example, if the
integer is 90190, then the function would return 2. Provide your code for the function below (12
Marks).
def countNines(n):
def countNines(n):
strInt = str(n) # 4 converting integer
count = 0
for ch in strInt: # 2 for loop
if ch == "9": # 4 for if and count
count = count + 1
return count # 2 for return
4.2) Create a function removePunc that takes a string containing letters, digits and punctuation and
removes all the punctuation and returns a new string with no punctuation. The punctuation to
consider is defined in the constant PUNC below. For example, if the word is "London’s.", then
the function would return "Londons". Provide your code for the function below (10 Marks).
PUNC = ".,:;'\"?!"
def removePunc(w):
def removePunc(w):
neww = "" # 1 for initialization
for ch in w: # 2 for loop
if ch not in PUNC: # 3 for if
neww = neww + ch # 2 for correct assignment
# and concatenation
return neww # 2 for correct return
def wordList(line):
def wordList(line):
words = [] # 1 for initialization
lst = line.split() # 2 for splitting like
for w in lst: # 1 for loop through list
w = removePunc(w) # 3 correct use
of"removePunc"
words.append(w) # 3 for correct append
return words # 1 for return