Skip to main content

Section 3.8 Functions Multiple-Choice Exercises

Most of the exercises below are from the Python for Everybody Interactive ebook on Runestone by Dr. Barb Ericson.

Activity 3.8.1.

What will the following Python program print out? (Given that each word will actually print on a new line)
def fred():
    print("Zap")

def jane():
    print("ABC")

jane()
fred()
jane()
  • Zap ABC jane fred jane
  • Incorrect! This code will only print the strings "Zap" or "ABC", not "jane" or "fred". Try again.
  • Zap ABC Zap
  • Incorrect! This would be correct if the call was fred() jane() fred(). Try again.
  • ABC Zap jane
  • Incorrect! This code will only print the strings "Zap" or "ABC", not "jane" or "fred". Try again.
  • ABC Zap ABC
  • Correct! jane() fred() jane() will print ABC Zap ABC.
  • Zap Zap Zap
  • Incorrect! This would be correct if the call was fred() fred() fred(). Try again.

Activity 3.8.2.

What value is printed when the following code is executed?
name = "Jane Doe"
def myFunction(parameter):
    value = "First"
    value = parameter
    print (value)

myFunction("Second")
  • value
  • Incorrect! "value" is the name of a variable. This code will print what "value" is assigned to. Try again.
  • Second
  • Correct! "value" is assigned to the value of parameter which is "Second", so that’s what will print.
  • parameter
  • Incorrect! "parameter" is equal to the function call’s argument. Try again.
  • First
  • Incorrect! Although the variable value is assigned to "First" initially, it is then reassigned to be equal to the parameter. Try again.
  • Jane Doe
  • Incorrect! The "name" variable is unused in the function call, so it does not affect what is printed. Try again.

Activity 3.8.3.

What will the following code print?
def pow(b, p):
    y = b ** p
    return y

def square(x):
    a = pow(x, 2)
    return a

n = 5
result = square(n)
print(result)
  • 5
  • This would be true if it first printed the value of x.
  • 10
  • This would be true if it printed 5 * 2 but it is 5 ** 2 which is 5 * 5.
  • 25
  • It prints the value of 5 raised to the 2nd power which is 5 * 5 = 25.
  • 32
  • This would be true if it was 2 raised to the 5th power (2 * 2 * 2 * 2 * 2) = 32.
  • 3125
  • This would be ture if it was 5 raised to the 5th power (5 * 5 * 5 * 5 * 5) = 3125.

Activity 3.8.4.

Consider the following Python code. What does this code print?
def rem(a, b):
    return a % b

print(rem(3,7))
  • 0
  • This would be true if it was rem(3,3). The value 3 would go into 3 one time with 0 remainder.
  • 3
  • The value 7 goes into 3 zero times, so the remainder is 3.
  • 7
  • How many times does 7 go evenly into 3 and what is the remainder?
  • 1
  • This would be true if it was rem(7,3)

Activity 3.8.5.

After running the following code, what will the output be?
def multiplication_one(num1, num2):
    num1 * num2

print(multiplication_one(5, 10))

def multiplication_two(num1, num2):
    return num1 * num2

multiplication_two(5, 10)
  • None will be outputted after printing and calling multiplication_one(5, 10). Nothing will be outputted after calling multiplication_two(5, 10).
  • Correct!
  • Nothing will be outputted after printing and calling multiplication_one(5, 10). None will be outputted after calling multiplication_two(5, 10).
  • Incorrect! None is printed when you print and call a function and there is no return statement in the function body. Nothing is outputted when you call a function that only has a return statement and you don’t print the function call.
  • 50 will be outputted after printing and calling multiplication_one(5, 10) and after calling multiplication_two(5, 10).
  • Incorrect! None is printed when you print and call a function and there is no return statement in the function body. Nothing is outputted when you call a function that only has a return statement and you don’t print the function call.
  • None will be outputted after printing and calling multiplication_one(5, 10). 50 will be outputted after calling multiplication_two(5, 10).
  • Incorrect! Nothing is outputted when you call a function that only has a return statement and you don’t print the function call.
  • 50 will be outputted after printing and calling multiplication_one(5, 10). Nothing will be outputted after calling multiplication_two(5, 10).
  • Incorrect! None is printed when you print and call a function and there is no return statement in the function body.

Activity 3.8.6.

What would be printed after running the code below? (Note: Ignore whitespaces.)
def addition(num1, num2):
    return(num1 + num2)

def subtraction(num1, num2):
    print(num1 - num2)

def main():
    add_answer = addition(2, 4)
    new_add_answer = addition(add_answer, 105)
    print(subtraction(new_add_answer, 200))

main()
  • None and -89
  • Incorrect! When you print a function call (e.g., print(subtraction(new_add_answer, 200))) and the function prints an output (e.g., def subtraction(num1, num2): print(num1 - num2)), the output will be printed first due to the function call. Then, printing the function call will output None because the function does not have a return statement.
  • None
  • Incorrect! When you print a function call (e.g., print(subtraction(new_add_answer, 200))) and the function prints an output (e.g., def subtraction(num1, num2): print(num1 - num2)), the output will be printed first due to the function call. Then, printing the function call will output None because the function does not have a return statement.
  • -89
  • Incorrect! When you print a function call (e.g., print(subtraction(new_add_answer, 200))) and the function prints an output (e.g., def subtraction(num1, num2): print(num1 - num2)), the output will be printed first due to the function call. Then, printing the function call will output None because the function does not have a return statement.
  • -89 and None
  • Correct!

Activity 3.8.7.

Which of the following values for x, y, and z would result in the function returning β€œTrue”?
def addition(x,y,z):
   if (x + y) == z:
      return True
   else:
      return False
  • x = 5, y = 6, z = 11
  • Correct!
  • x = 1, y = 5, z = 6
  • Correct!
  • x = 1, y = 3, z = 10
  • Try again! The value of z should be 4 in order to return "True".
  • x = -2, y = 1, z = -1
  • Correct!
  • x = 50, y = 41, z = 94
  • Try again! The value of z should be 91 in order to return "True".

Activity 3.8.8.

Given the code below, what would the function print?
def countodd(lst):
   num_of_odd = 0
   for item in lst:
      if item % 2 == 1:
         num_of_odd += 1

   return num_of_odd

print(countodd([1,2,3,4,5]))
  • 1
  • Try again! This function uses modulus to find the amount of odd numbers in the list that is passed in.
  • 2
  • Try again! This function uses modulus to find the amount of odd numbers in the list that is passed in.
  • 3
  • Correct!
  • 4
  • Try again! This function uses modulus to find the amount of odd numbers in the list that is passed in.
  • 5
  • Try again! This function uses modulus to find the amount of odd numbers in the list that is passed in.
You have attempted of activities on this page.