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")
  1. 7 divides
  2. prime
  3. 13 divides
  4. 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

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