CodeHS - AP CSP - Python Midterm (Units 3-7) Medium Difficulty
AP CSP Units 3–7 Medium Difficulty Quiz
Auto-Graded • 40 Questions
Unit 3: Variables, Expressions, Tracing
1. What is printed?
B. 8
C. 9
D. 11
x = 3
y = x * 2
x = y - 1
print(x + y)
A. 7B. 8
C. 9
D. 11
2. What is the value of result?
B. 3
C. 4
D. 6
a = 4
b = 7
result = (a + b) // 3 + b % a
A. 2B. 3
C. 4
D. 6
3. Which evaluates to True?
A. "5" + 2 == 7
B. 5 > 3 and not (2 > 3)
C. 5 == "5"
D. not(5 > 2 or 1 < 3)
A. "5" + 2 == 7
B. 5 > 3 and not (2 > 3)
C. 5 == "5"
D. not(5 > 2 or 1 < 3)
4. What does the following print?
B. 4
C. 5
D. 6
x = 12
print(x % 5 + x // 4)
A. 3B. 4
C. 5
D. 6
5. Which expression is equivalent to: “x is NOT between 10 and 20 inclusive”? A. not (10 <= x <= 20)
B. x < 10 and x > 20
C. x < 10 or x > 20
D. x <= 10 or x >= 20
B. x < 10 and x > 20
C. x < 10 or x > 20
D. x <= 10 or x >= 20
Unit 4: Conditionals
6. What is printed?
B. B
C. C
D. Nothing
x = 5
if x % 2 == 1:
if x > 3:
print("A")
else:
print("B")
else:
print("C")
A. AB. B
C. C
D. Nothing
7. Which Boolean is True only when x and y are different?
A. x == y
B. x != y
C. x and y
D. not x and not y
A. x == y
B. x != y
C. x and y
D. not x and not y
8. What is printed?
B. low
C. Error
D. Nothing
a = 10
b = 4
if a / b > 2:
print("high")
else:
print("low")
A. highB. low
C. Error
D. Nothing
9. What is printed?
B. B
C. C
D. Nothing
x = 8
y = 3
if x % y == 2:
print("A")
elif x % y == 1:
print("B")
else:
print("C")
A. AB. B
C. C
D. Nothing
10. Which is equivalent to NOT (A and B)? A. (not A) or (not B)
B. (not A) and (not B)
C. A or B
D. A != B
B. (not A) and (not B)
C. A or B
D. A != B
Unit 5: Loops
11. What is printed?
B. 12
C. 15
D. 21
sum = 0
for i in range(1, 6):
sum += i
print(sum)
A. 10B. 12
C. 15
D. 21
12. How many times does this loop run?
B. 2
C. 3
D. 4
count = 10
while count > 3:
count -= 3
A. 1B. 2
C. 3
D. 4
13. What is printed?
B. 0 1 2 3 4 5
C. 0 1 2 2 3 4
D. 0 1 1 2 2 3
for i in range(3):
for j in range(2):
print(i + j)
A. 0 1 1 2 2 3B. 0 1 2 3 4 5
C. 0 1 2 2 3 4
D. 0 1 1 2 2 3
14. What is printed?
B. 9
C. 11
D. 13
x = 7
while x % 3 != 0:
x += 2
print(x)
A. 7B. 9
C. 11
D. 13
15. What is printed?
B. 20
C. 24
D. 32
total = 1
for i in [2,3,4]:
total *= i
print(total)
A. 18B. 20
C. 24
D. 32
16. What happens?
B. Prints nothing
C. Infinite loop
D. Error
i = 0
while i < 5:
print(i)
A. Prints 0–4B. Prints nothing
C. Infinite loop
D. Error
17. What is printed?
B. 1 4 7
C. 1 4 7 10
D. 3 6 9
for i in range(1,10,3):
print(i)
A. 1 3 5 7 9B. 1 4 7
C. 1 4 7 10
D. 3 6 9
18. What is printed?
B. 4
C. 0
D. -4
x = 20
while x >= 10:
x -= 4
print(x)
A. 8B. 4
C. 0
D. -4
Unit 6: Lists and Strings
19. What is printed?
B. 5
C. 8
D. 14
nums = [2, 5, 8, 11, 14]
print(nums[nums[1]])
A. 2B. 5
C. 8
D. 14
20. What is the list after execution?
B. [1,2,99,4]
C. [1,99,3,4]
D. Error
a = [1,2,3,4]
b = a
b[2] = 99
A. [1,2,3,4]B. [1,2,99,4]
C. [1,99,3,4]
D. Error
21. What is printed?
B. 20
C. 30
D. 40
lst = [10,20,30,40]
print(lst[-2])
A. 10B. 20
C. 30
D. 40
22. What is printed?
B. ['b','d']
C. ['a','c']
D. ['c','e']
items = ["a","b","c","d","e"]
print(items[1:4:2])
A. ['b','c']B. ['b','d']
C. ['a','c']
D. ['c','e']
23. What is printed?
B. PROCK
C. PROC
D. SROC
s = "CSPROCKS"
print(s[2:6])
A. CSPROB. PROCK
C. PROC
D. SROC
24. What is printed?
B. 2
C. 3
D. 4
pets = ["cat","dog","bird","fish"]
print(len(pets) - pets.index("bird"))
A. 1B. 2
C. 3
D. 4
25. Which list contains only elements at even indexes?
B. data[2::2]
C. data[::2]
D. data[::-1]
data = [10,20,30,40,50] A. data[1::2]B. data[2::2]
C. data[::2]
D. data[::-1]
26. What is printed?
B. [3,7,11]
C. [3,6,12]
D. [4,7,10]
lst = [3,6,9]
for i in range(len(lst)):
lst[i] += i
print(lst)
A. [3,6,9]B. [3,7,11]
C. [3,6,12]
D. [4,7,10]
Unit 7: Functions
27. What is printed?
B. 6
C. 7
D. 8
def f(x): return x + 2
print(f(f(3)))
A. 5B. 6
C. 7
D. 8
28. What is printed?
B. 5
C. 8
D. 11
def mystery(a,b):
if a > b:
return a - b
return b - a
print(mystery(8,3))
A. 3B. 5
C. 8
D. 11
29. What is printed?
B. 2
C. 3
D. 6
def foo(n):
if n % 2 == 0:
return n // 2
return n * 3
print(foo(foo(4)))
A. 1B. 2
C. 3
D. 6
30. What is printed?
B. 9
C. 12
D. 15
def g(x,y): return x*y - x
print(g(3,4))
A. 6B. 9
C. 12
D. 15
31. What is printed?
B. 6
C. 8
D. 10
def h(a): return a + 1
def k(a): return h(a) * 2
print(k(3))
A. 4B. 6
C. 8
D. 10
32. What is printed?
B. 6
C. 7
D. Error
def f(x):
x += 1
return x
a = 5
f(a)
print(a)
A. 5B. 6
C. 7
D. Error
33. What is printed?
B. 4
C. 16
D. 256
def m(x): return x * x
print(m(m(2)))
A. 2B. 4
C. 16
D. 256
34. What is printed?
B. 3212
C. 31212
D. 312
def combine(a,b):
return str(a) + str(b)
print(combine(3, combine(1,2)))
A. 312B. 3212
C. 31212
D. 312
35. What is printed?
B. 20
C. 24
D. 9
def calc(a,b,c): return a + b * c
print(calc(2,3,4))
A. 14B. 20
C. 24
D. 9
36. What is printed?
B. 1
C. 2
D. 3
def fun(n): return n % 4
print(fun(fun(fun(10))))
A. 0B. 1
C. 2
D. 3
37. What is returned?
B. False
C. 1
D. Error
def foo(a,b):
if a > b:
return True
return False
print(foo(5,1))
A. TrueB. False
C. 1
D. Error
38. What is printed?
B. 10
C. 12
D. 14
def p(x): return x - 1
def q(x): return p(x) * 2
print(q(6))
A. 5B. 10
C. 12
D. 14
39. What is printed?
B. 4
C. 6
D. 7
def f(n):
if n == 0:
return 1
return n + f(n-1)
print(f(3))
A. 3B. 4
C. 6
D. 7
40. What is printed?
B. 5
C. 6
D. 7
def r(a,b): return (a + b) // 2
print(r(7,4))
A. 4B. 5
C. 6
D. 7