CodeHS - AP CSP - Python Midterm (Units 3-7) Easy
AP CSP Units 3–7 Programming Review Quiz
40 Multiple-Choice Questions
Unit 3: Variables, Input, Output
1. What will the following code print?
B. 3
C. 7
D. Error
x = 4
y = x + 3
print(y)
A. 4B. 3
C. 7
D. Error
2. Which of the following is a valid variable name?
A. 3value
B. value_3
C. value-3
D. value 3
A. 3value
B. value_3
C. value-3
D. value 3
3. What is x after the code runs?
B. 14
C. 20
D. 24
x = 10
x = x - 3
x = x * 2
A. 7B. 14
C. 20
D. 24
4. Which expression evaluates to a Boolean?
A. x + 7
B. "hello"
C. x > 10
D. print(x)
A. x + 7
B. "hello"
C. x > 10
D. print(x)
5. What is printed?
B. Alex
C. Hi Alex
D. Error
name = "Alex"
print("Hi " + name)
A. HiB. Alex
C. Hi Alex
D. Error
6. If the user enters 5, what is printed?
B. 5
C. 8
D. 15
num = int(input())
print(num * 3)
A. 3B. 5
C. 8
D. 15
7. Which operator means "remainder"?
A. //
B. /
C. %
D. **
A. //
B. /
C. %
D. **
8. Assignment operator (=) does what?
A. Checks equality
B. Stores a value
C. Repeats code
D. Creates a Boolean
A. Checks equality
B. Stores a value
C. Repeats code
D. Creates a Boolean
Unit 4: Conditionals & Boolean Logic
9. What is printed?
B. small
C. Error
D. Nothing
x = 8
if x > 10:
print("big")
else:
print("small")
A. bigB. small
C. Error
D. Nothing
10. True only when BOTH a and b are True:
A. a or b
B. a == b
C. a and b
D. not a or b
A. a or b
B. a == b
C. a and b
D. not a or b
11. What is printed?
B. B
C. C
D. Nothing
x = 4
if x == 4:
print("A")
elif x > 4:
print("B")
else:
print("C")
A. AB. B
C. C
D. Nothing
12. Equivalent to not (A or B):
A. (not A) or (not B)
B. (not A) and (not B)
C. A and B
D. A != B
A. (not A) or (not B)
B. (not A) and (not B)
C. A and B
D. A != B
13. What is printed?
B. Normal
C. temp
D. Error
temp = 95
if temp > 100 or temp < 32:
print("Extreme")
else:
print("Normal")
A. ExtremeB. Normal
C. temp
D. Error
14. True only when exactly one is True:
A. A and B
B. A != B
C. A == B
D. not A and not B
A. A and B
B. A != B
C. A == B
D. not A and not B
15. What is printed?
B. no
C. 1
D. Error
x = 10
y = 3
if x % y == 1:
print("yes")
else:
print("no")
A. yesB. no
C. 1
D. Error
16. What does this check?
B. age is 13–18 inclusive
C. age is NOT between 13–18
D. age > 18
if age >= 13 and age <= 18:
A. age is 13B. age is 13–18 inclusive
C. age is NOT between 13–18
D. age > 18
Unit 5: Loops (For & While)
17. How many times does this loop run?
B. 5
C. 6
D. Infinite
for i in range(5): print(i) A. 4B. 5
C. 6
D. Infinite
18. What is printed?
B. 1 2
C. 1 2 3
D. 2 3 4
i = 1
while i < 4:
print(i)
i += 1
A. 1B. 1 2
C. 1 2 3
D. 2 3 4
19. What is printed?
B. 2 3 4 5
C. 2 4 6 8 10
D. Error
for i in range(2, 10, 2): print(i) A. 2 4 6 8B. 2 3 4 5
C. 2 4 6 8 10
D. Error
20. What does this loop do?
B. Runs 3 times
C. Infinite loop
D. Error
count = 0
while count < 3:
count += 1
A. Prints numbersB. Runs 3 times
C. Infinite loop
D. Error
21. What is printed?
B. 18
C. 12
D. 24
sum = 0
for n in [3,6,9]:
sum += n
print(sum)
A. 9B. 18
C. 12
D. 24
22. Output?
B. csp
C. C S P
D. c
s
p
for c in "CSP":
print(c.lower())
A. CSPB. csp
C. C S P
D. c
s
p
23. What is printed?
B. 3
C. 1
D. -1
x = 5
while x > 0:
x -= 2
print(x)
A. 5B. 3
C. 1
D. -1
24. Which runs exactly 10 iterations?
A. for i in range(10)
B. for i in range(1,10)
C. while i < 10
D. for i in range(5,15)
A. for i in range(10)
B. for i in range(1,10)
C. while i < 10
D. for i in range(5,15)
25. What does break do?
A. Skip iteration
B. Stop loop
C. Restart loop
D. Ignore errors
A. Skip iteration
B. Stop loop
C. Restart loop
D. Ignore errors
26. What does continue do?
A. Skip to next iteration
B. End loop
C. Pause
D. Restart
A. Skip to next iteration
B. End loop
C. Pause
D. Restart
Unit 6: Lists & Strings
27. What is printed?
B. 20
C. 30
D. Error
nums = [10,20,30]
print(nums[1])
A. 10B. 20
C. 30
D. Error
28. What is printed?
B. 4
C. 5
D. Error
lst = [2,4,6,8]
print(len(lst))
A. 3B. 4
C. 5
D. Error
29. append() does what?
A. Add to front
B. Add to end
C. Remove last
D. Clear list
A. Add to front
B. Add to end
C. Remove last
D. Clear list
30. What is printed?
B. 10 6 18
C. 5
D. 3
nums = [5,3,9]
for n in nums:
print(n*2)
A. 5 3 9B. 10 6 18
C. 5
D. 3
31. What is printed?
B. [2,3]
C. [2,3,4]
D. [3,4]
nums = [1,2,3,4]
print(nums[1:3])
A. [1,2]B. [2,3]
C. [2,3,4]
D. [3,4]
32. What is printed?
B. e
C. l
D. o
s = "hello"
print(s[1])
A. hB. e
C. l
D. o
33. What is printed?
B. ["cat","dog","bird"]
C. ["hamster","dog","bird"]
D. Error
pets = ["cat","dog","bird"]
pets[1] = "hamster"
print(pets)
A. ["cat","hamster","bird"]B. ["cat","dog","bird"]
C. ["hamster","dog","bird"]
D. Error
34. Method that counts substring occurrences?
A. len()
B. count()
C. find()
D. sum()
A. len()
B. count()
C. find()
D. sum()
Unit 7: Functions
35. What does this function print?
B. greet
C. Nothing
D. Error
def greet():
print("Hi")
greet()
A. HiB. greet
C. Nothing
D. Error
36. What is printed?
B. 4
C. 7
D. 12
def add(a,b):
return a+b
print(add(3,4))
A. 3B. 4
C. 7
D. 12
37. Which keyword returns a value?
A. give
B. return
C. stop
D. send
A. give
B. return
C. stop
D. send
38. What is printed?
B. 2
C. 7
D. 10
def f(x):
return x*2
print(f(5))
A. 5B. 2
C. 7
D. 10
39. What is printed?
B. 12
C. 34
D. Error
def area(w,h):
return w*h
print(area(3,4))
A. 7B. 12
C. 34
D. Error
40. Parameters allow functions to:
A. Be integers only
B. Receive input
C. Never change
D. Be optional only
A. Be integers only
B. Receive input
C. Never change
D. Be optional only
Answer Key & Explanations
1. C – 4 + 3 = 7.
2. B – variable names cannot start with numbers or contain spaces/hyphens.
3. C – (10 – 3) × 2 = 14 → 20.
4. C – Comparisons produce Booleans.
5. C – String concatenation.
6. D – 5 × 3 = 15.
7. C – % is remainder.
8. B – Assignment stores values.
9. B – 8 is not > 10.
10. C – AND requires both true.
11. A – First condition is true.
12. B – De Morgan’s Law.
13. B – 95 is normal range.
14. B – XOR condition.
15. B – 10 % 3 = 1 → condition false.
16. B – Inclusive range.
17. B – range(5) → 0–4.
18. C – Prints 1, 2, 3.
19. A – Even numbers 2–8.
20. B – Loop runs 3 times.
21. B – 3 + 6 + 9 = 18.
22. D – Each letter printed on its own line.
23. D – 5→3→1→-1.
24. D – 5–14 = 10 iterations.
25. B – break stops loop entirely.
26. A – continue skips to next iteration.
27. B – Index 1 = 20.
28. B – Four elements.
29. B – Append adds to end.
30. B – Doubles each number.
31. B – Slice 1–2.
32. B – s[1] = e.
33. A – Replaces middle element.
34. B – count() counts occurrences.
35. A – Prints Hi.
36. C – 3+4 = 7.
37. B – return keyword.
38. D – 5*2 = 10.
39. B – 3×4 = 12.
40. B – Parameters → input.