← return to practice.dsc10.com


Lecture 3 — Collected Practice Questions

Below are practice problems tagged for Lecture 3 (rendered directly from the original exam/quiz sources).


Source: fa23-quiz1 — Q2

Problem 1

Consider the following four assignment statements.

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


Problem 1.1

What is the value of the expression bass * tuna?

Answer: "55"


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 48%.


Problem 1.2

Which of the following expressions results in an error?

Answer: int(sword[0])


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 51%.


Problem 1.3

Which of the following expressions evaluates to "DSC10"?

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


Difficulty: ⭐️

The average score on this problem was 92%.



Source: fa23-quiz1 — Q3

Problem 2

Consider the following assignment statement.

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


Problem 2.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 2.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

Difficulty: ⭐️⭐️⭐️

The average score on this problem was 74%.



Source: fa24-quiz1 — Q3

Problem 3

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: fa24-quiz1 — Q5

Problem 4

Define narwhal and jellyfish as follows.

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


Problem 4.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 4.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: fa25-quiz1 — Q4

Problem 5

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"


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 44%.


Source: sp24-final — Q8

Problem 6


Problem 6.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 6.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: sp24-quiz1 — Q2

Problem 7

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 7.1

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

Answer: 21.0


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 54%.


Problem 7.2

What is the result of the expression daisy + lily?

Answer: Option D


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 68%.


Problem 7.3

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

Answer: Option B


Difficulty: ⭐️⭐️

The average score on this problem was 79%.



Source: sp25-quiz1 — Q5

Problem 8

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


Difficulty: ⭐️

The average score on this problem was 93%.


Problem 9

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: wi24-quiz1 — Q2

Problem 10

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"])


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 50%.


Source: wi24-quiz1 — Q3

Problem 11

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 11.1

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

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


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 62%.


Problem 11.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]


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 36%.


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


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 48%.



Source: wi25-quiz1 — Q2

Problem 12


Problem 12.1

How many unique data types exist in the clubs DataFrame?

Answer: 4


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 61%.


Problem 12.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"


Difficulty: ⭐️⭐️

The average score on this problem was 75%.



Source: wi25-quiz1 — Q4

Problem 13

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)


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

The average score on this problem was 11%.


Source: wi26-quiz1 — Q1

Problem 14

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