Unit 2: Conditional and Iterative Statements - Practice Quiz

INT108 — Python Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is the value of 17 % 5 in Python?

Modulus operator Easy
A. 3
B. 2
C. 5
D. 4

2 Which Python module is commonly used to generate random numbers?

Random numbers Easy
A. choice
B. math
C. numbers
D. random

3 What type of value does the expression 8 > 3 produce?

Boolean expressions Easy
A. Boolean
B. Float
C. String
D. Integer

4 What is the result of True and False in Python?

Logic operators Easy
A. False
B. True
C. 0
D. None

5 Which keyword begins a basic conditional statement in Python?

Conditional statements Easy
A. if
B. when
C. condition
D. check

6 Which keyword provides an alternative when an if condition is false?

Conditional statements Easy
A. except
B. alternative
C. else
D. otherwise

7 What is a nested conditional?

Nested conditionals Easy
A. A loop inside a function
B. An if statement inside another if statement
C. A variable inside a list
D. A comment inside code

8 When does a Python while loop continue running?

While statements Easy
A. Until the program starts
B. While its condition is false
C. Only once
D. While its condition is true

9 What does for x in range(3): do?

For loop statements Easy
A. Runs three times
B. Runs two times
C. Runs one time
D. Runs four times

10 What values are produced by range(2, 5)?

For loop statements Easy
A. 2, 3, 4
B. 2, 3, 4, 5
C. 3, 4, 5
D. 1, 2, 3, 4

11 What is a nested for loop?

Nested for loops Easy
A. A for loop inside another for loop
B. A while loop inside an if statement
C. A function with two parameters
D. A for loop without a condition

12 What is a nested while loop?

Nested while loops Easy
A. A while loop with no condition
B. A for loop inside a function
C. A while loop inside another while loop
D. Two conditions on one line

13 What is a common purpose of generating a random number inside a loop?

Random numbers in loops Easy
A. To convert all values to strings
B. To create changing values on each repetition
C. To stop every loop immediately
D. To remove the loop condition

14 What does encapsulation generally mean in programming?

Encapsulation Easy
A. Comparing two values
B. Bundling data and methods together
C. Choosing a random number
D. Repeating a block forever

15 What is the main idea of generalization in programming?

Generalization Easy
A. Making code useful for many cases
B. Running only one example
C. Removing all conditions
D. Adding more random values

16 Which expression checks whether number is even?

Modulus operator Easy
A. number / 2 == 0
B. number % 2 == 0
C. number % 2 == 1
D. number * 2 == 0

17 What values can random.randint(1, 3) return?

Random numbers Easy
A. 1, 2, or 3
B. 0, 1, or 2
C. 2 or 3 only
D. 1 or 2 only

18 What is the result of 5 == 5?

Boolean expressions Easy
A. True
B. None
C. 5
D. False

19 Which operator is true when at least one of two conditions is true?

Logic operators Easy
A. in
B. and
C. or
D. not

20 Which keyword checks another condition after an if condition is false?

Conditional statements Easy
A. else
B. elif
C. again
D. then

21 A clock shows 10:45. The following code calculates the minute value after adding 40 minutes:

PYTHON
minute = 45
new_minute = (minute + 40) % 60
print(new_minute)



What is printed?

Modulus operator Medium
A. 85
B. 5
C. 25
D. 65

22 What does the following code print?

PYTHON
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')

Modulus operator Medium
A. B
B. C
C. D
D. A

23 Which value can be produced by the following expression?

PYTHON
random.randrange(2, 10, 2)

Random numbers Medium
A. 8
B. 10
C. 12
D. 5

24 What is printed by this code?

PYTHON
x = 5
y = 2
print(x > 3 == y)

Boolean expressions Medium
A. True
B. False
C. An error is raised
D. 2

25 What is the value of the following expression?

PYTHON
p = False
q = True
r = False
p or q and not r

Logic operators Medium
A. False
B. True
C. None
D. 0

26 What grade is printed?

PYTHON
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)

Conditional statements Medium
A. C
B. D
C. B
D. A

27 What does the following code print?

PYTHON
x = 12

if x > 0:
    if x % 2 == 0:
        if x % 3 == 0:
            print('A')
        else:
            print('B')
    else:
        print('C')
else:
    print('D')

Nested conditionals Medium
A. C
B. B
C. A
D. D

28 What is printed by this code?

PYTHON
n = 19
total = 0

while n > 0:
    total += n % 10
    n //= 10

print(total)

While statements Medium
A. 18
B. 10
C. 19
D. 9

29 What value is printed?

PYTHON
i = 0
total = 0

while i < 6:
    i += 1
    if i % 2 == 0:
        continue
    total += i

print(total)

While statements Medium
A. 15
B. 6
C. 12
D. 9

30 What does the following code print?

PYTHON
total = 0
for n in range(2, 11, 3):
    total += n
print(total)

For loop statements Medium
A. 15
B. 12
C. 21
D. 18

31 What is the final value of result?

PYTHON
result = 0
for x in [1, 2, 3, 4]:
    if x % 2 == 0:
        result += x * x
    else:
        result -= x
print(result)

For loop statements Medium
A. 12
B. 14
C. 18
D. 16

32 What does this nested loop print?

PYTHON
count = 0
for i in range(1, 4):
    for j in range(1, i + 1):
        if (i + j) % 2 == 0:
            count += 1
print(count)

Nested for loops Medium
A. 6
B. 3
C. 4
D. 5

33 What is the value of rows after this code runs?

PYTHON
rows = []
for i in range(3):
    row = ''
    for j in range(i, 3):
        row += str(j)
    rows.append(row)

Nested for loops Medium
A. ['012', '12', '2']
B. ['0', '12', '012']
C. ['012', '01', '0']
D. ['012', '012', '012']

34 What value is printed by the nested while loops?

PYTHON
i = 1
total = 0

while i <= 3:
    j = 1
    while j <= i:
        total += i * j
        j += 1
    i += 1

print(total)

Nested while loops Medium
A. 18
B. 27
C. 21
D. 25

35 Assume the five calls to random.randint(1, 3) return 3, 1, 3, 2, 3 in that order. What is printed?

PYTHON
import random
hits = 0

for _ in range(5):
    if random.randint(1, 3) == 3:
        hits += 1

print(hits)

Random numbers in loops Medium
A. 4
B. 3
C. 2
D. 5

36 What is printed by this function, which encapsulates the limiting logic?

PYTHON
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))

Encapsulation Medium
A. 0 10 15
B. -2 7 15
C. -2 10 10
D. 0 7 10

37 What happens when this code is executed?

PYTHON
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)

Encapsulation Medium
A. It raises NameError
B. It prints 2
C. It prints 3
D. It prints 0

38 The function below generalizes repeated processing by accepting an operation as an argument. What is printed?

PYTHON
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))

Generalization Medium
A. 8
B. 6
C. 10
D. 12

39 What is printed by this generalized data-processing function?

PYTHON
def process(values, transform):
    total = 0
    for item in values:
        total += transform(item)
    return total

print(process([1, 2, 3], lambda x: x * x))

Generalization Medium
A. 18
B. 14
C. 6
D. 9

40 Assume successive calls to random.randint(1, 6) return 2, 2, 1, 6. What is printed?

PYTHON
import random
rolls = 0
score = 0

while True:
    rolls += 1
    value = random.randint(1, 6)
    if value == 6:
        break
    score += value

print(rolls, score)

Random numbers in loops Medium
A. 4 11
B. 3 5
C. 5 6
D. 4 5

41 What is printed by the following Python code?

print(-17 % 5, 17 % -5, -17 % -5)

Modulus operator Hard
A. 3 2 3
B. -2 -3 3
C. 3 -3 -2
D. -2 2 -2

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?

Modulus operator Hard
A. 0
B. 2
C. 1
D. 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?

Random numbers Hard
A.
B.
C.
D.

44 What is the exact output of this code?

def f(v):
print(v, end='')
return v

print(f(3) < f(2) < f(1))

Boolean expressions Hard
A. 32False
B. 321False
C. 321True
D. 32True

45 What value is assigned to result?

result = [] or 0 or 'x' and '' or 7

Logic operators Hard
A. 'x'
B. 0
C. 7
D. ''

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='')

Conditional statements Hard
A. A
B. AB
C. AC
D. AD

47 Consider the code below. Under which condition is no function called?

if a:
if b:
first()
elif c:
second()
else:
third()

Nested conditionals Hard
A. a is true, while b and c are false
B. a and c are false, while b is true
C. a is false, while b and c are true
D. a and b are true, while c is false

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)

While statements Hard
A. -1 2
B. -1 1
C. 8 1
D. 7 2

49 What is the final value printed?

i = 0
while i < 4:
i += 1
if i % 2 == 0:
continue
i *= 2
print(i)

While statements Hard
A. 8
B. 4
C. 5
D. 6

50 What is the value of total after this statement?

total = sum(range(10, -3, -4))

For loop statements Hard
A. 16
B. 18
C. 14
D. 20

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)

For loop statements Hard
A. [1, 2, 3, 4, 5, 7, 9]
B. [1, 2, 3, 4, 7, 9]
C. [1, 2, 3, 4, 5, 7]
D. [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)

Nested for loops Hard
A. 4
B. 6
C. 5
D. 3

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

Nested for loops Hard
A. 6
B. 4
C. 5
D. 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)

Nested while loops Hard
A. 8
B. 11
C. 10
D. 9

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

Random numbers in loops Hard
A. 6
B. 4
C. 3
D. 2

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?

Random numbers in loops Hard
A. 0.0816
B. 0.1024
C. 0.1600
D. 0.1280

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]))

Encapsulation Hard
A. 2 1 4
B. 2 3 4
C. 2 1 1
D. 2 3 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)

Generalization Hard
A. 4
B. 10
C. 6
D. 2

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)

Boolean expressions Hard
A. It prints False without raising an exception
B. It raises ZeroDivisionError before printing
C. It raises TypeError before printing
D. It prints 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')

Conditional statements Hard
A. B
B. A
C. A ValueError is raised
D. C