Score of 13.75 out of 14.00
Open up IDLE and try out all the code as you do the exercise!
Question 1
Select the expression(s) that evaluate to
True
.Your Answer | Score | Explanation | |
---|---|---|---|
[1, 2, 3] in len('mom')
|
Correct | 0.25 | |
len('mom') in [1, 2, 3]
|
Correct | 0.25 | |
int('3') in [len('a'), len('ab'), len('abc')]
|
Correct | 0.25 | |
'3' in [1, 2, 3]
|
Correct | 0.25 | |
Total | 1.00 / 1.00 |
Question ExplanationEvaluate each of these expressions in
the Python shell and, if the results surprise you, you should also
evaluate each subexpression.
Question 2
Consider this code:
def mystery(s): i = 0 result = '' while not s[i].isdigit(): result = result + s[i] i = i + 1 return resultSelect the function call(s) that result in an error.
Your Answer | Score | Explanation | |
---|---|---|---|
mystery('abc')
|
Correct | 0.25 | |
mystery('abc123')
|
Correct | 0.25 | |
mystery('123')
|
Correct | 0.25 | |
mystery('123abc')
|
Correct | 0.25 | |
Total | 1.00 / 1.00 |
Question ExplanationRun the code. To understand why the error occurs (or does not), trace the code in the visualizer.
Question 3
Consider this code:
def example(L): """ (list) -> list """ i = 0 result = [] while i < len(L): result.append(L[i]) i = i + 3 return resultWhich is the best docstring description for function
example
?Your Answer | Score | Explanation | |
---|---|---|---|
Return a list containing every third item from L starting at index 0. |
Correct | 1.00 | |
Return an empty list . |
|||
Return a list containing every third index from L starting at index 0. |
|||
Return a list containing the items from L starting from index 0, omitting every third item. |
|||
Total | 1.00 / 1.00 |
Question ExplanationRun the code several times, passing various lists as arguments, to gain a better understanding of what it does.
Question 4
def compress_list(L): """ (list of str) -> list of str Return a new list with adjacent pairs of string elements from L concatenated together, starting with indices 0 and 1, 2 and 3, and so on. Precondition: len(L) >= 2 and len(L) % 2 == 0 >>> compress_list(['a', 'b', 'c', 'd']) ['ab', 'cd'] """ compressed_list = [] i = 0 while i < len(L): compressed_list.append(L[i] + L[i + 1]) # MISSING CODE HERE return compressed_listSelect the missing line of code.
Your Answer | Score | Explanation | |
---|---|---|---|
i = i + 2
|
Correct | 1.00 | |
i = i + 1
|
|||
i = i * 2
|
|||
i = i + i
|
|||
Total | 1.00 / 1.00 |
Question ExplanationRun the code to gain a better understanding of what it does and what effect each choice has.
Question 5
What is the sum of the even numbers from 524 through 10508, inclusive? Hint: write a
while
loop to accumulate the sum and print it. Then copy and paste that sum. For maximum learning, do it with a for
loop as well, using range
.
You entered:
Your Answer | Score | Explanation | |
---|---|---|---|
27541388 | Correct | 1.00 | |
Total | 1.00 / 1.00 |
Question ExplanationStart by solving a simpler problem,
such as printing the even integers between 2 and 10. Make sure that you
print 2, 4, 6, 8, and 10. Then introduce a variable, perhaps called
sum
, that starts at 0, and in each iteration adds the current integer to sum
. The sum of the even numbers from 2 through 10, inclusive, is 30. Then reattempt the question.Question 6
Consider this code:
def while_version(L): """ (list of number) -> number """ i = 0 total = 0 while i < len(L) and L[i] % 2 != 0: total = total + L[i] i = i + 1 return totalThe
while
loop stops as soon as an even number is found, and the sum of all the previous numbers is returned.
The four functions below use a for
loop to try to accomplish the same task, although they keep iterating through all of the numbers in L
regardless of whether the numbers are even or odd. Only one of them returns the same value as function while_version
. Which one is it?Your Answer | Score | Explanation | |
---|---|---|---|
def for_version(L):
found_even = False
total = 0
for num in L:
if num % 2 != 0 and not found_even:
total = total + num
else:
found_even = True
return total
|
Correct | 1.00 | |
def for_version(L): found_even = False total = 0 for num in L: if num % 2 != 0: total = total + num elif not found_even: found_even = True return total |
|||
def for_version(L): found_even = False total = 0 for num in L: if num % 2 != 0: total = total + num found_even = True return total |
|||
def for_version(L): found_even = False total = 0 for num in L: if num % 2 != 0: total = total + num found_even = True return total |
|||
Total | 1.00 / 1.00 |
Question ExplanationPay close attention to the variables involved in the loop condition:
i
and L
. Read through the code or trace it in the visualizer to determine what that while
loop does and when it exits. You should also run each of the for_version
functions.Question 7
Consider this code:
>>> numbers = [1, 4, 3] >>> # MISSING CODE HERE >>> print(numbers) [3, 4, 1]Which of the following code fragments(s) could be the missing code in the program above?
Your Answer | Score | Explanation | |
---|---|---|---|
numbers.reverse()
|
Correct | 0.25 | |
reverse(numbers)
|
Correct | 0.25 | |
numbers = reverse(numbers)
|
Correct | 0.25 | |
numbers = numbers.reverse()
|
Correct | 0.25 | |
Total | 1.00 / 1.00 |
Question ExplanationRun the code and try each code fragment.
Question 8
Consider this code:
fruits = ['banana', 'apple', 'pear', 'peach'] fruits.insert(fruits.index('pear'), 'watermelon') print(fruits)What is printed by the code above?
Your Answer | Score | Explanation | |
---|---|---|---|
['banana', 'apple', 'watermelon', 'pear', 'peach']
|
Correct | 1.00 | |
['banana', 'watermelon', 'apple', 'pear', 'peach']
|
|||
['banana', 'apple', 'watermelon', 'peach']
|
|||
['banana', 'apple', 'pear', 'watermelon', 'peach']
|
|||
Total | 1.00 / 1.00 |
Question ExplanationRun the code and read the help for
list.index
and list.insert
.Question 9
Your younger sibling has just
discovered music from the 1970's. They have put together a playlist of
the same 5 songs repeated again and again. Here are the songs:
- ABC by The Jackson 5
- Venus by Shocking Blue
- Lola by the Kinks
- Let It Be by the Beatles
- Cecilia by Simon and Garfunkel
Here is an example of their playlist:
['Lola', 'Venus', 'Lola', 'Lola', 'Let It Be', 'Lola', 'ABC', 'Cecilia', 'Lola', 'Lola']
You want to make sure that Lola gets played at most 3 times, so you want to complete this function that edits the playlist:
def cap_song_repetition(playlist, song): '''(list of str, str) -> NoneType Make sure there are no more than 3 occurrences of song in playlist. '''Select the loop(s) that accomplish this.
Your Answer | Score | Explanation | |
---|---|---|---|
while playlist.count(song) > 3:
playlist.pop(playlist.index(song))
|
Correct | 0.25 | |
while playlist.count(song) > 3:
playlist.remove(song)
|
Correct | 0.25 | |
while playlist.count(song) >= 3: playlist.remove(song) |
Correct | 0.25 | |
while playlist.count(song) > 3: playlist.pop(song) |
Correct | 0.25 | |
Total | 1.00 / 1.00 |
Question ExplanationLook at
help(list.remove)
and help(list.pop)
and see if you can determine which uses are correct.
You can also enter the different function completions into IDLE and test them on an equivalent example:
>>> playlist = ['A', 'B', A', 'A', 'C', 'A', 'D', 'E', 'A', 'A'] >>> cap_song_repetition(playlist, 'A') >>> print(playlist)
Question 10
Consider this code:
>>> a = [1, 2, 3] >>> b = a >>> # MISSING CODE HERE >>> print(a, b) [1, 'A', 3] [1, 'A', 3]Which of the following code fragments(s) could be the missing code in the program above?
Your Answer | Score | Explanation | |
---|---|---|---|
a[1] = 'A'
|
Correct | 0.25 | |
b[-2] = 'A'
|
Correct | 0.25 | |
b[1] = 'AB'
a[1] = a[1][0]
|
Correct | 0.25 | |
a = [1, 'A', 3]
b = [1, 'A', 3]
|
Correct | 0.25 | |
Total | 1.00 / 1.00 |
Question ExplanationRun the code, substituting each code fragment in for the missing code. Note that if a[1] is "CAT", then a[1][0] is "C".
Question 11
Consider this code:
>>> a = [1, 2, 3] >>> b = [1, 2, 3] >>> # MISSING CODE HERE >>> print(a, b) [1, 'A', 3] [1, 'A', 3]Which of the following code fragments(s) could be the missing code in the program above?
Your Answer | Score | Explanation | |
---|---|---|---|
a = [1, 'A', 3]
b = [1, 'A', 3]
|
Correct | 0.25 | |
a[1] = 'A' |
Correct | 0.25 | |
b[-2] = 'A'
|
Inorrect | 0.00 | |
b[1] = 'AB' a[1] = a[1][0] |
Correct | 0.25 | |
Total | 0.75 / 1.00 |
Question ExplanationRun the code, substituting each code fragment in for the missing code. Note that if a[1] is "CAT", then a[1][0] is "C".
Question 12
Consider this code:
def increment_items(L, increment): i = 0 while i < len(L): L[i] = L[i] + increment i = i + 1 values = [1, 2, 3] print(increment_items(values, 2)) print(values)What is printed by the program above?
Your Answer | Score | Explanation | |
---|---|---|---|
None
[3, 4, 5]
|
Correct | 1.00 | |
None [1, 2, 3] |
|||
[3, 4, 5] [1, 2, 3] |
|||
[3, 4, 5] None |
|||
Total | 1.00 / 1.00 |
Question ExplanationRun the code.
Question 13
Select the code fragment(s) that print
[3, 6, 9]
.Your Answer | Score | Explanation | |
---|---|---|---|
values = []
for num in range(3, 10, 3):
values.append(num)
print(values)
|
Correct | 0.25 | |
values = []
for num in range(1, 4):
values.append(num * 3)
print(values)
|
Correct | 0.25 | |
values = [] for num in range(1, 3): values.append(num * 3) print(values) |
Correct | 0.25 | |
values = [] for num in range(3, 9, 3): values.append(num) print(values) |
Correct | 0.25 | |
Total | 1.00 / 1.00 |
Question ExplanationCall
help(range)
to learn about the function range
and its two optional arguments.Question 14
Select the function calls to
range
that, when used to fill in the blank, cause the code to produce the results below.
for num in __________________: print(num)The loop should print this:
3 11 19
Your Answer | Score | Explanation | |
---|---|---|---|
range(3, 20, 8)
|
Correct | 0.25 | |
range(3, 23, 8)
|
Correct | 0.25 | |
range(3, 19, 8)
|
Correct | 0.25 | |
range(3, 8, 20)
|
Correct | 0.25 | |
Total | 1.00 / 1.00 |
Question ExplanationCall
help(range)
to learn about the function range
and it's two optional arguments.