Unit 2: Conditional and Iterative Statements - Practice Quiz
1
What is the value of 17 % 5 in Python?
2 Which Python module is commonly used to generate random numbers?
3
What type of value does the expression 8 > 3 produce?
4
What is the result of True and False in Python?
5 Which keyword begins a basic conditional statement in Python?
6
Which keyword provides an alternative when an if condition is false?
7 What is a nested conditional?
if statement inside another if statement
8
When does a Python while loop continue running?
9
What does for x in range(3): do?
10
What values are produced by range(2, 5)?
11
What is a nested for loop?
for loop without a condition
while loop inside an if statement
for loop inside another for loop
12
What is a nested while loop?
while loop inside another while loop
for loop inside a function
while loop with no condition
13 What is a common purpose of generating a random number inside a loop?
14 What does encapsulation generally mean in programming?
15 What is the main idea of generalization in programming?
16
Which expression checks whether number is even?
number % 2 == 1
number * 2 == 0
number % 2 == 0
number / 2 == 0
17
What values can random.randint(1, 3) return?
18
What is the result of 5 == 5?
19 Which operator is true when at least one of two conditions is true?
not
in
or
and
20
Which keyword checks another condition after an if condition is false?
elif
then
again
else
21
A clock shows 10:45. The following code calculates the minute value after adding 40 minutes:
minute = 45
new_minute = (minute + 40) % 60
print(new_minute)
What is printed?
22
What does the following code print?
n = 84
if n % 6 == 0 and n % 8 == 0:
print('A')
elif n % 6 == 0:
print('B')
elif n % 8 == 0:
print('C')
else:
print('D')
23
Which value can be produced by the following expression?
random.randrange(2, 10, 2)
24
What is printed by this code?
x = 5
y = 2
print(x > 3 == y)
25
What is the value of the following expression?
p = False
q = True
r = False
p or q and not r
26
What grade is printed?
score = 78
projects = 3
if score >= 90:
grade = 'A'
elif score >= 75 and projects >= 3:
grade = 'B'
elif score >= 75:
grade = 'C'
else:
grade = 'D'
print(grade)
27
What does the following code print?
x = 12
if x > 0:
if x % 2 == 0:
if x % 3 == 0:
print('A')
else:
print('B')
else:
print('C')
else:
print('D')
28
What is printed by this code?
n = 19
total = 0
while n > 0:
total += n % 10
n //= 10
print(total)
29
What value is printed?
i = 0
total = 0
while i < 6:
i += 1
if i % 2 == 0:
continue
total += i
print(total)
30
What does the following code print?
total = 0
for n in range(2, 11, 3):
total += n
print(total)
31
What is the final value of result?
result = 0
for x in [1, 2, 3, 4]:
if x % 2 == 0:
result += x * x
else:
result -= x
print(result)
32
What does this nested loop print?
count = 0
for i in range(1, 4):
for j in range(1, i + 1):
if (i + j) % 2 == 0:
count += 1
print(count)
33
What is the value of rows after this code runs?
rows = []
for i in range(3):
row = ''
for j in range(i, 3):
row += str(j)
rows.append(row)
['012', '012', '012']
['012', '12', '2']
['012', '01', '0']
['0', '12', '012']
34
What value is printed by the nested while loops?
i = 1
total = 0
while i <= 3:
j = 1
while j <= i:
total += i * j
j += 1
i += 1
print(total)
35
Assume the five calls to random.randint(1, 3) return 3, 1, 3, 2, 3 in that order. What is printed?
import random
hits = 0
for _ in range(5):
if random.randint(1, 3) == 3:
hits += 1
print(hits)
36
What is printed by this function, which encapsulates the limiting logic?
def clamp(value, low=0, high=10):
if value < low:
return low
if value > high:
return high
return value
print(clamp(-2), clamp(7), clamp(15))
-2 10 10
0 7 10
-2 7 15
0 10 15
37
What happens when this code is executed?
def count_even(values):
count = 0
for value in values:
if value % 2 == 0:
count += 1
return count
result = count_even([2, 5, 8])
print(count)
0
NameError
2
3
38
The function below generalizes repeated processing by accepting an operation as an argument. What is printed?
def apply_n_times(value, n, operation):
for _ in range(n):
value = operation(value)
return value
print(apply_n_times(2, 3, lambda x: x + 2))
39
What is printed by this generalized data-processing function?
def process(values, transform):
total = 0
for item in values:
total += transform(item)
return total
print(process([1, 2, 3], lambda x: x * x))
40
Assume successive calls to random.randint(1, 6) return 2, 2, 1, 6. What is printed?
import random
rolls = 0
score = 0
while True:
rolls += 1
value = random.randint(1, 6)
if value == 6:
break
score += value
print(rolls, score)
5 6
3 5
4 11
4 5
41
What is printed by the following Python code?
print(-17 % 5, 17 % -5, -17 % -5)
-2 -3 3
3 -3 -2
-2 2 -2
3 2 3
42
A circular buffer has length 7. Starting at index 2, the index is updated five times using index = (index - 3) % 7. What is the final index?
0
1
2
6
43
random.randrange(2, 15, 3) is called independently four times. What is the probability that the first three results are not 11 and the fourth result is 11?
44
What is the exact output of this code?
def f(v):
print(v, end='')
return v
print(f(3) < f(2) < f(1))
32True
321True
321False
32False
45
What value is assigned to result?
result = [] or 0 or 'x' and '' or 7
''
0
7
'x'
46
What is printed by the following code?
x = 3
if x > 2:
print('A', end='')
if x > 3:
print('B', end='')
elif x > 1:
print('C', end='')
else:
print('D', end='')
AB
A
AD
AC
47
Consider the code below. Under which condition is no function called?
if a:
if b:
first()
elif c:
second()
else:
third()
a and b are true, while c is false
a is true, while b and c are false
a is false, while b and c are true
a and c are false, while b is true
48
What is printed by this loop?
n = 10
s = 0
while n > 1:
n //= 2
s += n
if n == 2:
break
else:
s = -1
print(s, n)
-1 1
7 2
-1 2
8 1
49
What is the final value printed?
i = 0
while i < 4:
i += 1
if i % 2 == 0:
continue
i *= 2
print(i)
4
5
8
6
50
What is the value of total after this statement?
total = sum(range(10, -3, -4))
20
18
16
14
51
What list is printed?
values = [1, 2, 3, 4]
for x in values:
if x % 2:
values.append(x + 4)
if x > 4:
break
print(values)
[1, 2, 3, 4, 5, 7, 9]
[1, 2, 3, 4, 7, 9]
[1, 2, 3, 4, 5, 7]
[1, 2, 3, 4, 5, 9]
52
What is printed by this code?
count = 0
for i in range(4):
for j in range(4):
if i + j == 3:
break
if j % 2 == 0:
count += 1
print(count)
6
3
5
4
53
What is the final value of count?
count = 0
for i in range(3):
for j in range(3):
if i == j:
continue
if i + j > 2:
break
count += 1
else:
count += 1
5
4
6
7
54
What is printed by these nested loops?
i = 0
total = 0
while i < 3:
j = 3
while j > i:
j -= 1
if j == 1:
continue
total += i + j
i += 1
print(total)
10
9
8
11
55
The following loop uses independent random results. What is the expected number of calls to random.randint?
calls = 0
while True:
calls += 1
if random.randint(1, 6) in (1, 6):
break
4
2
6
3
56
A loop repeatedly calls random.random() until the result is less than 0.2. Assuming independent uniform results in , what is the probability that the loop stops on exactly the fourth call?
0.1600
0.1280
0.0816
0.1024
57
What is printed by this code, which encapsulates state in closures?
def make_counter():
count = 0
def add(values):
nonlocal count
for _ in values:
count += 1
return count
return add
a = make_counter()
b = make_counter()
print(a([1, 2]), a([3]), b([4]))
2 3 1
2 3 4
2 1 4
2 1 1
58
What does this generalized accumulation function return?
def accumulate(start, stop, step, keep):
total = 0
for x in range(start, stop, step):
if keep(x):
total += x
return total
result = accumulate(8, -5, -3, lambda x: x % 2 == 0)
10
2
6
4
59
What happens when this code is executed?
data = [2, 4, 0, 8]
result = all(x and 8 % x == 0 for x in data)
print(result)
TypeError before printing
False without raising an exception
ZeroDivisionError before printing
True without raising an exception
60
What is printed by the following code?
x = 0
if x != 0 and 10 / x > 1:
print('A')
elif x == 0 or int('invalid'):
print('B')
else:
print('C')
B
A
ValueError is raised
C
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →