Peer Instruction: Functions Multiple Choice QuestionsΒΆ
- Raaarrr I'm a bear
- Correct! The function "foo()" has been called in the last line. So, the print statement under it will be executed.
- Eeek a bear!
- Incorrect! The function "foo()" has been called in the last line and NOT "bar()". So, the print statement under "foo()" will be executed and returned.
- Both
- Incorrect! The function "foo()" has been called in the last line and NOT "bar()". So, the print statement under "foo()" will be executed and returned.
- Neither
- Incorrect! The function "foo()" has been called in the last line and NOT "bar()". So, the print statement under "foo()" will be executed and returned.
- I don't know
- Incorrect! The function "foo()" has been called in the last line and NOT "bar()". So, the print statement under "foo()" will be executed and returned.
Q-1: What does the following code print?
def foo():
print("Raaarrr I'm a bear")
def bar():
print("Eeek a bear!")
foo()
- 0
- Incorrect! Change in the values of a function's arguments doesn't change the assigned value of a variable. Try replacing "first(a)" with "first(x)" in function definition.
- 8
- Incorrect! Change in the values of a function's arguments doesn't change the assigned value of a variable. Try replacing "first(a)" with "first(x)" in function definition.
- 20
- Correct! Change in the values of a function's arguments doesn't change the assigned value of a variable.
- Error, because a cannot be assigned in two places
- Incorrect! Change in the value of a function's arguments doesn't change the assigned value of a variable. Try replacing "first(a)" with "first(x)" in function definition.
- I don't know
- Incorrect! Change in the values of a function's arguments doesn't change the assigned value of a variable. Try replacing "first(a)" with "first(x)" in function definition.
Q-2: What does the following code print?
def first(a):
a=8
a = 20
first(a)
print(a)
- 5
- Incorrect! Here, a=x=2 and b= 3+1 = 4. So, a + b + 3 = 2 + 4 + 3 = 9.
- 9
- Correct! Here, a=x=2 and b= 3+1 = 4. So, a + b + 3 = 2 + 4 + 3 = 9.
- 0
- Incorrect! Here, a=x=2 and b= 3+1 = 4. So, a + b + 3 = 2 + 4 + 3 = 9.
- 3
- Incorrect! Here, a=x=2 and b= 3+1 = 4. So, a + b + 3 = 2 + 4 + 3 = 9.
- I don't know
- Incorrect! Here, a=x=2 and b= 3+1 = 4. So, a + b + 3 = 2 + 4 + 3 = 9.
Q-3: What does the following code print?
def calculate(w, x, y):
a=x
b=w+1
return a + b + 3
print(calculate(3, 2, 0))
- x = f1()
- Correct! Only function "f1()" has a return statement.
- x = f2()
- Incorrect! Only function "f1()" has a return statement.
- x = f3()
- Incorrect! Only function "f1()" has a return statement.
- All of the above
- Incorrect! Only function "f1()" has a return statement.
- I don't know
- Incorrect! Only function "f1()" has a return statement.
Q-4: Which sets x
to the value 5?
def f1():
return 5
def f2():
print(5)
def f3():
return print(5)
- No bugs. The code is fine.
- Incorrect! Body of the function is not indented.
- The function body is not indented.
- Correct! Function body should be indented in Python.
- We use x as both a parameter and a variable, but we are not allowed to do that
- Incorrect! x can be defined as both a parameter and variable in Python.
- B and C
- Incorrect! Function body should be indented in Python. x can be defined as both a parameter and variable in Python.
- I don't know
- Incorrect! Body of the function is not indented.
Q-5: Which of the following are true about this code?
def add_one(x):
return x + 1
x = 2
x = x + add_one(x)
- 8, 8
- Incorrect! Here, odd(4, 2) will return 15 (y = y + 1 = 5 and x = x + 1 = 3) and print(x*y) in main() will return 8. The output of odd() will be printed first as it is initialized first.
- 15, 15
- Incorrect! Here, odd(4, 2) will return 15 (y = y + 1 = 5 and x = x + 1 = 3) and print(x*y) in main() will return 8. The output of odd() will be printed first as it is initialized first.
- 8, 15
- Incorrect! Here, odd(4, 2) will return 15 (y = y + 1 = 5 and x = x + 1 = 3) and print(x*y) in main() will return 8. The output of odd() will be printed first as it is initialized first.
- 15, 8
- Correct! Here, odd(4, 2) will return 15 (y = y + 1 = 5 and x = x + 1 = 3) and print(x*y) in main() will return 8. The output of odd() will be printed first as it is initialized first.
- I don't know
- Incorrect! Here, odd(4, 2) will return 15 (y = y + 1 = 5 and x = x + 1 = 3) and print(x*y) in main() will return 8. The output of odd() will be printed first as it is initialized first.
Q-6: What does the following code print?
def odd(y,x):
y = y +1
x = x + 1
print(x*y)
def main():
x = 2
y = 4
odd(x,y)
print(x*y)
- Example
- Correct! The function returns a boolean but the example returns an integer.
- Type contract
- Incorrect! The name and type of parameters have been clearly defined. Option A is the answer becauase the function returns a boolean but the example returns an integer.
- Header
- Incorrect! The syntax of function's header is correct. Option A is the answer becauase the function returns a boolean but the example returns an integer.
- Description
- Incorrect! The function has been clearly descibed. Option A is the answer becauase the function returns a boolean but the example returns an integer.
- Body
- Incorrect! The body of the function has been defined accurately. Option A is the answer becauase the function returns a boolean but the example returns an integer.
Q-7: One of the components of this function is inconsistent with the rest. Which is it?
def sum_eleven(num1, num2, num3):
'''(int, int, int) -> bool
Return True iff num1, num2, and num3 sum to 11.
>>>sum_eleven (4, 5, 2)
11
'''
return num1 + num2 + num3 == 11
- Example
- Incorrect! The example has been correctly defined. Option B is right because the function returns an integer but the type of contract indicates bool.
- Type contract
- Correct! Option B is right because the function returns an integer but the type of contract indicates bool.
- Header
- Incorrect! The syntax of function's header is correct. Option B is right because the function returns an integer but the type of contract indicates bool.
- Description
- Incorrect! The function has been clearly descibed. Option B is right because the function returns an integer but the type of contract indicates bool.
- Body
- Incorrect! The body of the function has been defined accurately. Option B is right because the function returns an integer but the type of contract indicates bool.
Q-8: One of the components of this function is inconsistent with the rest. Which is it?
def rightmost_digit(a):
'''(int) -> bool
Return the rightmost digit of a.
>>>rightmost_digit (14)
4
'''
return a % 10
- >>> num_pizzas(1, 2, 3) 2
- Correct! The group will need atleast 11 slices. Since each pizza has 8 slices, the group would need atleast 2 pizzas.
- >>> num_pizzas(1, 2, 3) 1
- Incorrect! The group will need atleast 11 slices. Since each pizza has 8 slices, the group would need atleast 2 pizzas.
- >>> num_pizzas(1, 2, 3) 9
- Incorrect! The group will need atleast 11 slices. Since each pizza has 8 slices, the group would need atleast 2 pizzas.
- >>> num_pizzas(1, 2, 3) 6
- Incorrect! The group will need atleast 11 slices. Since each pizza has 8 slices, the group would need atleast 2 pizzas.
Q-9: At a pizzeria, adults order two slices, boys order three slices, and girls order one slice. Each pizza has eight slices. Write a function that takes three parameters representing the number of adults, boys, and girls, and returns the required number of pizzas. Which example is correct?
You have attempted of activities on this page