Académique Documents
Professionnel Documents
Culture Documents
def f(x):
d=0
while x > 1:
(d,x) = (d+1,x/10)
return(d)
def h(m,n):
ans = 1
while (n > 0):
(ans,n) = (ans*m,n-1)
return(ans)
Q3 What can we say about a number n if g(n) returns True for the function g given below?
def g(n):
for i in range(2,n):
if n%i == 0:
return(False)
return(True)
n is a prime number.
n is a composite number.
n is multiple of 2.
def gcd(m,n):
if m < n:
(m,n) = (n,m)
if (m % n) == 0:
return(n)
else:
diff = m-n
return (gcd(max(n,diff),min(n,diff)))
Correct answer: (c) The behaviour depends on the exact values of m and n
Try it out in the Python interpreter and see for yourself!
Q5 One of the following 10 statements generates an error. Which one? (Your answer should be a
number between 1 and 10.)
x = [1,"abcd",2,"efgh",[3,4]] # Statement 1
y = x[0:50] # Statement 2
z = y # Statement 3
w = x # Statement 4
x[1] = x[1] + 'd' # Statement 5
x[1][1] = 'y' # Statement 6
y[2] = 4 # Statement 7
z[0] = 0 # Statement 8
w[4][0] = 1000 # Statement 9
a = (x[4][1] == 4) # Statement 10
1 point
Correct answer: 6
Statement 6 is trying to update a position in a string. x[1] is "abcd" and x[1][1] is "b".
x = [13,4,17,1000]
w = x[1:]
u = x[1:]
y = x
u[0] = 50
y[1] = 40
startmsg = "hello"
endmsg = ""
for i in range(0,len(startmsg)):
endmsg = startmsg[i] + endmsg
Q8 What is the value of mylist after the following lines are executed?
def mystery(l):
l = l + l
return()
mylist = [31,24,75]
mystery(mylist)
1 point
def mystery(l):
if l == []:
return (l)
else:
return (l[-1:] + mystery(l[:-1]))
marks =
{"Quizzes":{"Mahesh":[3,5,7,8],"Suresh":[9,4,8,8],"Uma":[9,9,7,6]},"Exams":{"Ma
hesh":[37],"Uma":[36]}}
marks["Exams"]["Suresh"].extend([44])
marks["Exams"]["Suresh"].append(44)
marks["Exams"]["Suresh"] = [44]
marks["Exams"]["Suresh"][0:] = [44]
d = {}
d[[1,2]] = 5
d[(1,2)] = 5
d["1,2"] = 5
d[12] = 5
Q14 Suppose u and v both denote sets in Python. Under what condition can we guarantee that u|v ==
u^v?
Q15 Suppose we insert 97 into the max heap [98,67,89,38,42,54,89,17,25]. What is the resulting heap?
1 point
Q16 Suppose we we apply delete_max() twice to the heap [100,97,93,38,67,54,93,17,25,42]. What is the
resulting heap?
1 point
fjadbihgec
1 point
Q18 We want to add a function length() to the class Node that implements user defined lists which will
compute the length of a list. An incomplete implementation of length() given below. You have to provide
an expression to put in place of *** on the last line.
def length(self):
if self.value == None:
return(0)
elif self.next == None:
return(1)
else:
return(***)
1 point
1 point
Q19 Suppose we add this function foo() to the class Tree that implements search trees. For a
name mytree with a value of type Tree, what would mytree.foo() compute?
def foo(self):
if self.isempty():
return(0)
elif self.isleaf():
return(1)
else:
return(1 + max(self.left.foo(),self.right.foo()))
Correct answer: (c) The length of the longest path from root to leaf in mytree.
The height of a tree is the length of the longest path to a leaf. This is 1 plus the longest path in either the
left or the right subtree.
A preorder traversal lists the vertices of a binary tree (not necessarily a search tree) as follows:
Hint: The preorder traversal tells you the root of the tree. By locating the root in the inorder traversal, you
can identify the nodes in the left and right subtrees. In this way, you can recursively reconstruct the tree
from the two traversals.
1 point
Correct answer: b
From the preorder traversal, g is the root of the tree. From the inorder traversal, the nodes in the left
subtree are ehbic. Referring back to the preorder traversal, the root of this subtree is b.