Skip to main content
Contents Index
Search Book
Search Results:
No results.
Read aloud
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 3.5 Lists Multiple-Choice Exercises
Some of the exercises below were adapted from the Python for Everybody Interactive ebook on Runestone by Dr. Barb Ericson.
Activity 3.5.1 .
What is the printed when the following code runs?
alist = [3, 67, "cat", 3.14, False]
print( len(alist) )
4
Incorrect! The built in function, len, returns the actual number of items in the list, not the maximum index value.
5
Correct! There are 5 items in this list.
False
Incorrect! The built in function, len, returns the actual number of items in the list.
3.14
Incorrect! The built in function, len, returns the actual number of items in the list.
Activity 3.5.2 .
What is the printed when the following code runs?
alist = [2, 67, "cat", 3.14, False]
print( alist[2] )
67
Incorrect! Remember that the list indexing starts at 0.
"cat"
Correct! The item at index 2 in this list is "cat" because the list is indexed starting at 0.
2
Incorrect! 2 is the item at index 0, but not the item at index 2.
alist[2]
Incorrect! The print would evaluate alist[2] and print the item in alist at index 2.
Activity 3.5.3 .
What is the printed when the following code runs?
alist = [2, 67, "cat", 3.14, False]
alist[1] = "dog"
alist[3] = 99
print( alist )
["dog", 67, 99, 3.14, False]
Incorrect, remember that the list is indexed starting at 0.
[2, "dog", "cat", 99, False]
Correct! List items 1 and 3 have been updated to "dog" and 99.
[2, "dog", 67, 99, "cat", 3.14, False]
Incorrect, the items at index 1 and 3 should be replaced, not inserted. The length of the list should not change.
["dog", 2, 99, 67, "cat", 3.14, False]
Incorrect, the items at index 1 and 3 should be replaced, not inserted. The length of the list should not change. And remember that the list is indexed starting at 0.
Activity 3.5.4 .
What is the printed when the following code runs?
alist = [2, 67, "cat", 3.14, False]
alist.insert(1, "dog")
alist.insert(3, 99)
print( alist )
["dog", 67, 99, 3.14, False]
Incorrect, remember that the list is indexed starting at 0.
[2, "dog", "cat", 99, False]
Incorrect! Insert adds an additional item at the specified index, but it doesnβt replace the item at that index. The list will get larger.
[2, "dog", 67, 99, "cat", 3.14, False]
Correct, the items have been inserted at index 1 and 3. The length of the list has increased.
["dog", 2, 99, 67, "cat", 3.14, False]
Incorrect, remember that the list is indexed starting at 0.
Activity 3.5.5 .
Which function is used to add a variable a to the end of a list alist?
alist.insert(a)
While you can use insert, you would need to specify an index for where to insert the item.
alist.pop(a)
pop removes an item from a list
alist.append(a)
Yes, append adds an item to the end of a list.
alist.remove(a)
remove deletes the first occurrence of any item that it is told. It does not add an item.
Activity 3.5.6 .
What is printed by the following program?
alist = [4, 2, 8, 6, 5]
blist = [ ]
for item in alist:
blist.append(item + 5)
print(blist)
[4, 2, 8, 6, 5]
5 is added to each item before the append is performed.
[4, 2, 8, 6, 5, 5]
There are too many items in this list. Only 5 append operations are performed.
[9, 7, 13, 11, 10]
Yes, the for loop processes each item of the list. 5 is added before it is appended to blist.
Error, you cannot concatenate inside an append.
5 is added to each item before the append is performed.
Activity 3.5.7 .
Which of the following code segments shows the correct way to accumulate the total sum of a list of numbers?
nums = [4, 5, 2, 93, 3, 5]
s = 0
for n in nums:
s = s + 1
print(s)
nums = [4, 5, 2, 93, 3, 5]
s = 0
for n in nums:
s = n + n
print(s)
nums = [4, 5, 2, 93, 3, 5]
s = 0
for n in nums:
s = s + n
print(s)
Pattern I
This pattern will only count how many items are in the list, not provide the total accumulated value.
Pattern II
This would reset the value of s each time the for loop iterated, and so by the end s would be assigned the value of the last item in the list plus the last item in the list.
Pattern III
Yes, this will solve the problem.
None of the above would be appropriate for the problem.
One of the patterns above is a correct way to solve the problem.
Activity 3.5.8 .
What will the following code print?
num_list = [5, 10, 15, 20]
out = []
for num in num_list:
if num > 10:
out.append(num)
print(out)
[10, 15, 20]
It only appends numbers that are greater than 10
[20, 15]
This would be true if append added at the front, but it appends at the end
[15, 20]
Yes, it appends all numbers greater than 10 in order.
[20, 15, 10]
Append adds items to the end of the list.
Activity 3.5.9 .
What will the following AP CSP Pseudocode print?
alist = [3, 0, 2, 0, 0]
index = 0
while (index < length(alist))
{
num = alist[index]
if (num == 0)
{
alist.remove(index)
}
index = index + 1
}
print(alist)
[3, 0, 2, 0, 0]
The list is modified by the pop.
[3, 0, 2]
It will remove 0βs except for the last one.
[3, 2]
This would be true if it didnβt always increment the index.
[3, 2, 0]
Since it always increments the index it will miss the last zero.
You have attempted
of
activities on this page.