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?
- continue
- break
- pass
- 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?
- Only when the loop body is never executed
- Only when the loop is ended by break
- When the loop finishes all its iterations without executing break
- 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, 0), (2, 0), (2, 1)]
- [(1, 0)]
- [(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
- An if-elif ladder assigns a grade for 68 marks. What does it print?
- A for loop prints the numbers produced by range(3) on one line. What is the…
- A loop accumulates the numbers from range(1, 6). What total is printed?
- Which statement skips the rest of the current iteration and moves on to the…
- A while loop keeps subtracting 2 from n. What is n when the loop ends?
- range() is given a negative step. What list does this print?
- A loop over the letters of a word stops at the letter p. What is printed?
- A for loop that searches for an even number has an else clause. What does this…
- continue is used inside a loop over range(1, 8). Which numbers are printed?
- enumerate() numbers a list of names starting from 1. What line is printed?
- zip() pairs two lists of unequal length inside a loop. What total is printed?
- The inner loop's range depends on the outer loop variable. How many times does…
- A helper function prints its argument each time it is called inside and / or…
- 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?
- A while loop with an else clause tests whether 91 has a small divisor. What…
- 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?
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
- Variables, Types & Operators: Dynamic typing, numbers, operators, precedence and truthiness.
- Python Strings: Indexing, slicing, methods, formatting and immutability.
- Lists & Tuples: Slicing, methods, copies, sorting and unpacking.
- Dictionaries & Sets: Lookups, iteration, hashable keys and set operations.
- Functions & Scope: Arguments, defaults, *args, closures, lambda and recursion.
- Comprehensions & Generators: List, dict and set comprehensions, yield and lazy evaluation.
- Classes & Objects: Attributes, inheritance, super(), dunder methods and properties.
- Exceptions & Files: try/except/else/finally, raising errors and the with statement.
- Built-ins & Standard Library: sorted, zip, any/all, collections, itertools and imports.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- SQL: SQL quizzes by topic, from SELECT and joins to window functions: query output and concepts for analyst and developer roles.
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