Conditions & Loops · Question 17 · Hard
A while loop with an else clause tests whether 91 has a small divisor. What does it print?
n = 91
d = 2
while d * d <= n:
if n % d == 0:
print(d, "divides")
break
d += 1
else:
print("prime")
- 7 divides
- prime
- 13 divides
- 7 divides prime
Answer and explanation
Answer: A. 7 divides
Explanation: The loop tries d = 2, 3, 4, 5, 6 with no success, then finds 91 % 7 == 0, since 91 = 7 × 13. It prints 7 divides and breaks, and because the loop ended with break the else block is skipped.
Previous: break appears inside the inner of two nested loops. Which pairs are collected? · Next: The loop variable is changed inside the body of a for loop. What does this…
Practise conditions & loops
Try a fresh set of conditions & loops questions with an explanation after every answer, free and without signing up: Conditions & Loops practice. Or sit a full placement mock test.
More conditions & loops questions
- When does the else block attached to a for loop run?
- A while loop halves even numbers and applies 3n + 1 to odd numbers until n…
- break appears inside the inner of two nested loops. Which pairs are collected?
- The loop variable is changed inside the body of a for loop. What does this…
- Both conditions of this if-elif contain a division by x, and x is 0. What is…
- continue comes after the increment inside a while loop. What list is built?
More python practice
Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge