break, continue & the loop-else — Concept Check
Questions on break, continue & the loop-else. break exits a loop, continue skips to the next iteration, and Python's unusual for...else runs the else block only when the loop finished WITHOUT break — perfect for search loops.
Questions in this quiz
7 questions on break, continue & the loop-else — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In a loop checking for a specific number in a list, what happens if the number is found and the loop contains a 'break'?
- AThe loop executes an else block after finding the number.
- BThe loop exits immediately after finding the number.
- CThe loop skips the current iteration and continues.
- DThe loop continues to check all elements.
2.In Python, what does the 'else' block in a for loop do?
- AExecutes only if the loop completes without a 'break'.
- BExecutes only if the loop contains a 'break'.
- CExecutes if the loop contains a 'continue'.
- DRuns after every loop iteration.
3.A loop iterates over a list of names looking for 'admin'. If 'admin' is not found, what executes if a for-else construct is used?
- AThe else block executes.
- BThe loop throws an error.
- CThe loop restarts.
- DA 'break' statement is triggered.
4.What is the primary advantage of using a for-else construct in a search loop?
- AIt automatically skips invalid entries.
- BIt speeds up the loop execution.
- CIt eliminates the need for a found-flag variable.
- DIt allows multiple breaks in a loop.
5.In a loop processing a list of numbers, what does 'continue' do when a non-digit is encountered?
- AProcesses the non-digit as zero.
- BSkips the current iteration and moves to the next.
- CExits the loop entirely.
- DExecutes the else block.
6.What happens if a 'break' statement is never executed in a for loop with an else clause?
- AThe else block executes after the loop completes.
- BThe loop executes indefinitely.
- CThe else block is skipped.
- DThe loop throws an exception.
7.A loop checks if a number is prime using a for-else construct. What happens if a divisor is found?
- AThe loop exits with no output.
- BThe loop breaks and 'not prime' is printed.
- CThe else block prints the number is prime.
- DThe loop continues to check other divisors.