Unit 1 Review Questions
Questions are grouped by learning objective. Each objective from Weeks 2–3 has several questions of varying difficulty for practice.
Week 2: input/output and format matching
1. Given the following program:
import math
side = float(input())
area = math.pi * side ** 2
print(f"A circle with radius {side} has area {area:.1f}")
If the user enters 4, write exactly what the program prints, matching spacing and punctuation precisely.
A circle with radius 4.0 has area 50.3
2. Given the following program:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"{name} will turn {age + 1} next year.")
If the user enters Maria and then 19, write exactly what is printed to the screen (including the prompts).
Enter your name: Maria Enter your age: 19 Maria will turn 20 next year.
3. Given the following program:
If the user enters 10, write exactly what the program prints.
The square root of 10 is 3.16
Week 2: telling apart error types
1. For each snippet below, state whether it is a SyntaxError (caught before the code runs), a runtime error such as ZeroDivisionError (happens while the code runs), a ruff style violation, or produces no error at all.
2x = 10SyntaxError (a name cannot start with a digit)total = 10 / 0Runtime error (ZeroDivisionError)x=5(no spaces around=) ruff style violationprint("hello"(missing closing paren) SyntaxErrory = int("abc")Runtime error (ValueError)
2. For each snippet below, state whether it is a SyntaxError, a runtime error, a ruff style violation, or produces no error at all.
payRate = 12.50ruff style violation (variable names should be snake_case, as inpay_rate)result = "5" + 5Runtime error (TypeError)print(total), wheretotalwas never assigned a value Runtime error (NameError)print("hello world)(missing closing quote) SyntaxErrorx = 5;y = 10(two statements on one line, unnecessary semicolon) ruff style violationtotal = 10 // 3No error at all — the program runs andtotalbecomes3.
3. A student writes import math at the top of a file but never uses it anywhere else in the program. Is this a SyntaxError, a runtime error, a ruff style violation, or none of these? Explain briefly.
ruff style violation — an unused import doesn't stop the program from running, but style checkers flag it as dead code.
Week 2: writing a statement to a required format
1. Given name = "Sam" and score = 87.5, write a single print() statement using an f-string that produces exactly the following two lines of output, including the literal double quotes around the score:
print(f"{name} scored \"{score}\" points.\n\"Nice job!\"")
2. Given item = "socks" and price = 4, write a single print() statement using an f-string that produces exactly:
(There is a tab character between each field, and the price is formatted with two decimal places.)
print(f"Item:\t{item}\tPrice: ${price:.2f}")
3. Given dash = "-" and total = 1234.5, write a single print() statement using an f-string that produces exactly the following two lines, where the first line is twenty dashes and the second shows the total with a thousands separator and two decimal places:
print(f"{dash * 20}\nTotal: ${total:,.2f}")
Week 3: predicting expression type and value
1. Assuming that the following assignments have occurred, determine the type (int, float, str, or Error) and value of the result of each expression, or determine if the expression would result in an error.
| Expression | Type | Value |
|---|---|---|
a + c |
float | 18.0 |
a // 2 |
int | 4 |
b + b |
str | "99" |
a + b |
Error | — |
b * 3 |
str | "999" |
c ** 2 |
float | 81.0 |
2. Assuming that the following assignments have occurred, determine the type (int, float, str, or Error) and value of the result of each expression, or determine if the expression would result in an error.
| Expression | Type | Value |
|---|---|---|
x / z |
float | 2.0 |
x % 2 |
int | 1 |
y * x |
str | "22222" |
x - y |
Error | — |
x ** y |
Error | — |
str(x) + y |
str | "52" |
3. Determine the type (int, float, str, or Error) and value of each expression below, or determine if the expression would result in an error.
Unlike the two questions above, these expressions use literal values rather than variables.
| Expression | Type | Value |
|---|---|---|
8 / 4 |
float | 2.0 |
8 // 3 |
int | 2 |
8 % 3 |
int | 2 |
8.0 % 3 |
float | 2.0 |
2 ** 5 |
int | 32 |
2 ** 0.5 |
float | 1.4142135623730951 |
"7" * 2 |
str | "77" |
"7" + 2 |
Error | — |
"cs" + "149" |
str | "cs149" |
"cs149"[2] |
str | "1" |
"cs149"[-1] |
str | "9" |
"cs149"[5] |
Error | — |
Week 3: string concatenation, repetition, indexing
1. What will be printed when the following code snippet executes?
PeYY
2. What will be printed when the following code snippet executes?
YB
3. What will be printed when the following code snippet executes?
hhht
Week 3: explaining why an expression errors
1. The following code snippet raises an error.
Name the error type (for example, TypeError, ValueError, IndexError, or ZeroDivisionError) and explain, in one sentence, why it occurs.
TypeError — the / operator cannot be used between a str and an int; n would need to be converted with int(n) first.
2. The following code snippet raises an error.
Name the error type (for example, TypeError, ValueError, IndexError, or ZeroDivisionError) and explain, in one sentence, why it occurs.
IndexError — word only has indices 0 through 4, so index 10 is out of range.
3. The following code snippet raises an error.
Name the error type (for example, TypeError, ValueError, IndexError, or ZeroDivisionError) and explain, in one sentence, why it occurs.
TypeError — you cannot add a str and an int directly; value would need to be converted with float(value) first.