Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 1. What will the following code print out?
x = 0
if x > 10:
print(True)
else:
print(False)
# 2. Refer to the following function
def f(a, b=0):
if 0 in a:
if b == 0:
return 0
else:
return b
else:
if b == 0:
return 0
else:
return sum(a)
# 2.a What will the function return given these parameter values?
f([-1,0,1])
# 2.b Given parameter values such that the function will return the value 1.0?
# 3. Is the following function functionally the same as the previous one in #2?
# Explain why/why not.
def f(a, b=0):
if 0 in a:
if b == 0:
return 0
return b
if b == 0:
return 0
return sum(a)
# 4. What will the following code return?
def f(a, b, c):
ans = True
for i in (a,b,c):
if i > 0:
ans = ans & True
else:
ans = ans & False
return ans
f(10, 6, -1)
# 5. Refer to the following piece of code
d = {}
for i in range(100):
d[str(i)] = i + 1
# 5.a What is d? (string, list, integer variable, etc...)
# 5.b What is the value of d[0]?
# 6. Refer to the following function
def f(a):
if a == 0:
return 0
else:
return a + f(a-1)
def g(a):
x = 0
for i in range(a+1):
x += i
return x
# 6.a Are f and g functions functionally the same? Always/Sometimes/Never? Explain.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment