Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings Prev Up Next Scratch ActiveCode Profile
title here
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 5.35 Functions with Loops Multiple Choice Questions
Activity 5.35.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]))
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.
Activity 5.35.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))
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.
Activity 5.35.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"
Activity 5.35.4 .
How many times would
"Hello world!" print?
for i in range(4):
print("Hello world!")
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.
Activity 5.35.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)
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!
Activity 5.35.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)
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.
Activity 5.35.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)
[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.
Activity 5.35.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]
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.
Activity 5.35.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.
[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.
Activity 5.35.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)
[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.
You have attempted
of
activities on this page.