Skip to main content\(
\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 11.11 Multiple Choice Questions
Activity 11.11.1.
11-9-1: Which of the following would complete
val = to set
val to 20 by slicing
aTuple.
aTuple = ("Orange", (10, 20, 30), (5, 15, 25))
val =
-
val = aTuple[1:2][1]
-
Incorrect! Running this code would result in an error. Try again.
-
val = aTuple[2][1]
-
Incorrect! This would access the value 15. Try again.
-
val = aTuple[1:2](1)
-
Incorrect! Running this code would result in an error. Try again.
-
val = aTuple[1][1]
-
Correct! This first goes to the second item in the tuple, then grabs the second item from the list, which is 20.
Activity 11.11.2.
11-9-2: True or False? A python tuple can be created without using any parentheses.
Activity 11.11.3.
11-9-3: What type will be printed when the following code executes?
aTuple = ("Orange")
print type(aTuple)
-
list
-
Incorrect! A list would use []. Try again.
-
tuple
-
Incorrect! To create a tuple with a single item, there must be a comma after the first item. Try again.
-
array
-
Incorrect! Arrays do not exist in Python. Try again.
-
str
-
Correct! To create a tuple with a single item, there must be a comma after the first item.
Activity 11.11.4.
11-9-4: What will the following code return?
def practice(tup):
a, b, c = tup
return a
aTuple = "Yellow", 20, "Red"
practice(aTuple)
-
("Yellow", 20, "Red")
-
Incorrect! Hint: think about how tuple assignment works. Try again.
-
Yellow
-
Correct! Tuple assignment makes the first value of the tuple equal to a.
-
20
-
Incorrect! Hint: think about how tuple assignment works. Try again.
-
Red
-
Incorrect! Hint: think about how tuple assignment works. Try again.
Activity 11.11.5.
11-9-5: What will the following code return?
def tuple_indexing(tup):
tup[1] = 800
return tup
aTuple = (100, 200, 300, 400, 500)
print(tuple_indexing(aTuple))
-
Nothing, it will cause an error.
-
Correct! A tuple is immutable, therefore you cannot change its values.
-
(100, 800, 200, 300, 400, 500)
-
Incorrect! Remember, tuples are immutable. Try again.
-
(800, 100, 200, 300, 400, 500)
-
Incorrect! Remember, tuples are immutable. Try again.
-
(100, 200, 800, 300, 400, 500)
-
Incorrect! Remember, tuples are immutable. Try again.
Activity 11.11.6.
11-9-6: Which of the following options will produce the same output?
tupl1 = (5, 3, 1, 9, 0)
# options i, ii, iii, or iv
i) print(tup1[:-1])
ii) print(tup1[0:5])
iii) print(tupl1[0:4])
iv) print(tupl1[-4:])
-
i, ii
-
Incorrect! ii will cause an error because the highest index in tupl1 is 4. Try again.
-
ii, iv
-
Incorrect! ii will cause an error because the highest index in tupl1 is 4. Try again.
-
i, iv
-
Incorrect! i will output all items besides the last, whereas iv will output all items besides the first. Try again.
-
i, iii
-
Correct! These two options will both output all the items in the tuple except for the last one.
Activity 11.11.7.
11-9-7: What is returned when the following code is run?
def tuple_comparison(tup1, tup2):
return tup1 < tup2
tup1 = (66, 4, 17, 4)
tup2 = (66, 4, 16, 5)
tuple_comarison(tup1, tup2)
-
True
-
Incorrect! When the first numbers in both tuples are equal, the comparison operator will move on to the next pair of numbers. Try again.
-
False
-
Correct! The compiler will look at the first pair of different numbers, then decide whether it is true or false depending on that pair.
Activity 11.11.8.
11-9-8: What is printed when the following code is run?
a = (1, 2, 3, 4)
print(a[1:-1])
-
Error, tuple slicing doesnβt exist
-
Incorrect! Tuple slicing does exist. Try again.
-
[2, 3, 4]
-
Incorrect! The output will be in the form of a tuple. Try again.
-
(2, 3, 4)
-
Incorrect! The value of -1 means to stop after the second to last item. Try again.
-
(2, 3)
-
Correct! This tuple slicing grabs the second item all the way to the second to last item.
Activity 11.11.9.
11-9-9: What is the name of an error caused when a data structure has the wrong type, size, or composition?
-
KeyError
-
Incorrect! A KeyError is raised when you try to access a dictionary with a key that doesnβt yet exist. Try again.
-
TypeError
-
Incorrect! A TypeError is raised when you attempt to call a function or use an operator on something of the incorrect type. Try again.
-
Shape Error
-
Correct! Shape errors are more likely to occur when working with compound data structures.
Activity 11.11.10.
11-9-10: What is printed when the following code is run?
def practice(m):
m = list(m)
x, y = m
return y
m = ('have', 'fun')
print(practice(m))
-
have
-
Incorrect! This would be printed if in line 3 we called print(x) instead. Try again.
-
[βhaveβ, βfunβ]
-
Incorrect! Hint: think about how tuple assignment works and apply that knowledge to lists. Try again.
-
fun
-
Correct! y is associated with the last value of tuple m.
-
(βhaveβ, βfunβ)
-
Incorrect! This is the original tuple, but the function returns the second element of the tuple.
Activity 11.11.11.
11-9-11: What is printed when the following code is run?
tup = ('30', '3', '2', '8')
print(sorted(tup))
-
[β2β, β3β, β30β, β8β]
-
This returns a list with the strings sorted in ascending order.
-
[β2β, β3β, β8β, β30β]
-
Incorrect, this would be the right order for numbers, but not for strings.
-
[β30β, β8β, β3β, β2β]
-
This would be correct if it was sorting numbers in descending order.
-
[β8β, β30β, β3β, β2β]
-
This would be correct if it was sorting in descending order.
Activity 11.11.12.
11-9-12: What is printed when the following code is run?
tup = ('30', '3', '2', '8')
print(sorted(tup), reverse = True)
-
[β2β, β3β, β30β, β8β]
-
This would be true if it sorted the strings in ascending order.
-
[β2β, β3β, β8β, β30β]
-
This would be correct if if was sorting numbers in ascending order.
-
[β30β, β8β, β3β, β2β]
-
This would be correct if it was sorting numbers in descending order.
-
[β8β, β30β, β3β, β2β]
-
It sorts the strings in descending order.
Activity 11.11.13.
11-9-13: What is printed when the following code is run?
tup = (30, 3, 2, 8)
print(sorted(tup), reverse = True)
-
[2, 3, 30, 8]
-
This would be true if the values were strings and it sorted in ascending order.
-
[2, 3, 8, 30]
-
This would be correct if the values were sorted in ascending order.
-
[30, 8, 3, 2]
-
It sorts the numbers in descending order.
-
[8, 30, 3, 2]
-
This would be correcdt if the values were strings.
You have attempted
of
activities on this page.