Conditions & Loops questions and answers

if/elif, for, while, range, break, continue and loop else. Practise conditions & loops questions from the python section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample conditions & loops questions with worked answers

Question 1

Which statement skips the rest of the current iteration and moves on to the next iteration of the loop?

  1. continue
  2. break
  3. pass
  4. return

Answer: A. continue

Explanation: continue jumps straight to the next iteration. break ends the whole loop, pass does nothing at all, and return leaves the enclosing function.

Question 2

When does the else block attached to a for loop run?

  1. Only when the loop body is never executed
  2. Only when the loop is ended by break
  3. When the loop finishes all its iterations without executing break
  4. Once after every iteration of the loop

Answer: C. When the loop finishes all its iterations without executing break

Explanation: A loop's else block runs when the loop ends normally, that is, the sequence is exhausted with no break. It also runs for an empty sequence, but that is not the only case. If break executes, the else block is skipped.

Question 3

break appears inside the inner of two nested loops. Which pairs are collected?

out = []
for i in range(3):
    for j in range(3):
        if j == i:
            break
        out.append((i, j))
print(out)
  1. []
  2. [(1, 0), (2, 0), (2, 1)]
  3. [(1, 0)]
  4. [(0, 1), (0, 2), (1, 2)]

Answer: B. [(1, 0), (2, 0), (2, 1)]

Explanation: break ends only the inner loop; the outer loop carries on with the next i. For i = 0 the inner loop breaks at once, for i = 1 it adds (1, 0) and breaks at j = 1, and for i = 2 it adds (2, 0) and (2, 1) before breaking.

All 20 conditions & loops questions with answers

Conditions & Loops: common questions

What are conditions & loops questions?

if/elif, for, while, range, break, continue and loop else. They appear in the python section of many placement tests.

Where can I practise conditions & loops questions with answers for free?

Right here on LogicWiz Exams. Every conditions & loops question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise conditions & loops?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of conditions & loops questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Python practice.

More python topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep