wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

DHS AP CSP Midterm Review

Total questions: 100

Worksheet time: 50mins

Name
Class
Date
1.
Which function reads a line of user input as a string in Python?
a)
input()
b)
read()
c)
scan()
d)
gets()
2.
What does `print('A', 'B')` output by default?
a)
AB
b)
A B
c)
A, B
d)
['A','B']
3.
Which is a syntax error in Python?
a)
print('hi')
b)
x = 3
c)
if True print('ok')
d)
a_b = 1
4.
What does `type(3.0)` return?
a)
<class 'int'>
b)
<class 'float'>
c)
<class 'str'>
d)
<class 'bool'>
5.
Which is a valid Python comment?
a)
// comment
b)
# comment
c)
/* comment */
d)
<!-- comment -->
6.
Which statement is true about indentation in Python?
a)
Optional for style only
b)
Defines code blocks
c)
Only required in functions
d)
Only required in loops
7.
What happens with `print('5' + '6')`?
a)
11
b)
56
c)
TypeError
d)
None
8.
Which call will print exactly `Hello\nWorld` (two lines)?
a)
print('Hello\nWorld')
b)
print('Hello/World')
c)
print('Hello n World')
d)
print('Hello\World')
9.
Which built-in converts the string '42' to an integer?
a)
str('42')
b)
int('42')
c)
float(42)
d)
bool('42')
10.
Which expression is valid Python?
a)
print 'hi'
b)
print('hi')
c)
echo('hi')
d)
say('hi')
11.
What is the result of `7 // 2`?
a)
3
b)
3.5
c)
4
d)
Error
12.
Which operator has the highest precedence?
a)
*
b)
+
c)
==
d)
and
13.
After `x = 5; x += 2`, what is `x`?
a)
5
b)
7
c)
2
d)
Error
14.
What is the value of `3 ** 2`?
a)
5
b)
6
c)
9
d)
8
15.
Which describes a variable name in Python?
a)
Can start with a digit
b)
Case-sensitive
c)
Can include spaces
d)
Must be all caps
16.
What is the type of `True`?
a)
int
b)
str
c)
bool
d)
float
17.
What is the value of `5 % 2`?
a)
2
b)
1
c)
0
d)
2.5
18.
Which statement reassigns a variable correctly?
a)
5 = x
b)
x == 5
c)
x = 5
d)
set x to 5;
19.
What is the result of `int(3.9)`?
a)
4
b)
3.9
c)
3
d)
Error
20.
Which expression evaluates to a float?
a)
5 / 2
b)
5 // 2
c)
5 % 2
d)
5 ** 2
21.
What is `'abc' * 3`?
a)
'abcabcabc'
b)
'abc3'
c)
'abccc'
d)
Error
22.
What is `'Python'[1]`?
a)
'P'
b)
'y'
c)
't'
d)
IndexError
23.
What does `'Hi ' + 'there'` evaluate to?
a)
'Hi there'
b)
'Hi there'
c)
'Hi, there'
d)
Error
24.
What is `'abc'.upper()`?
a)
'ABC'
b)
'Abc'
c)
['a','b','c']
d)
Error
25.
What does `len('hello')` return?
a)
4
b)
5
c)
6
d)
Error
26.
What is `'hello'.find('l')`?
a)
1
b)
2
c)
3
d)
-1
27.
Evaluate `'a' in 'banana'`
a)
True
b)
False
c)
TypeError
d)
None
28.
What is `'bananas'.count('an')`?
a)
1
b)
2
c)
3
d)
0
29.
What is `' hi '.strip()`?
a)
' hi '
b)
'hi'
c)
' hi '
d)
' hi'
30.
What is `'a,b,c'.split(',')`?
a)
['a','b','c']
b)
('a','b','c')
c)
['a,b,c']
d)
['a','bc']
31.
What does `if x and not y:` mean?
a)
x is False and y is True
b)
x is True and y is False
c)
Either x or y is True
d)
Both x and y are False
32.
Which operator has lower precedence than `==`?
a)
and
b)
*
c)
**
d)
%
33.
Which is the correct structure of an if-elif-else chain?
a)
if ...; elif ...; else ...
b)
if ...: elif ...: else ...:
c)
if ...: elif ...: else ...
d)
if ... then ... else ...
34.
What prints? x=5 if x>5: print('A') elif x==5: print('B') else: print('C')
a)
A
b)
B
c)
C
d)
None
35.
Which expression is True for even numbers n?
a)
n % 2 == 1
b)
n // 2 == 0
c)
n % 2 == 0
d)
n / 2 == 0
36.
What is the value of `not (True and False)`?
a)
True
b)
False
c)
None
d)
Error
37.
Fill the blank to check range 1..10 inclusive: `if 1 <= x ____ 10:`
a)
<
b)
<=
c)
==
d)
and
38.
Which is a valid ternary expression?
a)
x if cond else y
b)
if cond then x else y
c)
cond ? x : y
d)
x else y if cond
39.
What is printed? x=False y=True if x or y: print('go') else: print('stop')
a)
go
b)
stop
c)
True
d)
False
40.
Which evaluates to False?
a)
0
b)
'0'
c)
[0]
d)
1
41.
What does `range(3)` produce when iterated?
a)
0,1,2
b)
1,2,3
c)
0,1,2,3
d)
1,2
42.
Which loop sums numbers 1..5 correctly?
a)
s=0; for n in range(1,6): s+=n
b)
s=0; for n in range(5): s+=n
c)
s=0; while n<5: s+=n
d)
s=0; for n in [1,5]: s+=n
43.
What is printed? for ch in 'hi': print(ch)
a)
hi
b)
h i
c)
h↵i
d)
['h','i']
44.
Which statement immediately jumps to the next loop iteration?
a)
break
b)
continue
c)
return
d)
pass
45.
Which loop will execute at least once?
a)
for-loop
b)
while-loop
c)
Both guaranteed
d)
Neither guaranteed
46.
What is the final value of s? s=0 for i in range(1,4): s+=i
a)
3
b)
6
c)
4
d)
1
47.
Fill the blank to iterate over indices of list L: `for i in ____:`
a)
L.indexes()
b)
range(L)
c)
range(len(L))
d)
len(range(L))
48.
Which while-loop sums numbers until n becomes 0?
a)
s=0; while n: s+=n; n-=1
b)
s=0; while n>0: s+=n; n+=1
c)
s=0; while True: s+=n
d)
s=0; while n<0: s+=n
49.
What is printed? for i in range(2): for j in range(2): print(i,j)
a)
(0,0)(1,1)
b)
(0,0)(0,1)(1,0)(1,1)
c)
(0,1)(1,0)
d)
(0,0)(1,0)(0,1)(1,1)
50.
Which best describes `for x in iterable:`?
a)
Iterates values from an iterable
b)
Iterates only integers
c)
Iterates indices only
d)
Iterates key-value pairs
51.
What does `break` do in a loop?
a)
Skips to next iteration
b)
Exits the loop immediately
c)
Restarts the loop
d)
Raises an error
52.
Complete to sum even numbers 0..10 inclusive: `sum(n for n in range(0,11) if ____ )`
a)
n%2
b)
n%2==1
c)
n%2==0
d)
n//2==0
53.
What is printed? n=3 while n>0: n-=1 print(n)
a)
3
b)
2
c)
1
d)
0
54.
Which loop correctly iterates over a list `L` and prints elements?
a)
for i in range(L): print(i)
b)
for x in L: print(x)
c)
for L in x: print(L)
d)
for print in L: x
55.
Which comprehension builds squares of 0..4?
a)
[i**2 for i in range(5)]
b)
[i^2 for i in range(5)]
c)
(i**2 in range(5))
d)
{i**2}(range(5))
56.
Which keyword starts a function definition?
a)
func
b)
def
c)
lambda
d)
define
57.
What is returned by a function with no return statement?
a)
0
b)
False
c)
None
d)
''
58.
What prints? def add(a,b=2): return a+b print(add(3))
a)
3
b)
5
c)
None
d)
Error
59.
Which call uses keyword arguments? `def f(x,y): ...`
a)
f(3,4)
b)
f(x=3,y=4)
c)
f()
d)
f(xy=3)
60.
Local variables in a function are:
a)
Visible globally
b)
Persist after return
c)
Scoped to that function call
d)
Stored in a file
61.
What prints? def echo(s): print(s) print(echo('hi'))
a)
hi
b)
None
c)
hi then None
d)
Error
62.
Which header allows an optional `sep` parameter defaulting to '-' after required `a` and `b`?
a)
def g(sep='-',a,b):
b)
def g(a,b,sep='-'):
c)
def g(a='-',b,sep):
d)
def g(a,b,'-'=sep):
63.
What is printed? x=1 def f(): x=9 return x print(f(),x)
a)
9 9
b)
9 1
c)
1 9
d)
Error
64.
Why prefer returning a value over printing in helper functions?
a)
Faster typing
b)
Better for unit testing/reuse
c)
Print adds type safety
d)
Printing is required
65.
Which is the best pattern to avoid a shared default list?
a)
def add(v,L=[]): L.append(v); return L
b)
def add(v,L=None): if L is None: L=[] L.append(v); return L
c)
def add(v,L=list()): return v
d)
def add(v,L={}): return L
66.
What is printed? def step(n,by=3): return n+by print(step(5,by=2))
a)
7
b)
8
c)
10
d)
Error
67.
Which is True about parameters vs arguments?
a)
Parameters are values; arguments are names
b)
Same thing
c)
Parameters are names; arguments are values
d)
Arguments are defined in the header
68.
Which call will cause an error? `def area(w,h): return w*h`
a)
area(2,3)
b)
area(w=2,h=3)
c)
area(h=3,w=2)
d)
area(w=2,3)
69.
What is printed? def classify(n): if n<0: return 'neg' elif n==0: return 'zero' print('pos') print(classify(5))
a)
pos
b)
zero
c)
neg
d)
None
70.
Refactor guidance: keeping logic pure (no I/O) makes functions:
a)
Harder to test
b)
Easier to reuse and test
c)
Slower
d)
Impossible to call
71.
What is printed? L=[1,2,3] L.append(4) print(L)
a)
[1,2,3,4]
b)
[1,2,3]
c)
[4,1,2,3]
d)
Error
72.
Which returns the last element of list L safely if len(L) >= 1?
a)
L[len(L)]
b)
L[-1]
c)
L[len(L)-2]
d)
L[0:][-2]
73.
What is printed? L=[0,1,2,3] print(L[1:3])
a)
[1,2]
b)
[0,1,2]
c)
[2,3]
d)
[1,2,3]
74.
Which builds a list of even numbers 0..8?
a)
[i for i in range(10) if i%2==0]
b)
[i%2 for i in range(10)]
c)
list(range(1,10,2))
d)
[i for i in [0,8] if i]
75.
Which mutates the original list L?
a)
L + [1]
b)
L.extend([1])
c)
L*2
d)
L + L
76.
What is printed? L=[3,1,2] L.sort() print(L)
a)
[3,1,2]
b)
[1,2,3]
c)
[2,1,3]
d)
Error
77.
Which removes the first occurrence of value 2 from L?
a)
L.pop(2)
b)
del L[2]
c)
L.remove(2)
d)
L.discard(2)
78.
What is printed? L=[1,2] M=L M.append(3) print(L)
a)
[1,2]
b)
[1,2,3]
c)
Error
d)
None
79.
Which creates a shallow copy of list L?
a)
M=L
b)
M=L.copy()
c)
M=list; M()
d)
M=copy(L, shallow=True)
80.
Which expression flattens one level: `[[1],[2,3]] -> [1,2,3]`?
a)
sum([[1],[2,3]])
b)
[x for sub in [[1],[2,3]] for x in sub]
c)
list([[1],[2,3]])
d)
flatten([[1],[2,3]])
81.
What is `len([[]])`?
a)
0
b)
1
c)
2
d)
Error
82.
Which is True about lists?
a)
Immutable and hashable
b)
Mutable sequences
c)
Always sorted
d)
Cannot contain other lists
83.
Which slice returns all but the first element of L?
a)
L[1:]
b)
L[:-1]
c)
L[:1]
d)
L[::1]
84.
Which returns a new sorted list from L without altering L?
a)
L.sort()
b)
sorted(L)
c)
L = L.sort()
d)
L.sorted()
85.
Which best describes list comprehension?
a)
Faster always than loops
b)
A concise syntax to build lists from iterables
c)
Only for numeric data
d)
Returns a generator
86.
What is printed? D={'a':1,'b':2} print(D['a'])
a)
'a'
b)
1
c)
2
d)
KeyError
87.
Which adds a new key 'x' with value 10 to dict D?
a)
D.add('x',10)
b)
D['x']=10
c)
D.append(('x',10))
d)
D.insert('x',10)
88.
Which gets a default when key is missing without raising?
a)
D['missing']
b)
D.get('missing',0)
c)
D.pop('missing')
d)
D.missing
89.
What is printed? S=set([1,2,2,3]) print(S) (order ignored)
a)
{1,2,2,3}
b)
{1,2,3}
c)
[1,2,3]
d)
(1,2,3)
90.
Which checks membership efficiently in a large collection?
a)
list
b)
tuple
c)
set
d)
str
91.
Which creates an immutable sequence?
a)
[1,2]
b)
{1,2}
c)
(1,2)
d)
set((1,2))
92.
Which operation removes duplicates from a list `L` (order not guaranteed)?
a)
tuple(L)
b)
list(set(L))
c)
sorted(L)
d)
L.unique()
93.
Which loop iterates keys and values of dict D?
a)
for k in D.keys():
b)
for v in D.values():
c)
for k,v in D.items():
d)
for item in D:print(item.value)
94.
What is printed? T=(1,2,3) print(T[1])
a)
1
b)
2
c)
3
d)
Error
95.
Which removes and returns an arbitrary element from set S?
a)
S.remove()
b)
S.pop()
c)
S.shift()
d)
S.discard()
96.
Which creates a dictionary from two lists `K=['a','b']`, `V=[1,2]`?
a)
dict(zip(K,V))
b)
dict(K,V)
c)
{K:V}
d)
map(dict,K,V)
97.
What is printed? D={'x':1} D['x']+=2 print(D['x'])
a)
1
b)
2
c)
3
d)
Error
98.
Which statement about sets is True?
a)
Sets preserve insertion order
b)
Sets allow duplicates
c)
Set elements must be hashable
d)
Sets are indexed by position
99.
Which merges two dicts D1 and D2 into a new dict (Py 3.9+)?
a)
D1 + D2
b)
dict(D1,D2)
c)
D1 | D2
d)
merge(D1,D2)
100.
Which expression builds a set of squares from 1..3?
a)
{1,4,9}
b)
set(i*i for i in range(1,4))
c)
[i*i for i in range(1,4)]
d)
(1,4,9)