← return to practice.dsc10.com
This quiz was administered in-person. It was closed-book and
closed-note; students were not allowed to use the DSC
10 Reference Sheet. Students had 20 minutes to work on
the quiz.
This quiz covered Lectures 1-4 of the Fall 2024 offering of
DSC 10.
⚠️ PDF version available here .
What does the following expression evaluate to? Write your answer exactly how the output would appear in Python.
5 * (4 ** 3 - 40) / (2 * 8) ** .5 + 10 / 3 + 2 * (5 / 6 - 1 / 2)
🎥 A solution walkthrough video is available for this problem.
Answer: 34.0
Follow the order of operations, working inside parentheses first.
4 ** 3 - 40 is 64 - 40 = 24, and
5 * 24 is 120.(2 * 8) ** .5 is the square root of 16,
which is 4.0. Raising to the power .5 always
gives a float.120 / 4.0 = 30.0.10 / 3 is 3.333....5 / 6 - 1 / 2 is 0.333..., so
2 * (5 / 6 - 1 / 2) is 0.666....Adding everything gives
30.0 + 3.333... + 0.666... = 34.0.
The answer is 34.0 and not 34 because
/ always produces a float. Once a float is involved, the
result of the whole expression is a float.
The average score on this problem was 61%.
Select all the answer choices below that correspond to valid assignment statements. For the selected choice(s) only, write the value of the variable.
"seal" = abs(-7) + round(23.442, 1)
whale = max(round(17.82), 5, -20)
shark = abs(max(4, 3, min(10, 5.5, 11)), 100, 30)
squid = "error" + "?" * 4
🎥 A solution walkthrough video is available for this problem.
Answer:
18"error????"Option 1 is incorrect because the left side of an assignment
statement must be a variable name. "seal" is a string, and
Python can’t assign a value to a string, so this line errors.
Option 2 is correct. round(17.82) is 18, so
max(18, 5, -20) is 18.
Option 3 is incorrect. min(10, 5.5, 11) is
5.5 and max(4, 3, 5.5) is 5.5,
but abs takes only one argument. Calling
abs(5.5, 100, 30) errors.
Option 4 is correct. Multiplying a string by an integer repeats the
string, so "?" * 4 is "????". Adding strings
concatenates them, giving "error????".
The average score on this problem was 78%.
Suppose that weights is an array containing the weights,
in kilograms, of several leopard sharks living in La Jolla Cove. Several
leopard sharks represented in weights weigh more than 10
kilograms.
Suppose that we have imported a module called sharkpy by
running the code import sharkpy. The sharkpy
module includes a function heavy that takes as input an
array of shark weights and returns a smaller array containing only the
weights that are above 10 kilograms.
Using the heavy function and an array method of
your choice, write an expression that evaluates to the weight,
in kilograms, of the lightest leopard shark in weights that
weighs more than 10 kilograms.
🎥 A solution walkthrough video is available for this problem.
Answer:
sharkpy.heavy(weights).min()
heavy is part of the sharkpy module, so we
call it with dot notation as sharkpy.heavy(weights).
Calling heavy(weights) on its own would error, because we
ran import sharkpy, not
from sharkpy import heavy.
sharkpy.heavy(weights) returns an array of only the weights
above 10 kilograms. The lightest of those sharks has the smallest value
in that array, which the array method .min() gives us.
A common mistake is weights.min(), which finds the
lightest shark overall. That shark might weigh less than 10 kilograms.
Using .max() would give the heaviest shark instead of the
lightest.
The average score on this problem was 59%.
Suppose we run the following code.
creatures = "crab, eel, clam"
oceans = "Pacific, Atlantic"
creatures = creatures.title()
oceans = oceans.upper()
creatures = creatures.replace("clam", "lobster")
What does the expression creatures + ", " + oceans
evaluate to? Write your answer exactly how the output
would appear in Python.
🎥 A solution walkthrough video is available for this problem.
Answer:
"Crab, Eel, Clam, PACIFIC, ATLANTIC"
.title() capitalizes the first letter of each word, so
creatures becomes "Crab, Eel, Clam".
.upper() makes every letter uppercase, so
oceans becomes "PACIFIC, ATLANTIC".
The .replace("clam", "lobster") line changes nothing.
String methods are case-sensitive, and after .title() the
string contains "Clam" with a capital C, not
"clam". Since .replace finds no match, it
returns the string unchanged. “lobster” never appears in the answer.
Finally, creatures + ", " + oceans joins the two strings
with ", " between them, giving
"Crab, Eel, Clam, PACIFIC, ATLANTIC". The quotation marks
are part of the answer because Python displays strings in quotes.
The average score on this problem was 66%.
Define narwhal and jellyfish as
follows.
narwhal = np.arange(6, 12, 3)
jellyfish = np.arange(12, 6, -3)
True or False: narwhal.sum() and
jellyfish.sum() evaluate to the same value.
True
False
Answer: False
np.arange(start, stop, step) starts at
start, moves by step, and stops
before it reaches stop.
narwhal = np.arange(6, 12, 3) is
array([6, 9]). 12 is not included, so
narwhal.sum() is 15.jellyfish = np.arange(12, 6, -3) counts down from 12
and stops before 6, giving array([12, 9]). So
jellyfish.sum() is 21.Since 15 and 21 are different, the answer
is False.
The average score on this problem was 86%.
True or False: (narwhal+jellyfish)[0] and
(narwhal+jellyfish)[1] evaluate to the same value.
True
False
Answer: True
Arithmetic on two arrays of the same length works element-wise.
narwhal + jellyfish is array([6 + 12, 9 + 9]),
which is array([18, 18]). Both elements are
18, so (narwhal+jellyfish)[0] and
(narwhal+jellyfish)[1] evaluate to the same value.
The average score on this problem was 92%.
🎥 A solution walkthrough video is available for this problem.
The DataFrame finding_nemo contains information about
characters in the movie Finding Nemo. Each row represents a
character, and the columns include:
"Name" (str): the unique name of the character (ex.
"Nemo", "Dory")
"Species" (str): the species of the character (ex.
"clownfish", "blue tang")
There is one character in finding_nemo named
"Bruce". Which of the following lines of code evaluates to
Bruce’s species?
finding_nemo.get("Species").loc["Bruce"]
finding_nemo.get("Species").iloc["Bruce"]
finding_nemo.get("Species").loc["Bruce"].iloc[0]
finding_nemo.set_index("Name").get("Species").loc["Bruce"]
finding_nemo.set_index("Name").get("Species").iloc["Bruce"]
finding_nemo.set_index("Name").get("Species").loc["Bruce"].iloc[0]
🎥 A solution walkthrough video is available for this problem.
Answer:
finding_nemo.set_index("Name").get("Species").loc["Bruce"]
To look up a row by a character’s name with .loc, the
names must be in the index. finding_nemo has the default
index 0, 1, 2, ..., so we first use
.set_index("Name"). Then .get("Species") gives
a Series of species indexed by name, and .loc["Bruce"]
returns the value with the label "Bruce". Each name is
unique, so this is a single string: Bruce’s species.
The other options are incorrect:
finding_nemo.get("Species").loc["Bruce"] errors,
because "Bruce" is not in the default index.finding_nemo.get("Species").loc["Bruce"].iloc[0] errors
for the same reason..iloc["Bruce"] error, because
.iloc takes an integer position, not a label.finding_nemo.set_index("Name").get("Species").loc["Bruce"].iloc[0]
errors because .loc["Bruce"] already returns a string, and
strings don’t have .iloc.
The average score on this problem was 80%.