0% found this document useful (0 votes)
5 views9 pages

CS1010E-202223-S1 Final With Answerspdf

Uploaded by

xuweizheng45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

CS1010E-202223-S1 Final With Answerspdf

Uploaded by

xuweizheng45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CS1010E Final (AY2022/2023, SEM1)

Section 1 Syntax and Python usage 1.5 x 40 = 60 marks


QN Questions Answer
You can consider the code in each question is in a separate
file. What will be the output when we run the file in IDLE?
1 print(9-2*3-1) *a) 2
b) 20
c) 5
d) 4
e) 14
2 print(3**--3) *a) 27
b) 9
c) -9
d) 0
e) Error
3 print(False or True) *a) True
b) False
c) 1
d) 0
e) None of the rest
4 print((False == False) in [False]) *a) False
b) True
c) 0
d) 1
e) Error
5 print(True != False in [False]) *a) True
b) False
c) 1
d) 0
e) Error
6 print(10>10 and 4<(9/0)) *a) False
b) True
c) 1
d) 0
e) Error
7 print('abc'>'abbbbbbbb') *a) True
b) False
c) 'abc'
d) 'abcabbbbbbbb'
e) Error
8 print(max('12','111')+max('3','0')) *a) 123
b) 111
c) 15
d) 114
e) 1113
9 print('1234567890'[1]) *a) 2
b) 1
c) 0
d) 3
e) None
10 print('12345'[1:5][0:4][2][0]) *a) 4
b) 1
c) 2
d) 3
e) Error
11 print((2)+(3,)) *a) Error
b) (2, 3)
c) (5,)
d) 5
e) (2, (3,))
12 print([9,[8,[7,6],[5,4]],3][1][2][3:5]) *a) []
b) 5
c) 4
d) 6
e) Error
13 print(['123','456']['012'[1]]) *a) Error
b) Empty string
c) 456
d) 123
e) 4
14 A = ['a','b'] *a) [[['b'], 'c'],
B = [A,'c'] ['b']]
C = [B,A] b) [['b', 'c'], ['b']]
B[0].remove('a') c) [[['b'], 'c'],
print(C) ['a','b']]
d) [[['a','b'], 'c'],
['b']]
e) [['b', 'c'],
['a','b']]
15 L1 = [0,1] *a) [[0, 999], 2, 3]
L2 = [L1,2,3] b) [[0, 1], 2, 3]
L3 = L2.copy() c) [[999, 1], 2, 3]
L3[0][1] = 999 d) [999, 2, 3]
print(L2) e) Error
16 L1 = ([1,2],3,4) *a) ([9, 2], 3, 4)
def f(L): b) ([1, 2], 3, 4)
L[0][0] = 9 c) [[1, 2], 3, 4]
f(L1) d) (9, 3, 4)
print(L1) e) Error
17 B = [1,2] *a) 1
B.append(B) b) [1, 2]
print(B[2][2][2][2][2][2][0]) c) [1, 2, [...]]
d) Error
e) [[1, 2, [...]]]
18 print((9,8,7)*2+(1,2,3)[1]) *a) Error
b) (9, 8, 7, 1)
c) (9, 8, 7, 2)
d) (11, 10 , 9)
e) (9, 8, 7, 9, 8, 7,
2)
19 A = ([1,2,3],[5,6,7]) *a) ([1, 2, 3, 4], [5,
A[0].append(4) 6, 7])
print(A) b) ([1, 2, 3], [5, 6,
7])
c) Error
d) ([1, 2, 3], [5, 6,
7], 4)
e) [[1, 2, 3, 4], [5,
6, 7]]
20 def f(x): *a) 15
return x+2 b) 13
print(f(f(f(f(7))))) c) 7
d) 28
e) 14
21 def a(x): *a) RecursionError
return 1+c(x) b) NameError
def b(x): c) Infinite loop
return 2+d(x) d) 8
def c(x): e) 6
return 1+a(x)
print(a(1))
22 def f(x): *a) TypeError
if x > 0: b) None
return 1+f(x-2) c) 0
print(f(10)) d) 5
e) RecursionError
23 x = 100 *a) 90
for j in range(0,100,10): b) 0
x -=1 c) 91
print(x) d) 1
e) 100
24 x = 0 *a) 360
for i in range(0,12): b) 0
for j in range(0,10): c) 25
for k in range(0,3): d) 198
x += 1 e) 22
print(x)
25 x,y = 0,0 *a) 15
while x < 10: b) 10
x += 2 c) 12
y += 3 d) 8
print(y) e) 27
26 x = 0 *a) 0
for i in range(0,1000000,25): b) RecursionError
x = 1-x c) 40000
print(x) d) - 40000
e) 1
27 x = 10 *a) 2
if x > 10: b) 1
y = 1 c) 3
elif x < 100: d) 4
y = 2 e) Error
elif x == 10:
y = 3
else:
y = 4
print(y)
28 y = 'a' *a) 4
if y == 'b': b) 2
x = 1 c) 3
elif y == 'a': d) 1
x = 2 e) Error
if y == 'c':
x = 3
else:
x = 4
print(x)
29 print(tuple({9:8,7:6,5:4})) *a) (9, 7, 5)
b) (9, 8, 7, 6, 5, 4)
c) ((9, 8), (7, 6), (5,
4))
d) ({9:8,7:6,5:4},)
e) ({9:8,7:6,5:4})
30 d = {0:2, 1:5, 2:1, 3:2, 4:1, 5:3, 3:9, *a) 239
2:7, 2:3} b) 27
a, output = 0, '' c) 21539
while a in d: d) Infinite Loop
a = d[a] e) 20
output += str(a)
print(output)
31 f1 = lambda x,y : x+y *a) 11
f2 = lambda a,b,c: a(b,c) b) 28
print(f2(f1,4,7)) c) a function
d) Error
e) None
32 f = lambda x:x%4 *a) 7
print(len(list(filter(f,range(0,10))))) b) 3
c) 10
d) 12
e) 1
33 print(list(map(lambda x:x*3,'123'))) *a) ['111', '222',
'333']
b) ['123123123']
c) '123123123'
d) ['123', '123',
'123']
e) Error
34 class Graduate: *a) BSc Alan
def __init__(self,name,title): b) Dr Alan
self.name = name c) Alan
self.title = title d) Dr
self.fullname = title+" "+name e) Bsc

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.

For all the fill-in-the-blank questions, please do not


enter spaces before (on the left side) of your answers.
Or it will be deemed as wrong answer
45 Given a list of unique numbers L, we want to check if def check_ascending(L):
for i in range(0,len(L)-1):
the list L is sorted in an ascending order. E.g. if (L[i]>L[i+1]):
>>> print(check_ascending([1,2,3])) return False
return True
True
>>> print(check_ascending([1,4,3]))
False
Complete the following code for the function:
def check_ascending(L):
for i in (__BLANK1__):
if (__BLANK2__):
return (__BLANK3__)
return (__BLANK4__)

46 Implement a sorting function to sort a list of numbers def my_sort(L):


by filling in the blanks. Your function should modify the for i in
input list and sort it without returning a new list. Sample range(len(L)):
output: for j in
>>> L = [4,1,2,3,8,1,2,3,9,6] range(i,len(L)):
>>> my_sort(L) if
>>> print(L) L[j]<L[i]:
[1, 1, 2, 2, 3, 3, 4, 6, 8, 9]
Fill in the blanks the blanks: L[i],L[j] = L[j],L[i]
def my_sort(L):
for i in (__BLANK1__):
for j in (__BLANK2__):
if (__BLANK3__):
L[BLANK4],L[BLANK5] =
L[BLANK6],L[BLANK7]
Hint: You should use selection sort or bubble sort

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))

48 The following is the very slow code for our recursive X = {}


binomial coefficient computation in Assignment 3. def nCk(n,k):
def nCk(n,k): if (n,k) in X:
if n==k or k==0: return
return 1 X[(n,k)]
ans = nCk(n-1,k-1)+nCk(n-1,k) if n==k or k==0:
return ans return 1
Use one technique taught in our class to speed up the ans = nCk(n-1,k-
code by filling in the blanks: 1)+nCk(n-1,k)
X = __BLANK1__ X[(n,k)]=ans
def nCk(n,k): return ans
if (__BLANK2__):
return (__BLANK3__)
if n==k or k==0:
return 1
ans = nCk(n-1,k-1)+nCk(n-1,k)
X[__BLANK4__]=__BLANK5__
return ans

You might also like