← return to practice.dsc10.com


Lecture 3 — Collected Practice Questions

Topics: Strings and arrays.

Below are practice problems tagged for Lecture 3 (rendered directly from the original exam/quiz sources). By default, problems are sorted from easiest to hardest by average score; problems without a score are listed last.


Source: Question 1 from Winter 2021 Final Exam

Problem 1

One way to use np.arange to produce the sequence [2, 6, 10, 14] is np.arange(2, 15, 4). This gives three inputs to np.arange.

Fill in the blanks below to show a different way to produce the same sequence, this time using only one input to np.arange. Each blank below must be filled in with a single number only, and the final result, x*np.arange(y)+z, must produce the sequence [2, 6, 10, 14].

Using x*np.arange(y)+z fill in the missing values:

x = _

y = _

z = _

Answer:

x = 4, y = 4, z = 2

The question states that we are trying to create the sequence [2, 6, 10, 14] by filling in the blanks for the statement x*np.arange(y)+z. If we look at the sequence we are attempting to derive, we can see that each step in the sequence increments by 4. Similarly, we can see that the sequence begins at 2. We know that passing an argument by itself in np.arange will increment up to that number (for example np.arange(4) will produce the sequence [0,1,2,3]). Knowing this, we can create this sequence by setting y to 4. Attempting to reach the desired sequence of [2, 6, 10, 14] from [0, 1, 2, 3] we can multiply each number by 4 by setting x to 4 and instantiate the sequence at 2 by setting z as 2.


Difficulty: ⭐️

The average score on this problem was 96%.


Source: Question 5 from Spring 2025 Quiz 1

Problem 2

Below is the opening line from the legendary Minecraft parody song

Revenge:


    line = "Creeper aww man"

You want to create an echo effect to produce the following string:

    "Creeper Creeper Creeper aww man"

Which of the following code snippets produces this output? Select all that apply.

Answer:

Option 1 and Option 3

Solution:

Option 1 is correct because line.split(" ") separates the string into individual words, and (words[0] + " ") * 3 repeats "Creeper " three times before concatenating the remaining words. Option 2 is incorrect because line * 3 repeats concatenation of the entire string three times, producing "Creeper aww manCreeper aww manCreeper aww man". Option 3 is correct because .replace("Creeper", "Creeper Creeper Creeper") replaces the original occurrence of "Creeper" with three repetitions, which is what we want. Option 4 is incorrect because words[0] * 3 + words[1] creates "CreeperCreeperCreeperaww", and " ".join(...) inserts spaces between each character, producing "C r e e p e r C r e e p e r C r e e p e r a w w".


Difficulty: ⭐️

The average score on this problem was 93%.


Source: Question 5 from Fall 2024 Quiz 1

Problem 3

Define narwhal and jellyfish as follows.

narwhal = np.arange(6, 12, 3)
jellyfish = np.arange(12, 6, -3)


Problem 3.1

True or False: narwhal.sum() and jellyfish.sum() evaluate to the same value.

Answer: False


Difficulty: ⭐️⭐️

The average score on this problem was 86%.


Problem 3.2

True or False: (narwhal+jellyfish)[0] and (narwhal+jellyfish)[1] evaluate to the same value.

Answer: True


Difficulty: ⭐️

The average score on this problem was 92%.



Source: Question 1 from Winter 2026 Quiz 1

Problem 4

Your supervisor asks you to create a welcome message for new students. The code below generates this message and stores it in the variable message. In the box, write in the value of the message variable after the code executes. Write clearly!

school = "ucsd"
year = "2026"
message = "hello"
message = school.upper() + " " + year
message = message.replace("SD", " San Diego")

Answer: "UC San Diego 2026"

Trace through each line:

  • Lines 1–3: school = "ucsd", year = "2026", message = "hello".
  • Line 4: message = school.upper() + " " + year. school.upper() is "UCSD". Concatenating gives "UCSD" + " " + "2026" → "UCSD 2026".
  • Line 5: message.replace("SD", " San Diego") replaces the substring "SD" inside "UCSD 2026" with " San Diego" (note the leading space in the replacement string), yielding "UC San Diego 2026".

Difficulty: ⭐️⭐️

The average score on this problem was 86%.


Source: Question 5 from Spring 2026 Quiz 1

Problem 5

Determine the value of best after running the code below.

best = "Ronaldo"
a = "it's almost time for the World Cup!"
a = a.split(" ")[0]
m = "message"
m = m.title().replace("age", "i")
best = best[2] + best[-1] + " " + a + " " + m

Hint: We can access individual characters from a string in the same way we access individual elements from an array. For example, "hello"[4] is "o".

Answer: "no it's Messi"


Difficulty: ⭐️⭐️

The average score on this problem was 84%.


Source: Question 3 from Fall 2023 Quiz 1

Problem 6

Consider the following assignment statement.

puffin = np.array([5, 9, 13, 17, 21])


Problem 6.1

Provide arguments to call np.arange with so that the array penguin is identical to the array puffin.

penguin = np.arange(____)

Answer: We need to provide np.arange with three arguments: 5, anything in (21, 25], 4. For instance, something like penguin = np.arange(5, 25, 4) would work.


Difficulty: ⭐️

The average score on this problem was 90%.


Problem 6.2

Fill in the blanks so that the array parrot is also identical to the array puffin.
Hint: Start by choosing y so that parrot has length 5.

parrot = __(x)__ * np.arange(0, __(y)__, 2) + __(z)__

Answer:

  • x: 2
  • y: anything in (8, 10]
  • z: 5

Let’s work from the inside out. First inside the arange, we need length 5 so anywhere between 8-10 results in 5 entries in the array. Next, we need to multiply by 2 since the difference between elements in the final array should be 4, while the arange difference is 2. Finally, add 5 to make the first element from 0 to 5, and the other elements follow suit.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 74%.



Source: Question 4 from Spring 2026 Quiz 1

Problem 7

For each part below, determine the data type of the result, or state that the expression errors. If you can determine the exact value of the result, give it.


Problem 7.1

(np.arange(5) + "5")[-1]

Answer: error


Difficulty: ⭐️⭐️

The average score on this problem was 88%.


Problem 7.2

np.sum(np.arange(1, 9, 3) * 3)

Answer: int

Value: 36


Difficulty: ⭐️⭐️

The average score on this problem was 78%.


Problem 7.3

(wc.shape[1] / wc.get("score").iloc[2])

Answer: float

Value: 5.0


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 72%.


Problem 7.4

(wc.set_index("country").get("country").iloc[0])

Answer: error


Difficulty: ⭐️⭐️

The average score on this problem was 78%.



Source: Question 3 from Winter 2026 Final Exam

Problem 8

For this problem, let weight and age be defined by:

weight = np.array(zoo.get('weight_lb'))
age = np.array(zoo.get('age'))


Problem 8.1

(a) Avi wants to quickly estimate each animal’s weight in kilograms instead of pounds using the shortcut ( ).

Which of the following expressions output an array of the animal weights in kilograms using this shortcut? Select all that apply.

Answer: A and C

  • A: Multiplies pounds by 0.45 directly.
  • C: weight * 0.90 / 2 equals 0.45 * weight.
  • B subtracts a scalar (wrong).
  • D divides instead of multiplying.

Difficulty: ⭐️⭐️

The average score on this problem was 82%.


Problem 8.2

(b) Michelle wants to find the range of age, i.e., the age of the oldest zoo animal minus the age of the youngest zoo animal.

Which of the following expressions incorrectly computes this value?

Answer: B

age - age.max() is always non-positive, so .min() gives the negative of the range, not the range.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 74%.



Source: Question 8 from Spring 2024 Final Exam

Problem 9


Problem 9.1

The management of the Solazzo apartment complex is changing the comple’s name to be the output of the following line of code. Write the new name of this complex as a string.

Note that the string method .capitalize() converts the first character of a string to uppercase.

("Solazzo".replace("z", "ala" * 2)
          .split("aa")[-1]
          .capitalize()
          .replace("o", "Jo"))

Answer: “LaJo”

Let’s trace the steps:

We start with the original string: “Solazzo”.

"Solazzo".replace("z", "ala" * 2)
Replace every instance of “z” with “alaala” since “ala” * 2 = “alaala”: “Solaalaalaalaalao”

"Solaalaalaalaalao".split("aa")
Split the string by “aa”: [“Sol”, “l”, “l”, “l”, “lao”]

["Sol", "l", "l", "l", "lao"][-1]
Get the last element of the list: “lao”

"lao".capitalize()
Uppercase the first character of the string: “Lao”

"Lao".replace("o", "Jo")
Replace every instance of “o” with “Jo”: “LaJo”


Difficulty: ⭐️⭐️

The average score on this problem was 76%.


Problem 9.2

The management fo the Renaissance apartment complex has decided to follow suit and rename their complex to be the output of the following line of code. Write the new name of this complex as a string.

(("Renaissance".split("n")[1] + "e") * 2).replace("a", "M")

Answer: “MissMeMissMe”

Let’s trace the steps:

We start with the original string: “Renaissance”.

"Renaissance".split("n")
Split the string by “n”: [“Re”, “aissa”, “ce”]

["Re", "aissa", "ce"][1]
Get the element in the 1st index of the list (the second element in the list): “aissa”

"aissa" + e
Add an “e” to the end of the string: “aissae”

("aissae") * 2
Repeat the string twice: “aissaeaissae”

"aissaeaissae".replace("a", "M")
Replace every instance of “a” with “M”: “MissMeMissMe”


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 70%.



Source: Question 2 from Winter 2025 Quiz 1

Problem 10


Problem 10.1

How many unique data types exist in the clubs DataFrame?

Answer: 4

Look at the kind of value stored in each column:

  • "Name" and "Category" hold text: str
  • "Members" and "Budget" hold whole numbers: int
  • "Rating" is a decimal out of 5.0: float
  • "Active" is True/False: bool

That’s 4 unique types: str, int, float, and bool.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 61%.


Problem 10.2

Suppose we run the following code.

clubs_and_ratings = ["Cheese Club".upper(), str(int(4.6)), "Painting Club".title(), 3.7 - 1]
a = np.array(clubs_and_ratings)

What do a[0], a[1], a[2], and a[3] evaluate to?

Answer:

a[0]: "CHEESE CLUB"

a[1]: "4"

a[2]: "Painting Club"

a[3]: "2.7"

First, evaluate each element of the list:

  • "Cheese Club".upper() → "CHEESE CLUB"
  • int(4.6) truncates to 4 (it doesn’t round), and str(4) → "4"
  • "Painting Club".title() capitalizes the first letter of each word, which it already does → "Painting Club"
  • 3.7 - 1 → 2.7, a float

The key step is np.array(...). Every element of an array must have the same type, so when a list mixes strings and numbers, NumPy converts everything to strings. That’s why a[3] is the string "2.7", not the float 2.7.


Difficulty: ⭐️⭐️

The average score on this problem was 75%.



Source: Question 2 from Spring 2024 Quiz 1

Problem 11

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 11.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 11.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 11.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%.



Source: Question 2 from Fall 2023 Quiz 1

Problem 12

Consider the following four assignment statements.

bass = "5"
tuna = 2
sword = ["4.0", 5, 12.5, -10, "2023"]
gold = [4, "6", "CSE", "doc"]


Problem 12.1

What is the value of the expression bass * tuna?

Answer: "55"

Multiplying a string produces a larger string containing that many copies of the original string.


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 48%.


Problem 12.2

Which of the following expressions results in an error?

Answer: int(sword[0])

“44.0” isn’t directly convertible to an int (it would be a float) so the conversion fails.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 51%.


Problem 12.3

Which of the following expressions evaluates to "DSC10"?

Answer: gold[3].replace("o", "s").upper() + str(gold[0] + int(gold[1]))

We would want upper to produce “DSC” instead of title which would product “Dsc”, and gold[0] + int(gold[1]) is the only expression that would produce 10, which we then convert to a string.


Difficulty: ⭐️

The average score on this problem was 92%.



Source: Question 3 from Fall 2024 Quiz 1

Problem 13

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.

Answer: sharkpy.heavy(weights).min()


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 59%.


Source: Question 3 from Fall 2026 Quiz 1

Problem 14

Ella and Bianca, a duo of world renowned pizza-turned-sushi chefs, are interviewing jointly for a job at Coleman’s Catch. Help them ace their interview question by determining the output of the code block below. Write legibly!

food1 = "hamachi"
food2 = "sashimi"
food2.replace("sh", "iba").replace("s", "b") + "o".join(food1.split("a"))

Answer: "baibaimihomochi"

The expression is two pieces joined with +, so evaluate each piece separately.

Left piece: food2.replace(“sh”, “iba”).replace(“s”, “b”)

“sashimi”.replace(“sh”, “iba”) swaps the “sh” in the middle, giving “saibaimi”. .replace(“s”, “b”) then swaps every remaining “s”. The only one left is the first letter, giving “baibaimi”.

Right piece: “o”.join(food1.split(“a”))

“hamachi”.split(“a”) cuts at each “a”, giving [“h”, “m”, “chi”]. “o”.join(…) puts an “o” between the pieces, giving “homochi”.

+ on strings concatenates, so the result is “baibaimi” + “homochi”, which is “baibaimihomochi”


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 58%.


Source: Question 10 from Winter 2026 Midterm Exam

Problem 15

Pizza by Peter awards loyalty points: every third slice you purchase from them earns 2^i points, starting from the 1st slice. For example, the 1st, 4th, 7th and 10th slices that a customer purchases each earn 2^i loyalty points where i is 1, 4, 7 and 10 respectively.

Without using the + operator, write a one-line expression that evaluates to the total loyalty points from those four slices.

Answer: (2**(np.arange(1, 12, 3))).sum()

The 1st, 4th, 7th, and 10th slices earn 2^1, 2^4, 2^7, and 2^{10} points. Exponents 1, 4, 7, 10 are every third integer starting at 1: np.arange(1, 12, 3).

Explanation:

np.arange(1, 12, 3) gives [1, 4, 7, 10]; 2** raises 2 to each power; .sum() adds them (without using binary + in your source).

Alternatives: There are several different possible alternatives; you could use np.sum(), sum, or .sum(), end the np.arange at different places, specify exact ranges without np.arange, etc. For example, np.sum(np.array([2**1, 2**4, 2**7, 2**10])), sum(2 ** np.array(1, 4, 7, 10)), or other functional alternatives.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 58%.


Source: Question 2 from Winter 2024 Quiz 1

Problem 16

Consider the assignment statement below.

pear = [6, 10.1, "pear", 13]

What does the expression np.array(pear) evaluate to?

Answer: array(["6", "10.1", "pear", "13"])

Every element of an array must have the same type. When np.array() receives a list with mixed types, (if possible) it converts all of the elements to a single type that can represent every one of them. A number can always be written as a string, but not every string can be written as a number, so every element in this array is converted to a string.

Option 1: array([6, 10.1, "pear", 13]). Incorrect. This keeps the original types of the elements, so the array would hold numbers and strings together.

Option 2: array([6, 10.1, pear, 13]). Incorrect. Without quotes, pear refers to the variable (the whole list), not the string "pear", so this would put a list inside the array alongside numbers.

Option 3: array(["6", "10.1", "pear", "13"]). Correct. All four elements become strings, so the array has a single type.

Option 4: array(["pear"]). Incorrect. np.array() doesn’t throw away elements whose type doesn’t match. All four elements are kept.

Option 5: array([pear]). Incorrect. This would be an array containing the variable pear itself, rather than the elements of the list. This nests the list pear within another list, ultimately resulting in array([['6', '10.1', 'pear', '13']]).

Option 6: This expression errors. Incorrect. NumPy converts every element to a common type, which in this problem is a string.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 50%.


Source: Question 3 from Winter 2024 Quiz 1

Problem 17

Suppose x and y are both ints that have been previously defined, with x < y. Now, define:

peach = np.arange(x, y, 2)

Say that the spread of peach is the difference between the largest and smallest values in peach. The spread should be a non-negative integer.


Problem 17.1

Using array methods, write an expression that evaluates to the spread of peach.

Answer: peach.max() - peach.min()

The spread is the largest value minus the smallest value. Arrays have the methods .max() and .min(), which return the largest and smallest elements of the array.

  • Order matters. The largest value has to come first.
  • Methods vs. functions. max(peach) and np.max(peach) also find the largest value, but they are functions (note the array goes inside the parentheses). A method is called on the array with a dot, like peach.max().
  • Don’t forget the parentheses. peach.max without () refers to the method itself and doesn’t call it, so peach.max - peach.min errors.

Difficulty: ⭐️⭐️⭐️

The average score on this problem was 62%.


Problem 17.2

Without using any methods or functions, write an expression that evaluates to the spread of peach.
Hint: Use [ ].

Answer: peach[len(peach) - 1] - peach[0] or peach[-1] - peach[0]

The key is that np.arange(x, y, 2) produces values in increasing order. That means:

  • The smallest value is the first element, peach[0].
  • The largest value is the last element, peach[-1]. A negative index counts from the end of the array, so -1 is the last element.

Common mistakes:

  • peach[len(peach)] - peach[0] errors. Indexing starts at 0, so the index of the last element is len(peach) - 1, not len(peach).
  • y - x or peach[y] - peach[x]. y is never in peach because np.arange stops before y. Also, x and y are values, not positions.

Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 36%.


Problem 17.3

Choose the correct way to fill in the blank in this sentence:

The spread of peach is ______ the value of y - x.

Answer: always less than

The smallest value in peach is x. Because np.arange excludes the stop value, the largest value in peach is always strictly less than y. It’s either y - 1 or y - 2, depending on whether y - x is odd or even. So the spread is (largest value) − x, which is strictly less than y - x.

Why the other options are wrong:

  • “sometimes less than and sometimes equal to” / “always equal to”: the spread could only equal y - x if y were in the array. np.arange never includes the stop value, so equality never happens.
  • “always greater than” / “sometimes greater than and sometimes equal to”: every value in peach lies between x and y, and x is the smallest, so the spread can’t exceed y - x.

Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 48%.



Source: Question 1 from Fall 2026 Quiz 1

Problem 18

Coleman purchased a property on Clougherty Drive, but the street number can only be determined by evaluating the following expression. What is the street number?

sum(2 ** np.arange(3, 7, 2)) * len(str(len("sushi" * 999)))

Answer: 160

The np.arange(3, 7, 2) results in an array that starts at 3, steps by 2, and stops before 7, which produces [3, 5].

2 ** np.array([3, 5]) raises 2 to each element, giving [8, 32], and sum of that is 40.

On the other side of the expression, “sushi” * 999 repeats the 5-character string 999 times, so len(…) is just 5 * 999, which equals 4995.

str() wraps around 4995, producing “4995”, and its len is 4.

Finally, we have 40 * 4, which produces an answer of 160.


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 48%.


Source: Question 4 from Fall 2025 Quiz 1

Problem 19

Alongside the unveiling of your new game, you’ll want to announce a new fighter to build hype. The code below calculates the name of this new fighter and stores it in the variable new. In the box, write in the value of the new variable after the code executes. Write clearly!

lu = "luigi"
dk = "dk"
new = "crash"
dk.replace("k", "kong")
new = ((lu + "t") * len(dk)).title()
new = new

Answer: "Luigitluigit"

dk.replace(“k”, “kong”) creates a new string, “dkong”, but the result is never assigned to anything. Strings can’t be changed in place, so dk is still “dk”.

len(dk) is 2, because dk is still “dk”.

lu + “t” is “luigit”.

“luigit” * 2 repeats it, giving “luigitluigit”.

.title() capitalizes the first letter of each word. There are no spaces, so the whole thing counts as one word and only the first letter changes, giving “Luigitluigit”.

new = new does nothing.


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 44%.


Source: Question 4 from Winter 2025 Quiz 1

Problem 20

The Data Science Student Society has a $500 budget, which they will use only to buy food for their 4 meetings this quarter. Each time they have a meeting, they will use exactly half of their remaining budget on food. For example, the first meeting’s food cost will be $250 and the next meeting’s food cost will be $125. Use np.arange(), together with arithmetic operations, to assign food_costs to an array containing the food cost for each of the 4 meetings. Do not use np.array().

food_costs =

Answer: 500 * 0.5 ** np.arange(1,5) or 500/2 ** np.arange(1,5)

Each meeting costs half of the previous one, so the costs are:

500 \cdot 0.5^1,\ 500 \cdot 0.5^2,\ 500 \cdot 0.5^3,\ 500 \cdot 0.5^4 = 250,\ 125,\ 62.5,\ 31.25

The exponents run from 1 to 4, which is exactly np.arange(1, 5). The stop value isn’t included. Arithmetic on an array is applied to every element, so 0.5 ** np.arange(1, 5) gives [0.5, 0.25, 0.125, 0.0625], and multiplying by 500 gives the four costs.

This also depends on precedence: ** runs before * and /. So 500 * 0.5 ** ... raises 0.5 to each power first and then multiplies, which is what we want.


Difficulty: ⭐️⭐️⭐️⭐️⭐️

The average score on this problem was 11%.