Functions Practice Quiz¶
CS20-FP3 Construct and utilize functions to create reusable pieces of code.
To confirm that you understand the major concepts you’ve seen in Python, try to answer the following questions without opening Python.
Question 1¶
- def some_function()
- Try again! Something is missing at the end...
- function some_function()
- Try again! We don't use the word function...
- function some_function():
- Try again! We don't use the word function...
- def some_function():
- Great!
function-quiz1: Which of the following is the valid way to begin the definition of a function in Python?
Question 2¶
- 5
- Try again! The first_value is returned.
- 21
- Great!
- 18
- Try again! 8 is added to the first_value, not the second_value.
- None of the above.
- Try again!
function-quiz2: What will the following program print?:
def some_thing(number1, number2):
first_value = number1 + 8
second_value = number2 - 5
return first_value
print(some_thing(13, 10))
Question 3¶
- 5
- Try again! Consider what Python is doing with the answer that is being returned.
- 21
- Try again! Consider what Python is doing with the answer that is being returned.
- 18
- Try again! Consider what Python is doing with the answer that is being returned.
- None of the above.
- Great! Although the function is called, nothing is actually printed!
function-quiz3: What will the following program print?:
def some_thing(number1, number2):
first_value = number1 + 8
second_value = number2 - 5
return first_value
some_thing(13, 10)
Question 4¶
function-quiz4: What will the following program print?:
def some_thing(number1, number2):
first_value = number1 + 8
second_value = number2 - 5
temp_value = other_thing(second_value)
return temp_value
def other_thing(another_value):
return (another_value + 5) * 3
print(some_thing(13, 10))
Question 5¶
- 30
- Try again! Think about variable scope.
- An error will occur.
- Great! Since the second_value variable is defined inside the some_thing function, you cannot access it from anywhere else in your code.
- 5
- Try again! Although the value of second_value is 5 while your code is inside the some_thing function, we are trying to print that value outside the some_thing function.
- None of the above.
- Try again!
function-quiz5: What will the following program print?:
def some_thing(number1, number2):
first_value = number1 + 8
second_value = number2 - 5
temp_value = other_thing(second_value)
return temp_value
def other_thing(another_value):
return (another_value + 5) * 3
some_thing(13, 10)
print(second_value)
Question 6¶
function-quiz6: What will the following program print?:
def surprising_function(value):
thing = 0
for counter in range(value+1):
thing = thing + counter
return thing
print(surprising_function(5))
Question 7¶
function-quiz7: What will the following program print?:
def a(x, y):
x = x + 3
y = y + 2
return x+y
x = 5
y = 10
z = a(x, y)
print(z)
Question 8¶
function-quiz8: What will the following program print?:
def a(x, y):
x = x + 3
y = y + 2
return x+y
x = 5
y = 10
z = a(x, y)
print(x)