这是一篇来自加拿大的关于COMP 202最终测验的计算机代考
Multiple choice questions, 3 points each (36 points)
- (3 points) What will this code output?
d = {‘first’: {‘name’: ‘Kate’, ‘id’: 260800000}}
x = d[‘first’]
for i in str(x[‘id’]):
d[i] = x
print(len(d))
- 10
- An exception will occur.
- 5
- 1
- (3 points) Which of the below statements regarding methods are correct? Select all that apply. You must
select all the correct answer(s) to get the points for this question.
A.An instance method must take at least one argument, called ‘cls’ by convention (though any name is OK).
B.You must put a decorator on the line above a method defifinition so that the Python interpreter what kind of method it is.
C.A class method can be called using dot notation on an instance of the class.
D.For a class that represents a bookstore, a method that prints out the list of books contained in the store
would make most sense as an instance method.
- (3 points) Assume that a Painting class has been defifined whose constructor takes two arguments. The class
Painting also contains an instance method is_fake which returns a boolean. Which of the following
methods, if added to the class, would let the code below execute without error? Select all that apply.
You must select all correct answers to get the points for this question.
orders = Painting(“Orders”, “Philip Guston”)
blue_sky = Painting(“Blue Sky”, “Philip Guston”)
print(orders > blue_sky)
- @staticmethod
def __gt__(painting1, painting2):
….return not painting1.is_fake() and painting2.is_fake()
- def __eq__(self, other):
….return other is not None and other.is_fake()
- def __gt__(self, painting2):
….return False
- def __gt__(self, other):
….return other.is_fake() and not self.is_fake()
- (3 points) What will this code output?
t = ((13, 37), (900, 1))
for i, k in enumerate(t):
print(dict([(i, k)]))
- {13: 37}
{900: 1}
- TypeError: tuples of tuples cannot be enumerated
- {0: (13, 37)}
{1: (900, 1)}
- TypeError: tuple cannot be unpacked
- {13: 37, 900: 1}
- (3 points) Consider the following error traceback obtained during execution of a piece of code.
Traceback (most recent call last):
File “alt_mt.py”, line 46, in <module>
main()
File “alt_mt.py”, line 42, in main
remove_snow_from_string(input(“Enter snowy string: “))
File “alt_mt.py”, line 18, in remove_snow_from_string
snow_helper(snow_s)
File “alt_mt.py”, line 5, in snow_helper
raise ValueError(“Rain in string!”)
ValueError: Rain in string!
In which function did the exception that caused this error fifirst occur?
- input
- There is not enough information in the traceback to know for sure.
- remove_snow_from_string
- main
- snow_helper
- (3 points) What will this code output?
import copy
class Author:
def __init__(self, name):
self.name = name
me = Author(“Disgruntled Bovine”)
you = Author(“Disgruntled Bovine”)
if me == you:
print(“Imposter!”)
other = copy.copy(me)
if other == me or other == you:
print(“I am Spartacus.”)
- Imposter!
I am Spartacus.
- An error traceback will be printed.
- I am Spartacus.
- Nothing will be printed.
- Imposter!
- (3 points) The following function f takes a monotone binary string as input. A monotone binary string
consists of only 0s, and 1s and with all the 1s coming after all the 0s : ‘000 . . . 0011 . . . 11′. Examples
of monotone binary strings are ‘00111’, ‘000001, 0111′ or ’01’. Assume the monotone binary
string has at least one 0 and at least one 1.
What does f return?
def f(data):
low = 0
high = len(data) – 1
while low <= high:
middle = (low + high) // 2
if data[middle] < data[middle + 1]:
return middle
elif data[low] == data[middle]:
low = middle + 1
else:
high = middle – 1
- The number of 1s in the string
- The index of the fifirst 1 in the string
- The number of 0s in the string
- The index of the last 0 in the string
- (3 points) What will this code output?
x = [1, 2, 3]
for i in range(len(x), 0, -1):
try:
print(1 / (x[i] – 2))
except ZeroDivisionError:
print(“Bad math”)
except:
print(“Something went wrong”)
finally:
print(“PRINT ME!!”)
- Something went wrong
PRINT ME!!
1.0
PRINT ME!!
Bad math
PRINT ME!!
- Something went wrong
PRINT ME!!
- Something went wrong
Bad math
1.0
-1.0
PRINT ME!!
- Something went wrong
1.0
Bad math
PRINT ME!!
- 1.0
PRINT ME!!
Bad math
PRINT ME!!
-1.0
PRINT ME!!