Skip to content

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:

import math
n = int(input())
print(f"The square root of {n} is {math.sqrt(n):.2f}")

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:   SyntaxError
  • total = 10 / 0   Runtime error (ZeroDivisionError)
  • x=5 (no spaces around =)   ruff style violation
  • print("hello" (missing closing paren)   SyntaxError
  • y = 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" + 5   Runtime error (TypeError)
  • values = [1, 2, 3] followed by print(values[10])   Runtime error (IndexError)
  • while True print("hi")   SyntaxError
  • x = 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:

Sam scored "87.5" points.
"Nice job!"
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:

Item:   socks   Price: $4.00

(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:

"The Great Gatsby"

  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.

a = 9
b = "9"
c = 9.0
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.

x = 5
y = "2"
z = 2.5
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?

word1 = "PYTHON"
word2 = "code"
result = word1[0] + word2[-1] + (word1[1] * 2)
print(result)

  PeYY

2. What will be printed when the following code snippet executes?

first = "XYZ"
second = "ABC"
result = first + second
print(result[1] + result[4])

  YB

3. What will be printed when the following code snippet executes?

word = "hornet"
print(word[0] * 3 + word[-1])

  hhht

Week 3: explaining why an expression errors

1. The following code snippet raises an error.

n = "12"
total = n / 4

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.

word = "hello"
letter = word[10]

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.

value = "3.5"
result = value + 2

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.