# Peer Instruction: Function Multiple Choice Questions¶

- Correct! Variable 'smallest' here will always be the smallest number among a, b and c.
- Try again. Variable 'smallest' is not defined in this program.
- Try again. This will give a type error because min() is missing positional arguments: a, b and c. And this question only requires to write a function. So min() is not necessary.
- Try again. This will output the biggest number among a, b and c.
- I don't know
- Try again. The hint is: we can compare a, b and c in pairs using If block. After each comparison, we make variable 'smallest' equal to the smaller number in the pair.

Q-1: Write a Python function called min(a, b, c) that takes in integers a, b, c and returns the smallest.

```
A.
def min(a, b, c):
smallest = a
if (b < smallest):
smallest = b
if (c < smallest):
smallest = c
return smallest
```

```
B.
def min(a, b, c):
min = a
if (b > min):
min = b
if (c < smallest):
min = c
```

```
C.
def min(a, b, c):
smallest = a
if (b < smallest):
smallest = b
if (c < smallest):
smallest = c
min()
```

```
D.
def min(a, b, c):
smallest = a
if (b > smallest):
smallest = b
if (c > smallest):
smallest = c
return smallest
```

- Try again. Let's first break woot(3) down into four parts: print(3), yar(4), print(x) and yar(5). And your problem might be related to yar(4). Because 4 < 5, yar(4) jumps to Else block and leads to foo (5). foo(5) leads to prints(4) and yar(6). Because 6 > 5, yar(6) leads to print(6*2). So yar(4) outputs 4 and 12.
- Correct! woot(3) can be broken down into four parts: print(3), yar(4), print(x) and yar (5).
- Try again. Let's first break woot(3) down into four parts: print(3), yar(4), print(x) and yar (5). And your problem might be related to yar(5). Because 7 > 5, yar(7) prints (7 * 2), which is 14.
- None of the above
- Try again. woot(3) can be broken down into four parts: print(3), yar(4), print(x) and yar(5). The difficulty is that yar(4) and yar (5) satisfy different conditions. yar(4) activates foo() function while yar(5) doesn't. Try to calculate the output of each part based on the condition.
- I don't know
- Try again. woot(3) can be broken down into four parts: print(3), yar(4), print(x) and yar(5). The difficulty is that yar(4) and yar (5) satisfy different conditions. Try to calculate the output of each part based on the condition.

Q-2: What does this print?

```
def woot(x):
print (x)
yar(x + 1)
print (x)
yar(x + 2)
def yar(y):
if y>5:
print (y ∗ 2)
else:
foo(y + 1)
def foo(z):
print (z − 1)
yar(z + 1)
def main():
woot (3)
main()
```

```
A.
3
4
12
3
10
```

```
B.
3
4
12
3
5
14
```

```
C.
3
4
10
10
24
```

- 1
- Try again. Because n != 1, fact(3) returns 3*fact(2). And 2 != 1, fact(2) returns 2*fact(1). Since 1 == 1, fact(1) returns 1. fact(3) returns 3*2*fact(1), which equals to 6.
- 3
- Try again. Because n != 1, fact(3) returns 3*fact(2). And 2 != 1, fact(2) returns 2*fact(1). Since 1 == 1, fact(1) returns 1. fact(3) returns 3*2*fact(1), which equals to 6.
- 6
- Correct! The program returns different results depending on the value of n, which also involves in iteration.
- This code will cause an error
- Try again. This code falls into a loop but n gradually approaches 1. When n == 1, the program stops.
- I don’t know
- Try again. The program returns different results depending on the value of n. When n != 1, it always returns n*fact(n-1), which leads to another round of iteration. But n gradually approaches 1 during the process. When n == 1, the program stops.

Q-3: This code will return

```
def fact(n):
if (n == 1):
return 1
else:
return n*fact(n - 1)
fact(3)
```

- 4, 3, 2, 1
- Try again. The program will keep running because num(x-1) is infinite. When x = 1, num(x-1) returns num (0), then num (-1), num(-2)...
- 4, 3, 2, 1, 2, 3, 4
- Try again. The program will keep running because num(x-1) is infinite. When x = 1, num(x-1) returns num (0), then num (-1), num(-2)...
- 4, 3, 2, 1, 1, 2, 3, 4
- Try again. The program will keep running because num(x-1) is infinite. When x = 1, num(x-1) returns num (0), then num (-1), num(-2)...
- This code will cause an error.
- Correct! This code falls into a loop without an end, since num(x-1) is indefinite.
- I don't know
- Try again. You can focus on the num() function. There is an endless iteration here because num(4) returns num(3), then num(2), num(1)...

Q-4: This code will return

```
def num(x):
print(x)
num(x - 1)
print(x)
num(4)
```

- 4 3 2 1 3 2 1 1 2 3 4
- Try again. Because 4 > 0, num(4) --> 4 num(3) num(2) 4. Because there is only one num(3), number 3 will only show up twice.
- 4 3 2 1 3 2 1 1 2 3 1 2 3 4
- Try again. Because 4 > 0, num(4) --> 4 num(3) num(2) 4. Because there is only one num(3), number 3 will only show up twice. And as we can see num(4) --> 4 num(3) num(2) 4, num(2) is before 4, so the last two number must be 2 and 4.
- 4 3 2 1 1 2 1 1 3 2 1 1 2 4
- Correct! The function stop when x < 0, but it has two functions embedded in num(4): num(3) and num(2), which makes it a little complicated.
- This code will cause an error
- Try again. This will not report an error because once x < 0 or x = 0, the function will stop.
- I don't know
- Try again. Because 4 > 0, num(4) --> 4 num(3) num(2) 4. Since 3 > 0, num(3) --> 3 num(2) num(1) 3. Because 2 > 0, num(2)--> 2 num(1) num(0) 2. Becuase 1 > 0, num(1) --> 1 1. Try to detach each part as a block.

Q-5: This code will return

```
def num(x):
if x > 0:
print(x)
num(x - 1)
num(x - 2)
print(x)
num(4)
```

- 1
- Try again. Everything in Python is an object. So, your functions can return numeric values ( int , float , and complex values), collections and sequences of objects... But a + b here is not an object. So it will report an error.
- 2
- Try again. Everything in Python is an object. So, your functions can return numeric values ( int , float , and complex values), collections and sequences of objects... But a + b here is not an object. So it will report an error.
- 5
- Try again. Everything in Python is an object. So, your functions can return numeric values ( int , float , and complex values), collections and sequences of objects... But a + b here is not an object. So it will report an error.
- This code will cause an error
- Correct! Everything in Python is an object. So, your functions can return numeric values ( int , float , and complex values), collections and sequences of objects... But a + b here is not an object. So it will report an error.
- I don't know

Q-6: This code will return

```
def noob(x):
if x < 2:
return 1
else:
a = noob(x - 1)
b = noob(x - 2)
return a + b
y = noob(4)
```

- Try again. This function will cause a loop without an end.
- Try again. Option B is correct! But option C is also correct. else block in option C can be removed.
- Try again. Option C is correct! But option B is also correct. else block in option C can be removed.
- Both B and C
- Correct! Option B and C actually express the same meaning.
- I don't know
- Try again. Examine whether each option can end by itself! Some will cause a loop without an end.

Q-7: Which code is correct?

```
A.
def fac(x):
return x*fac(x - 1)
```

```
B.
def fac(x):
if x == 0:
return 1
return x*fac(x - 1)
```

```
C.
def fac(x):
if x == 0:
return 1
else:
return x*fac(x - 1)
```

- Yes
- Try again. The function examines whether the sequence is symmetrical by comparing letters counrting backwards with the ranked order. However, if the wordhas odd number of letters, when the functioon approaches the middle of the sequence, it will report string index out of range error. Becuase len(x) night equal to 0, leading x[1:len(x)-1] out of range.
- No
- Correct! The function will report an error when it encounters a word with odd number of letters. Becuase len(x) night equal to 0, leading x[1:len(x)-1] out of range.
- I don't know
- Try again. The function examines whether the sequence is symmetrical by comparing letters counrting backwards with the ranked order. Try to test it with a word with odd number of letters.

Q-8: Will this code return Palindromes?

```
def pal(x):
if x[0] != x[len(x) - 1]:
return False
else:
return pal(x[1:len(x) - 1])
```

- Try again. This function only examines whether the first letter is the same with the last one. It has no iteration.
- Try again. This function will report an error. Because when len(x) <= 1, len(x) - 1 <= 0. return pal (x[1:len(x)-1]) will report string index out of range error.
- Try again. When len(x) = 1, if the string satisfies comdition in the else block, it returns pal(x[1:len(x) - 1]). x[1:0] will report string index out of range error.
- Correct! The function examines whether the string is a Palindromes. When len(x) = 1, the functions ends.
- I don't know
- Try again. The function examines whether the string is a Palindromes. It first compares the first letter with the last letter, then the second letter with the seonc last one... Try to focus on how it iterates and its ending conditoon.

Q-9: Which code is correct?

```
A.
def pal(x):
if len(x) <= 1:
return True
elif x[0] != x[len(x) - 1]:
return False
else return pal(x)
```

```
B.
def pal(x):
if x[0] != x[len(x) - 1]:
return False
else return pal(x[1:len(x) - 1])
```

```
C.
def pal(x):
if len(x) <= 0:
return True
elif x[0] != x[len(x) - 1]:
return False
else return pal(x[1:len(x) - 1])
```

```
D.
def pal(x):
if len(x) <= 1:
return True
elif x[0] != x[len(x) - 1]:
return False
else return pal(x[1:len(x)-1])
```

- (3) only
- Try again. Function call is to invoke the program to execute the function. So (1), (3) and (4) are all function calls.
- (2) and (3)
- Try again. Function call is to invoke the program to execute the function. So (1), (3) and (4) are all function calls. (2) is a function that needs to be executed but not a function call.
- (1), (3), and (4)
- Correct! Function call is to invoke the program to execute the function. So (1), (3) and (4) are all function calls.
- All of (1), (2), (3), and (4) include a function call
- Try again. Function call is to invoke the program to execute the function. So (1), (3) and (4) are all function calls. (2) is a function that needs to be executed but not a function call.

Q-10:

```
Which of the following contains a function call?
(1) type(4.5)
(2) def add_one(x):
return x + 1
(3) area(2, 9)
(4) print("Hello")
```

- 5
- Try again. calculate(3, 2, 0) assigns different values to w, x and y. a = x = 2, b = w + 1 = 3 + 1 = 4. Therefore a + b + 3 = 9. The functions returns 9.
- 9
- Correct! calculate(3, 2, 0) assigns different values to w, x and y. a = x = 2, b = w + 1 = 3 + 1 = 4. Therefore a + b + 3 = 9. The functions returns 9.
- 0
- Try again. calculate(3, 2, 0) assigns different values to w, x and y. a = x = 2, b = w + 1 = 3 + 1 = 4. Therefore a + b + 3 = 9. The functions returns 9.
- 3
- Try again. calculate(3, 2, 0) assigns different values to w, x and y. a = x = 2, b = w + 1 = 3 + 1 = 4. Therefore a + b + 3 = 9. The functions returns 9.

Q-11:

```
What is the output of this code?
def calculate(w, x, y):
a = x
b = w + 1
return a + b + 3
print(calculate(3, 2, 0))
```

- 3
- Try again. calculate(1, 2, 0) assigns values 1, 2 and 0 to w, x and y. a = x = 2, b = w + 1 = 1 + 1 = 2. Therefore a + b + 2 = 5. The functions returns 5.
- 4
- Try again. calculate(1, 2, 0) assigns values 1, 2 and 0 to w, x and y. a = x = 2, b = w + 1 = 1 + 1 = 2. Therefore a + b + 2 = 5. The functions returns 5.
- 5
- Correct! calculate(1, 2, 0) assigns values 1, 2 and 0 to w, x and y. a = x = 2, b = w + 1 = 1 + 1 = 2. Therefore a + b + 2 = 5. The functions returns 5.
- 6
- Try again. calculate(1, 2, 0) assigns values 1, 2 and 0 to w, x and y. a = x = 2, b = w + 1 = 1 + 1 = 2. Therefore a + b + 2 = 5. The functions returns 5.

Q-12:

```
What is the output of this code?
def calculate(w, x, y):
a = x
b = w + 1
return a + b + w
print(calculate(1, 2, 0))
```

- No bugs. The code is fine
- Try again. It is required by Python that the body of the function should be indented.
- The function body is not indented
- Correct! The body of the function is not indented.
- We are using x as a parameter and a variable, but we are not allowed to do that
- Try again! It is required by Python that the body of the function should be indented.
- Both B and C are bugs
- Try again! It is required by Python that the body of the function should be indented.

Q-13:

```
What is the output of this code?
def add_one(x):
return x + 1
x = 2
x = x + add_one(x)
```

- 8
- Try again. In fucntions, changes to a in function first(a) do not change the value of a.
- 20
- Correct! In fucntions, changes to a in function first(a) do not change the value of a.
- Error, because a cannot be assigned in two places
- Try again. Parameter passing is just like an assignment statement. When first starts running, a gets the value of 20. And in fucntions, changes to a in function first(a) do not change the value of a.
- None
- Try again. Parameter passing is just like an assignment statement. When first starts running, a gets the value of 20. And in fucntions, changes to a in function first(a) do not change the value of a.
- 0
- Try again. Parameter passing is just like an assignment statement. When first starts running, a gets the value of 20. And in fucntions, changes to a in function first(a) do not change the value of a.

Q-14:

```
def first(a):
a = 8
return a
a = 20
first(a)
print(a)
```

- 8
- Correct! In function first(a), a has been assigned to a new value 8. So first(a) returns 8. And a = first(a) makes a equals to 8.
- 20
- Try again. In function first(a), a has been assigned to a new value 8. So first(a) returns 8, however the value of a is still 20 outside the function until a has been assigned with a new value first(a). Therefore, a equals to 8.

Q-15:

```
def first(a):
a = 8
return a
a = 20
a= first(a)
print(a)
```

- x = f1()
- Correct! f1() returns 5 and 5 has been assigned to x.
- x = f2()
- Try again. f2() only prints 5 but does not return the value to x.
- x = f3()
- Try again. f3() return print(5), but does not assign the value to x.
- Two of the above
- Try again. There is only one function assigning 5 to x.
- All of the above
- Try again. There is only one function assigning 5 to x.

Q-16:

```
Which of the following assigns 5 to x?
def f1():
return 5
def f2():
print(5)
def f3():
return print(5)
```

- 1
- Try again. The output of b(2) is print(a(1)). In a(num), the function returns 2. Therefore, b(2) outputs 2. There is nothing to do with num here.
- 2
- Correct! The output of b(2) is print(a(1)). In a(num), the function returns 2. Therefore, b(2) outputs 2. .
- 4
- Try again. The output of b(2) is print(a(1)). In a(num), the function returns 2. Therefore, b(2) outputs 2. There is nothing to do with num here.
- 8
- Try again. The output of b(2) is print(a(1)). In a(num), the function returns 2. Therefore, b(2) outputs 2. There is nothing to do with num here.
- Error because of an undefined variable
- Try again. The output of b(2) is print(a(1)). In a(num), the function returns 2. Therefore, b(2) outputs 2. There is nothing to do with num here..

Q-17:

```
WWhat is the output of this code?
def a(num):
num = 4
return 2
def b(val):
num = 8
print(a(1))
b(2)
```

- 1
- Try again. The output of b(2) is print(a(1)). In a(num), the function returns 2. Therefore, b(2) outputs 2. val and num do not play a role in the output of b(2).
- 2
- Correct! The output of b(2) is print(a(1)). In a(num), the function returns 2. Therefore, b(2) outputs 2. .
- 4
- Try again. The output of b(2) is print(a(1)). In a(num), the function returns 2. Therefore, b(2) outputs 2. val and num do not play a role in the output of b(2).
- 8
- Try again. The output of b(2) is print(a(1)). In a(num), the function returns 2. Therefore, b(2) outputs 2. val and num do not play a role in the output of b(2).
- Error because of an undefined variable

Q-18:

```
What is the output of this code?
def a(num):
num = 4
return 2
def b(val):
val = 8
print(a(1))
b(2)
```

You have attempted of activities on this page