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.
if x = 5:SyntaxErrortotal = 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.
def greet( name ):ruff style violation (extra spaces inside parentheses)result = "5" + 5Runtime error (TypeError)values = [1, 2, 3]followed byprint(values[10])Runtime error (IndexError)while True print("hi")SyntaxErrorx = 5;y = 10(statement on one line, unnecessary semicolon) ruff style violation
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 title = "the great gatsby", write a single statement that prints the title in title case, surrounded by quotation marks, exactly as:
print(f""{title.title()}"")
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" |
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.