Example test
All answers must be explained! If a program throws an error, describe the cause (it is not necessary to provide the exact text of the error message).
1. What is the output of the following program?
number = 4
number = 7
print(number)
llist = [-1, 4, 5, -2]
llist[1] = 7
print(llist)
mx = [[1, 2, 3], [-1, -2, -3], [1, 0, -2], [4, 10, -3]]
print(mx[3][:2])
mxa = [[1, 2, 3], [-1, -2, -3], [4, -5, 6]]
part = []
for i in range(len(mxa)):
for j in range(len(mxa[i])):
if i == len(mxa[i]) - j - 1:
part.append(mxa[j][i])
print(part)
2. What is the output of the following program?
musicians = ("John", "Paul", "George", "Ringo")
a = [1941, 1842, 1943]
a[1] = 1942
d = {musicians[0]: a[0], musicians[2]: a[2], musicians[3]: a[0]}
d["Paul"] = a[1]
print(d["John"])
print(d["Paul"])
d["John"] = d.get("John",-1)
d["Bill"] = d.get("Bill",-1)
print(d["John"])
print(d["Bill"])
for i, j in d.items():
print(j, i)
3. What is the output of the following program?
s1 = {1, 2 ,3 ,4}
s2 = {3, 5, 6, 7}
s2.add(7)
s3 = {4, 6, 7}
s4 = s1 | s2 & s3
print(s4 ^ {1, 2, 5})
Hint: check the operator precedence in Python
4. What is the output of the following program?
def recFun(x, y):
print(x)
if y < 0:
return x
else:
return recFun(x + 2, y - 1)
print(recFun(-2, 1) + recFun(1, 3))