CS1010E-202223-S1 Final With Answerspdf
CS1010E-202223-S1 Final With Answerspdf
alan = Graduate('Alan','BSc')
alan.title = 'Dr'
print(alan.fullname)
35 class Animal: *a) *cute*
def __init__(self): None
self.sound = None b) *cute*
def speak(self): Woof
print(self.sound) c) Woof
class Dog(Animal): d) *cute*
def __init__(self): e) None
self.sound = "Woof"
class Chihuahua(Animal):
def speak(self):
print("*cute*")
super().speak()
dolly = Chihuahua()
dolly.speak()
36 n = 2 *a) 15
x = 0 b) None
try: c) 10
while True: d) 5
x += 10//n e) The code crashes
n-=1
except:
print(x)
37 n = 0 *a) 3
try: b) 1
n += 1 c) 2
except what: d) 4
n += 1 e) The code crashes
except:
n += 1
else:
n += 1
finally:
n += 1
print(n)
38 x = 0 *a) 2
try: b) 0
assert 0*(1/0) c) 1
except AssertionError: d) 3
x = 1 e) The code crashes
except ZeroDivisionError:
x = 2
except:
x = 3
print(x)
39 The two file opening modes, 'w+' and *a) True
'r+', have some different b) False
functionalities.
40 print(round(2.5)+round(3.5)) *a) 6
b) 5
c) 7
d) 7.0
e) 6.0
Section 2, 4 x 4 marks each
New section
41 If the input L is a list of numbers with *a) Median of L
odd length, what is the output of the b) Mean of L
following code? c) Mode of L
def foo(L): d) A sorted list of L
L2 = list(L.copy()) e) A zig-zag list of L
output = []
while len(L2)>2:
output.append(max(L2))
output.append(min(L2))
L2.remove(max(L2))
L2.remove(min(L2))
output.append(L2[0])
return output[-1]
42 If we want to write a Python program to *a) Dictionary
store a large collection of b) List
names(strings) and query if a certain c) Tuple
name(string) is in the collection, what d) File
will be the best data structure to store e) All of the answer
all the names in terms of speed? here will be the same
43 Give a list of unique integers L, we want to find how *a) Each pair may be
many pairs of number in L with a sum to a value x. E.g. counted twice
>>> sumTo([1,2,3,4,5,6,7,8,9],13) b) The code is actually
3 correct all the time
Explain why the following code is buggy? c) The code will crash
def sumTo(L,x): if the length of the
count = 0 list is 0
for i in range(len(L)): d) The code will be
for j in range(len(L)): correct if x//2 is not
if L[i]+L[j]==x: in L
count += 1 e) The code will be
return count correct if x//2 is in L
44 A palindrome is a word that reads the same backwards as a) The code can check
forwards, e.g. madam, ere. The following code is to check if palindrome correctly
a string is a palindrome: without crash
def check_palindrome(s): b) The code actually
if not s: tell the wrong
return True (reversed) answer always
if s[0]==s[-1]:
c) The code will crash
return check_palindrome(s[1:len(s)-1])
return False if len(s) is odd
Provided that the input s is a string, what is the best d) The code will crash
description of the code? if the input s has its
length len(s) == 1800
e) The code cannot work
for reasons that are not
mentioned here
Section 3 Fill-in-the-blanks: 4 x 6 marks
Please note that your code will got zero mark if there are syntax errors. Also, There is no partial marks for
each question.
def find_star(m):
47 You are given a 2D map like the following: n_row = len(m)
map1 = ['.#+#...#...', n_col = len(m[0])
for i in range(1,n_row-1):
'..#...#+#..', for j in range(1,n_col-
'.......#...', 1):
if (m[i-1][j]=='#'
'.+...#.....', and m[i+1][j]=='#' and m[i][j-
'....#+#...#', 1:j+2]=='#+#'):
print((i,j))
'.....#...#+']
And you want to locate all the positions of the star by
printing out the positions of their centers :
#
#+#
#
E.g.
>>> find_star(map1)
(1, 7)
(4, 5)
Fill in the blanks in the following code to complete the
function find_star():
def find_star(m):
n_row = len(m)
n_col = len(m[0])
for i in (__1__):
for j in (__2__):
if (__3__):
print((i,j))