Given two modules testmod.py and runmod.py
#testmod.py
x = 0
def f1():
x = 10
def f2():
global x
x += 1
def f3():
import testmod
testmod.x += 1
def f4():
import sys
sys.modules["testmod"].x += 1
#runmod.py
import testmod as t
t.f1();t.f2();t.f3();t.f4()
print(t.x)
The entry point to the program is the runmod.py module.
What will be the output?
What will be the output?
Anonymous Quiz
10%
0
20%
1
20%
2
30%
3
0%
10
0%
11
10%
12
0%
13
10%
See solution
What will this code do?
def maker(n):
s = 0
def g(x=n):
nonlocal s
s += x
return s
return lambda x: s + x + g()
f = maker(2)
print(f(3), f(3))
Choose your answer
Anonymous Quiz
13%
3 5
25%
5 5
25%
7 7
25%
5 7
0%
5 9
0%
7 9
0%
9 9
13%
See solution
What will this code do?
f = lambda x, f=(lambda x: x**2): f(x)
print(f(5), f(5, f))
Choose your answer
Anonymous Quiz
8%
5 5
8%
5 (5, 5)
15%
5 25
8%
5 125
15%
5 (25, 25)
23%
25 25
0%
25 125
0%
125 125
8%
5 (125, 125)
15%
See solution
What will this code do?
def maker(n, h=lambda: 3):
return lambda f=h: f()**n, lambda f=h: n**f()
f, g = maker(2)
r = f(g) + g(f)
print(r)
Choose your answer
Anonymous Quiz
9%
5
36%
17
0%
31
9%
72
9%
73
0%
128
18%
576
0%
19699
18%
See solution
What will this code do?
funcs = [lambda x: x**i for i in range(2, 4)]
print(funcs[0](5))
Choose your answer
Anonymous Quiz
16%
5
32%
25
11%
[25]
16%
125
21%
[25, 125]
0%
625
5%
See solution
What will this code do?
def f():
def g():
nonlocal x
x = 2
g()
print(x)
f()
👍1
👍1
What will this code do?
def f(name="", value=0):
import sys
func_name = sys._getframe(0).f_code.co_name
func = sys._getframe(1).f_locals[func_name]
setattr(func, name, value)
return sum(func.__dict__.values())
print(f("x", 1), f("y", 2), f())
Choose your answer
Anonymous Quiz
20%
1 1 1
0%
1 2 2
0%
1 2 3
40%
1 3 3
10%
1 2 None
0%
3 3 3
30%
See solution