# 编程代写｜Faculty of Science COMP-202 – Foundations of Programming (Winter 2020) Midterm Examination

### 编程代写｜Faculty of Science COMP-202 – Foundations of Programming (Winter 2020) Midterm Examination

Instructions:

• This is a closed book examination; only a legal-sized (8.5” by 14”) reference sheet is permitted. This reference sheet can be single or double-sided; it can be handwritten or typed. Non-electronic translation dictionaries are permitted, but instructors and invigilators reserve the right to inspect them at any time during the examination.
• Besides the above, only writing implements (pens, pencils, erasers, pencil sharpeners, etc.) are allowed. The possession of any other tools or devices is prohibited.
• Answer all questions on the exam booklet provided.
• This examination has 10 pages including this cover page, and is printed on both sides of the paper. On page 9,you will a copy of the Function Design Recipe. You may detach the Appendix (page 9 onwards) from the

examination if you wish.

Scoring

The exam is out of 100.

1. Part 1 is True or False: 5 questions, 1 point each (total: 5)
2. Part 2 is Short Answer/Multiple choice: 15 questions, 3 points each (total: 45)
3. Part 3 is Long Answer (total: 50)

1 True or False (5 points)

Each question is worth 1 point (5 questions). Please answer all questions on the exam booklet provided. Note that,

you will not receive any points for answers noted on the exam questions.

1-1. What is the value of b?

b = float(1 // 2) < 2 / 5 and 64 > 8 * 1 ** 2

1-2. The following instruction converts the number 19 from base 16 to base 10 and stores the result in the variable x

x = int(’19’, 16)

1-3. The following snippets of code produce the same output.

Snippet #1:

x = 6

if x < 10:

print(’a’)

elif x % 3 == 0:

print(’b’)

Snippet #2:

x = 6

if x < 10:

print(’a’)

if x % 3 == 0:

print(’b’)

1-4. It is possible to create a local variable with the same name as a global variable from the same module.

1-5. The following code displays all the characters of the string s one by one, starting from the last character in the string and ending with the fifirst one.

s = ’kittens’

for i in range(len(s), 1, -1):

print(s[i – 1])

print(s[i – 1])

2 Short Answer/Multiple choice (45 points)

Each question is worth 3 points (15 questions). Please answer all questions from this section on the exam booklet provided. Note that, you will not receive any points for answers noted on the exam questions.

For all of the questions in this section, if you believe that an error is raised please answer the question with the name of the error that Python will raise.

2-1. What prints when the following snippet of code is executed?

x = 5

y = 3

def mystery(x, y):

temp = x

x = y

y = x

x += 1

return y

mystery(y, x)

print(x, y)

2-2. What prints when the following snippet of code is executed?

if x > 0:

print(“Masterfully “)

elif x < 0:

print(“Wonderfully “)

else:

print(“Splendidly “)

2-3. What prints when the following snippet of code is executed?

def f(x):

x = 5

z = 7

if x < z:

print(’A’, end = ’ ’)

z = 4

if x < z:

z = 6

print(’B’, end = ’ ’)

else:

print(z > y)

y = 6

f(y)

2-4. What prints when the following snippet of code is executed?

def one(x):

x = three(x) + two(two(three(x)))

return x

def two(y):

if y % 2 == 0:

return y

return y // 2

def three(z):

return z – 3

print(one(5))

2-5. What prints when the following snippet of code is executed?

def f(x):

s = 0

for d in str(x):

s += int(d)

return str(s)

def g(y, z):

x = float(y)

return round(x) % z

a = 537

b = “12.345”

print(f(a) + str(g(b, 3)))

2-6. Which of the following is a valid way (i.e. it will not raise any kind of exception) to import and then use the constant pi? Write the letters corresponding to all correct answers in your answer sheet.

(A) import math

x = math.pi() ** 2

(B) from math import pi

y = 3 – pi

(C) from math import *

x = math.pi/9

(D) import math

z = “Pi: ” + math.pi

(E) from math import *

x = int(pi + 9)

2-7. Here is the module hello.py. What will this module output?

print(“Hi”)

def f():

print(__name__)

if __name__ == “__main__”:

f()

print(“Hey”)

2-8. Here is the module goodbye.py, which imports the module from Q2-7. What will goodbye.py output?

import hello as h

def g():

print(“Goodbye”)

if __name__ == “__main__”:

g()

a = h.f()

print(“Farewell”, a)

2-9. How many times will this code print out the character ‘?’?

i = 4

j = 20

while (i < j):

print(’?’)

for k in range(1, 3):

print(’?’)

j -= 2

2-10. The following function takes an integer as input and returns the binary representation of the input as a string.

Write one statement that fifills in the blank and completes the code.

def decimal_to_binary(q):

binary = ’’

while (q != 0):

r = q % 2

q = q // 2

*** BLANK ***

return binary

2-11. What prints when the following snippet of code is executed?

x = 5

y = [7]

print(x > 0 and f(y, x))

def f(a, b):

return a[1] > b

2-12. What prints when the following snippet of code is executed?

x = [1, 8, 4, 2, 10]

x[2] = x[1]

x[4] = x[x[3]]

y = x

y[0] = 5

print(x)

print(y)

2-13. What prints when the following snippet of code is executed?

def my_func(a):

a[-1] = a[0]

b = []

for i in range(len(a)):

b.append(a[i]*2)

a = b

x = [1, 4, 9, 16, 25]

my_func(x)

print(x)