# 这是一篇来自加拿大的关于编程基础最终测验的编程代写

Multiple choice questions (45 points)

1. (3 points) What will this code output?

i = 0

ct = 0

while i in range(0, 10, 2):

ct += i

i += 1

print(ct)

1. 20
2. 45
3. 1
4. 0
5. (3 points) What will this code output?

postal_code = “H0H-0H0”

postal_code.replace(“H0H”, “0H0H”)

print(postal_code.strip(“0H0”))

1. 0H0H
2. H0H
3. 0H-0H
4. (3 points) What will this code output?

postal_code = “H0H-0H0”

other_code = “MU4-H4H”

print(postal_code.count(‘H’) + other_code.split(‘H’)[1].count(‘H’))

1. 5
2. 4
3. 6
4. 3
1. (3 points) How many different objects of type integer exist in the computer’s memory after the following code has been executed? Assume that no garbage collection has been executed and that no optimizations have been performed.

x, y = 5, 10

x = x * 5

y = x

z = 5

1. 3
2. 6
3. 4
4. 5
5. 2
6. (3 points) How many different objects of type list exist in the computer’s memory after the following code has been executed? Assume that no garbage collection has been executed and that no optimizations have been performed.

import copy

a = [1, 2, 3, 9001]

b = []

for x in a:

b = b + [x]

c = a

a = b[:]

b = copy.deepcopy(b)

1. 12
2. 15
3. 8
4. 11
5. 4
1. (3 points) How many different objects of type list exist in the computer’s memory after the following code has been executed? Assume that no garbage collection has been executed and that no optimizations have been performed.

import copy

a = [[1, 2], [11, 12], [21, 22]]

b = a[1]

c = b[:]

d = copy.deepcopy(a) + b

1. 10
2. 12
3. 9
4. 11
5. 8
6. (3 points) How many different objects of type WorldsBestClass exist in the computer’s memory after the following code has been executed? Assume that no garbage collection has been executed and that no optimizations have been performed.

import copy

class WorldsBestClass:

def __init__(self):

self.name = “Hi!”

wc = WorldsBestClass()

oc = WorldsBestClass()

classes = [wc, oc]

classes += [oc]

c = copy.copy(classes)

1. 1
2. 5
3. 2
4. 4
5. 3
1. (3 points) What will this code output?

d = {[3]: (3, 4), [5]: (5, 6)}

a = [3]

print(d.get(a, 0))

1. TypeError
2. (3, 4)
3. 3
4. 0
5. (3 points) What will this code output?

def swap_inputs(x, y):

return y, x

def apply(func, x, y):

return func(x, y)

print(apply(swap_inputs, swap_inputs, 5))

1. NameError
2. (5, <function swap_inputs at 0x[memory address]>)
3. (<function swap_inputs at 0x[memory address]>, 5)
4. TypeError
5. (3 points) Which of the below statements regarding a factory method is correct?
6. A factory method is a class method that takes the class as input and returns an object of the given class.
1. A factory method can be called on objects of a class but not on the class itself.
2. A factory method is a static method that takes an object of the given class as input and can return any value.
1. A factory method defifinition must be prefaced with the decorator @factorymethod.
1. (3 points) Suppose we want to fifind the target number 149 in the list [8, 16, 33, 94, 102, 909] using binary search. Recall that the algorithm works by comparing the target to a particular number in the list at each iteration. What will be the numbers compared to the target starting from the fifirst iteration?
1. 94, 102, 909
2. 16, 94, 102
3. 33, 94, 102, 909
4. 33, 102, 909
5. (3 points) Let us defifine a new number base called North Pole Base. It is a number system comprised of 11 symbols: the digits 0-9 and the letter H, which represents the number 10. What is the base 10 (decimal) representation of the North Pole Base number “H0H”?
1. 13420
2. 14762
3. 1342
4. 1220
5. (3 points) What will this code output?

class Cat:

def __init__(self, name=‘Cheshire Cat’):

self.name = name

def __str__(self):

return self.name + ” says: Meow!”

print(Cat(‘Crookshanks’))

1. An exception will be raised.
2. <__main__.Cat object at [memory address]>
3. Crookshanks says: Meow!
4. Cheshire Cat says: Meow!

E-mail: vipdue@outlook.com  微信号:vipnxx