5.34. Functions with Loops Multiple Choice Questions¶
- 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.
Q-1: 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]))
- 50
- Try again! This function will divide the number that is passed in by two until it reaches one.
- 25
- Try again! This function will divide the number that is passed in by two until it reaches one.
- 5
- Try again! This function will divide the number that is passed in by two until it reaches one.
- 6
- Correct!
- 2
- Try again! This function will divide the number that is passed in by two until it reaches one.
Q-2: After how many iterations will this code execute the break
?
def divide_by_two_until_one(num):
count = 0
while (True):
num = num/2
count = count + 1
if (num <= 1):
break
return count
print(divide_by_two_until_one(50))
- 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".
Q-3: 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"
- 3
- Try again! The in range function has an inclusive end value.
- 4
- Correct!
- 0
- Try again! The in range function has an inclusive end value.
- 5
- Try again! The in range function has an inclusive end value.
Q-4: How many times would "Hello world!"
print?
for i in range(4):
print("Hello world!")
- It prints 19 times and it skip counts by the current value in lst2.
- Try again! For loops include the last element.
- It prints 20 times and it skip counts by the current value in lst1.
- Try again! This code counts by the second list.
- It prints 19 times and it skip counts by the current value in lst1.
- Try again! For loops include the last element and count by the second list.
- It prints 20 times and it skip counts by the current value in lst2.
- Correct!
Q-5: How many times does the following code print and in what pattern?
lst1 = [1, 3, 5, 7]
lst2 = [2, 4, 6, 8, 10]
for x in lst1:
for y in lst2:
print(x * y)
- i: 0 j: 0, i: 1 j: 1,
- The inner loop will loop twice for each value of i.
- i: 0 j: 0, i: 1 j: 1, i: 2 j: 2,
- The values of i and j will range from 0 to 1.
- i: 1 j: 1, i: 1 j: 2, i: 2: j: 1, i:2: j:2,
- The values of i and j will range from 0 to 1.
- i: 0 j: 0, i: 0 j: 1, i: 1: j: 0, i:1: j:1,
- Correct! The values of i and j range from 0 to 1 and the inner loop executes twice each time i changes.
Q-6: What does the following code print?
out = ""
for i in range(2):
for j in range(2):
out += "i: " + str(i) + " j:" + str(j) + ", "
print(out)
- [4, 6]
- This would be true if there was a single loop using an index to loop though both lists
- [4, 5, 5, 6]
- It adds l1[0] and l2[0], then l1[0] and l2[1], then l1[1] and l2[0], then l1[1] and l2[1].
- [1, 2, 3, 4]
- This would be true if the two loops were one after the other instead of nested and it just added the value in each list
- [13, 14, 23, 24]
- The + adds the numbers together, it does not concatenate them.
Q-7: What does the following code print?
l1 = [1, 2]
l2 = [3, 4]
out = []
for val1 in l1:
for val2 in l2:
out.append(val1 + val2)
print(out)
- [5, 7]
- This would be true if there was a single loop using an index to loop though both lists but only to the length of the smallest.
- [5, 6, 7, 6, 7, 8]
- This would be true if it looped through l2 and then l1 in the inner loop.
- [5, 6, 6, 7, 7, 8]
- For every value in l1 it loops through all the values in l2.
- [14, 15, 24, 25, 34, 35]
- The + adds the numbers together, it does not concatenate them.
Q-8: What does the following code print?
l1 = [1, 2, 3]
l2 = [4, 5]
out = []
for val1 in l1:
for val2 in l2:
out.append(val1 + val2)
print(out)
- [5, 7]
- This would be true if there was a single loop using an index to loop though both lists but only to the length of the smallest.
- [5, 6, 7, 6, 7, 8]
- For every value in l2 it loops through all the values in l1.
- [5, 6, 6, 7, 7, 8]
- This would be true if looped through l1 and then l2 in the inner loop.
- [41, 42, 43, 51, 52, 53]
- The + adds the numbers together, it does not concatenate them.
Q-9: What does the following code print?
l1 = [1, 2, 3]
l2 = [4, 5]
out = []
for val1 in l2:
for val2 in l1:
out.append(val1 + val2)
print(out)
- [5, 7]
- This would be true if there was a single loop using an index to loop though both lists but only to the length of the smallest and the items were numbers.
- [5, 6, 7, 6, 7, 8]
- This would be true if it looped through l2 and then l1 in the inner loop and the list items were numbers.
- [5, 6, 6, 7, 7, 8]
- This would be true if it looped through l1 and then l2 in the inner loop and the list items were numbers.
- ['14', '15', '24', '25', '34', 35']
- Since the list items are strings the + will concatenate the values.
Q-10: What does the following code print?
l1 = ['1', '2', '3']
l2 = ['4', '5']
out = []
for val1 in l1:
for val2 in l2:
out.append(val1 + val2)
print(out)
You have attempted of activities on this page