Spring 2024 Quiz 1

← 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 Spring 2024 offering of DSC 10.

⚠️ PDF version available here .


Problem 1

Lecture 2


Problem 1.1

True or False: int(3.4) and round(3.4) evaluate to the same value.

Answer: True

int drops everything after the decimal point, so int(3.4) is 3. round goes to the nearest integer. Since 3.4 is closer to 3 than to 4, round(3.4) is also 3.


Difficulty: ⭐️⭐️

The average score on this problem was 89%.


Problem 1.2

True or False: For any float x, int(x) and round(x) evaluate to the same value.

Answer: False

int always drops the decimal part, but round can round up. For example, int(3.7) is 3, but round(3.7) is 4.


Difficulty: ⭐️

The average score on this problem was 95%.



Problem 2

Lecture 3

Consider the following code.

iris = 3 / 1
poppy = 8 - 6
daisy = np.array([8, 1, 5])
lily = np.array([4, 2])
poppy = iris ** iris - iris * poppy


Problem 2.1

What is the value of poppy after this code is executed?

Answer: 21.0

iris is 3.0, because / always gives a float. poppy is 2.

The last line uses these values: 3.0 ** 3.0 - 3.0 * 2.

The exponent comes first: 27.0. Then the multiplication: 6.0. Then the subtraction: 27.0 - 6.0 = 21.0.

The answer is a float because iris is a float.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 54%.


Problem 2.2

What is the result of the expression daisy + lily?

Answer: Option D

Adding two arrays adds them position by position: first plus first, second plus second, and so on. daisy has 3 elements and lily has only 2, so the third element has nothing to pair with, and Python raises an error. Option A is wrong because + never joins two arrays together.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 68%.


Problem 2.3

What is the result of the expression daisy + lily[0]?

Answer: Option B

lily[0] is 4, a single number. When you add a number to an array, it is added to every element: [8 + 4, 1 + 4, 5 + 4].


Difficulty: ⭐️⭐️

The average score on this problem was 79%.



Problem 3

Lecture 1

You are tracking the growth of a flower stem over a seven-day period. The flower stem starts out at 24.5 cm and ends up at 29.7 cm.

Write one line of code that calculates the average daily growth, in centimeters, and assigns the result to the variable avg_growth. Do not round your answer.

Answer: avg_growth = (29.7 - 24.5) / 7

The total growth is 29.7 - 24.5, which is 5.2 cm. To get the growth per day, divide by the 7 days. The parentheses are needed because division happens before subtraction. Without them, Python would compute 29.7 - (24.5 / 7).


Difficulty: ⭐️⭐️

The average score on this problem was 84%.


Problem 4

Lecture 4

Suppose flower_data is a DataFrame with information on different species of flowers, where:


Problem 4.1

One of these three columns is a good choice to use as the index of this DataFrame. Write a line of code that sets this column as the index of flower_data, and assigns the resulting DataFrame to the variable flowers.

Answer: flowers = flower_data.set_index("species")

A good index has a different value in every row. The problem says each "species" value is unique. Two species could have the same number of petals or the same length, so those columns are poor choices.


Difficulty: ⭐️⭐️

The average score on this problem was 79%.


Important: The following questions will use flowers instead of flower_data.


Problem 4.2

Which of the following expressions evaluates to a DataFrame that is sorted by "petals" in descending order?

Answer: Option B

sort_values(by="petals") sorts the whole DataFrame by the "petals" column, and ascending=False puts the largest values first. Option A sorts in ascending order. Options C and D first use .get("petals"), so they give back a Series, not a DataFrame.


Difficulty: ⭐️⭐️

The average score on this problem was 83%.


Problem 4.3

Suppose that the 4th row of flowers corresponds to a rare species of flower named "fire lily". Fill in the blanks below so that both of these expressions evaluate to the stem length in inches of "fire lily".

i. flowers.get("length").loc[__(x)__]
ii. flowers.get("length").iloc[__(y)__]

Answer: (x): "fire lily", (y): 3

.loc looks up a row by its row label. Since the index is now the species names, the row label is "fire lily". .iloc looks up a row by its position, and positions start at 0, so the 4th row is at position 3.


Difficulty: ⭐️⭐️

The average score on this problem was 83%.


Problem 4.4

Suppose that the 3rd row of flowers corresponds to the species "stinking corpse lily". Using the flowers DataFrame and the string method .split(), write an expression that evaluates to "corpse".

Answer: flowers.index[2].split(" ")[1]

flowers.index[2] gives the 3rd row label, which is "stinking corpse lily". .split(" ") breaks it at the spaces into ["stinking", "corpse", "lily"]. Then [1] picks the second element, "corpse".


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 46%.



👋 Feedback: Find an error? Still confused? Have a suggestion? Let us know here.