Chapter 3 Quiz: Python Fundamentals I — Variables, Data Types, and Expressions
Instructions: This quiz tests your understanding of Chapter 3. Answer all questions before checking the solutions. For multiple choice, select the best answer. For code prediction questions, write your answer before checking — the learning is in the predicting. Total points: 100.
Section 1: Multiple Choice (8 questions, 4 points each)
Question 1. Which of the following is a valid Python variable name?
- (A)
3rd_place - (B)
third-place - (C)
third_place - (D)
third place
Answer
**Correct: (C)** - **(A)** is invalid because variable names cannot start with a number. - **(B)** is invalid because variable names cannot contain hyphens (dashes). - **(C)** is valid: letters, underscores, and no leading digit. - **(D)** is invalid because variable names cannot contain spaces.Question 2. What is the value of result after this code executes?
result = 17 // 5
- (A)
3.4 - (B)
3 - (C)
4 - (D)
2
Answer
**Correct: (B)** The `//` operator performs floor division, which divides and then rounds *down* to the nearest integer. `17 / 5 = 3.4`, and the floor of 3.4 is `3`. This is different from rounding (which would give 3) and from ceiling (which would give 4).Question 3. What does type("42") return?
- (A)
<class 'int'> - (B)
<class 'float'> - (C)
<class 'str'> - (D)
<class 'bool'>
Answer
**Correct: (C)** The quotes make `"42"` a string, not a number. It doesn't matter that the characters inside look like a number — the quotes determine the type. This is a critical distinction in data science, where data loaded from files often arrives as strings even when it represents numbers.Question 4. What is the output of the following code?
x = 5
y = x
x = 10
print(y)
- (A)
10 - (B)
5 - (C)
NameError - (D)
None
Answer
**Correct: (B)** When `y = x` is executed, `y` is assigned the value that `x` currently points to, which is `5`. When `x` is later reassigned to `10`, this moves the label `x` to a new value — but `y` still points to `5`. Reassigning one variable doesn't affect another variable that was previously assigned the same value. This is the "labels, not boxes" concept.Question 5. Which string method removes leading and trailing whitespace?
- (A)
.clean() - (B)
.trim() - (C)
.strip() - (D)
.remove()
Answer
**Correct: (C)** `.strip()` removes leading and trailing whitespace from a string. Python doesn't have `.clean()`, `.trim()`, or `.remove()` methods for this purpose. (`.remove()` exists for lists, not strings, and `.trim()` is the equivalent in JavaScript, not Python.)Question 6. What does int(3.9) return?
- (A)
4(rounds up) - (B)
3(truncates toward zero) - (C)
3.0(converts to float) - (D)
ValueError
Answer
**Correct: (B)** `int()` truncates toward zero — it drops the decimal part without rounding. `int(3.9)` is `3`, not `4`. Similarly, `int(-3.9)` is `-3`, not `-4`. If you want rounding, use the `round()` function instead.Question 7. What does bool("") return?
- (A)
True - (B)
False - (C)
""(unchanged) - (D)
TypeError
Answer
**Correct: (B)** An empty string is one of Python's "falsy" values. `bool("")` returns `False`. Note the difference from `bool(" ")`, which returns `True` because a string containing a space is not empty — it has one character.Question 8. Which line of code correctly creates an f-string that displays a variable rate as a percentage with two decimal places?
- (A)
print("Rate: {rate:.2f}%") - (B)
print(f"Rate: {rate:.2f}%") - (C)
print(f"Rate: {rate:2}%") - (D)
print(f"Rate: " + rate + "%")
Answer
**Correct: (B)** - **(A)** is missing the `f` prefix, so the curly braces are treated as literal text. - **(B)** correctly uses an f-string with the `:.2f` format specifier (2 decimal places, float format). - **(C)** uses `:2` which specifies field width, not decimal places. - **(D)** would cause a `TypeError` if `rate` is a number, because you can't concatenate a string and a number with `+`.Section 2: True or False (4 questions, 4 points each)
Question 9. True or False: In Python, = and == do the same thing.
Answer
**False.** `=` is the assignment operator (it stores a value: `x = 5`). `==` is the comparison operator (it checks equality: `x == 5` returns `True` or `False`). Confusing them is one of the most common beginner mistakes.Question 10. True or False: The expression 10 / 5 evaluates to the integer 2.
Answer
**False.** In Python, the `/` operator always returns a float. `10 / 5` evaluates to `2.0`, not `2`. If you want an integer result, use floor division: `10 // 5` returns `2`.Question 11. True or False: String methods like .upper() modify the original string.
Answer
**False.** Strings in Python are immutable — they cannot be changed after creation. String methods like `.upper()`, `.lower()`, `.strip()`, and `.replace()` return a *new* string; they do not modify the original. To use the result, you must assign it to a variable: `city = city.upper()`.Question 12. True or False: "Data" == "data" evaluates to True.
Answer
**False.** String comparison in Python is case-sensitive. `"Data"` and `"data"` are different strings because `D` and `d` are different characters. If you want case-insensitive comparison, convert both to the same case first: `"Data".lower() == "data".lower()` returns `True`.Section 3: Short Answer (3 questions, 6 points each)
Question 13. Explain in 2-3 sentences why the "variables are labels, not boxes" mental model matters. When does the distinction become practically important?
Answer
When you assign `b = a`, both names point to the same value — no copy is made. With simple types like numbers and strings, this doesn't cause problems because those types are immutable. But with mutable types like lists (Chapter 5), modifying the value through one label changes what you see through the other label, which can cause surprising bugs if you expected an independent copy. The "labels" model correctly predicts this behavior; the "boxes" model does not.Question 14. A beginner writes int("3.14") and gets a ValueError. Explain why this error occurs and provide the correct way to convert the string "3.14" to the integer 3.
Answer
`int()` can only convert strings that look like integers (e.g., `"42"`, `"-7"`). The string `"3.14"` contains a decimal point, which `int()` doesn't accept directly. To convert it to the integer `3`, go through `float` first: `int(float("3.14"))`. This first converts the string to the float `3.14`, then truncates it to the integer `3`.Question 15. Name three "falsy" values in Python and three "truthy" values. Explain the general rule for what makes something falsy.
Answer
**Falsy values:** `0`, `""` (empty string), `False` (also `0.0`, `None`, and empty collections like `[]`). **Truthy values:** `1`, `"hello"`, `True` (also `-5`, `" "`, `[1, 2]`, or any non-zero, non-empty value). **General rule:** A value is falsy if it represents "nothing" or "zero" — the number zero, an empty string, the boolean False, or the special value None. Everything else is truthy.Section 4: Code Prediction (3 questions, 6 points each)
For each code snippet, predict the exact output. Write your answer before checking.
Question 16. What does this code print?
a = "Hello"
b = "World"
c = a + " " + b
print(c)
print(len(c))
Answer
Hello World
11
`a + " " + b` concatenates the strings with a space in between, producing `"Hello World"`. `len("Hello World")` counts all characters, including the space, giving `11`.
Question 17. What does this code print?
x = 10
y = 3
print(x / y)
print(x // y)
print(x % y)
Answer
3.3333333333333335
3
1
- `10 / 3` = `3.3333...` (regular division, always returns a float)
- `10 // 3` = `3` (floor division, drops the decimal)
- `10 % 3` = `1` (modulo: 10 = 3*3 + **1**, the remainder is 1)
Note the slight floating-point imprecision in the first result (ending in 5 instead of 3).
Question 18. What does this code print?
word = "DataScience"
print(word[0])
print(word[-1])
print(word[4:11])
print(word[:4])
Answer
D
e
Science
Data
- `word[0]` is the first character: `'D'`
- `word[-1]` is the last character: `'e'`
- `word[4:11]` is characters at indices 4 through 10: `'Science'`
- `word[:4]` is characters from the beginning through index 3: `'Data'`
Section 5: Code Analysis (2 questions, 8 points each)
These questions ask you to analyze code, identify errors, and predict behavior.
Question 19. The following code has two errors. Identify each error, name the error type Python would raise, and provide the corrected code.
city = "Minneapolis
population = "425,000"
density = population / 54
print(f"The population density of {city} is {density} people per square mile")
Answer
**Error 1 (line 1):** `SyntaxError` — The string `"Minneapolis` is missing its closing quotation mark. Fix: `city = "Minneapolis"` **Error 2 (line 3):** `TypeError` — `population` is a string (`"425,000"`), not a number. You can't divide a string by an integer. Fix: First clean the comma and convert to int: `population = int("425000")` or `population = int(population.replace(",", ""))`, then `density = population / 54`. Corrected code:city = "Minneapolis"
population = 425000
density = population / 54
print(f"The population density of {city} is {density:.0f} people per square mile")
Question 20. Read the following code carefully and predict the exact output (including whether certain lines produce errors). Do not skip any lines.
a = 5
b = 2.0
c = "3"
print(a + b)
print(a * c)
print(type(a + b))
print(a > b and b > 0)
print(c + str(a))
Answer
7.0
33333
<class 'float'>
True
35
Line by line:
- `a + b` → `5 + 2.0` = `7.0` (int + float = float)
- `a * c` → `5 * "3"` = `"33333"` (multiplying a string by an int repeats the string — this is string repetition, not numeric multiplication!)
- `type(a + b)` → `type(7.0)` → `