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. 4
B. 5
C. 3
D. 2

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

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

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

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

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

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

5 Which keyword begins a basic conditional statement in Python?

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

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

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

7 What is a nested conditional?

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

8 When does a Python while loop continue running?

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

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

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

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

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

11 What is a nested for loop?

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

12 What is a nested while loop?

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

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

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

14 What does encapsulation generally mean in programming?

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

15 What is the main idea of generalization in programming?

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

16 Which expression checks whether number is even?

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

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

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

18 What is the result of 5 == 5?

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

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

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

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

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

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. 5
B. 25
C. 85
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. A
B. C
C. D
D. B

23 Which value can be produced by the following expression?

PYTHON
random.randrange(2, 10, 2)

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

24 What is printed by this code?

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

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

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. 0
D. None

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. B
C. D
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. A
B. B
C. D
D. C

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. 9
C. 19
D. 10

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. 6
B. 12
C. 15
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. 21
C. 12
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. 16
C. 14
D. 18

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. 5
D. 4

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', '012', '012']
B. ['012', '12', '2']
C. ['012', '01', '0']
D. ['0', '12', '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. 27
B. 18
C. 25
D. 21

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. 2
C. 3
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. -2 10 10
B. 0 7 10
C. -2 7 15
D. 0 10 15

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 prints 0
B. It raises NameError
C. It prints 2
D. It prints 3

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. 6
B. 12
C. 10
D. 8

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. 14
B. 18
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. 5 6
B. 3 5
C. 4 11
D. 4 5

41 What is printed by the following Python code?

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

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

Modulus operator Hard
A. 0
B. 1
C. 2
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. 32True
B. 321True
C. 321False
D. 32False

45 What value is assigned to result?

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

Logic operators Hard
A. ''
B. 0
C. 7
D. '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='')

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

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 and b are true, while c is false
B. a is true, while b and c are false
C. a is false, while b and c are true
D. 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)

While statements Hard
A. -1 1
B. 7 2
C. -1 2
D. 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)

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

50 What is the value of total after this statement?

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

For loop statements Hard
A. 20
B. 18
C. 16
D. 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)

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. 6
B. 3
C. 5
D. 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

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

Random numbers in loops Hard
A. 4
B. 2
C. 6
D. 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?

Random numbers in loops Hard
A. 0.1600
B. 0.1280
C. 0.0816
D. 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]))

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

Generalization Hard
A. 10
B. 2
C. 6
D. 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)

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