Python Fundamentals · Practice set 7 of 8
Control Flow and Patterns: 10 practice questions
10 free Python Fundamentals practice questions on Control Flow and Patterns, with an explanation for every answer. Untimed. The full mock exam and the timed version are in the app.
-
Question 1 of 10
Which statement about an `if` chain is correct?
- AIt may have zero or more `elif` clauses, and `else` is optional.
- BIt must have exactly one `elif` clause and one `else` clause.
- CIt may have several `else` clauses but cannot contain `elif`.
- DIt must end with `elif`, even when no alternative path is needed.
Show the answer
An `if` chain can express only its first condition or add any number of ordered alternatives, with an optional final fallback.
Next → 1 / 10 -
Question 2 of 10
What does `break` terminate?
- AThe innermost enclosing `for` or `while` loop.
- BThe nearest `if` statement without ending its loop.
- CEvery active loop in the current program.
- DOnly the current iteration of the nearest loop.
Show the answer
`break` exits one enclosing loop; `continue` is the nearby statement that advances only to the next iteration.
Next → 2 / 10 -
Question 3 of 10
Why would a developer place `pass` in an otherwise empty function body?
- AIt satisfies the required statement position while performing no action.
- BIt exits the function and returns an empty string.
- CIt skips directly to the next iteration of an enclosing loop.
- DIt returns `True` while postponing the rest of the function.
Show the answer
`pass` is a no-operation placeholder for a syntactically required body; it is distinct from loop control and return behavior.
Next → 3 / 10 -
Question 4 of 10
How does a Python `for` statement traverse a list or string?
- AIt visits the sequence's items in the order they appear.
- BIt visits numeric indices in reverse order unless given a step.
- CIt constructs an arithmetic progression from the sequence length.
- DIt repeatedly tests a condition without obtaining sequence items.
Show the answer
A `for` loop consumes items from a sequence directly; `range()` is the separate tool for an integer progression.
Next → 4 / 10 -
Question 5 of 10
Which values are produced by `range(0, 10, 3)`?
- A`0, 3, 6, 9`
- B`3, 6, 9, 12`
- C`0, 1, 2, 3`
- D`0, 3, 6, 9, 10`
Show the answer
The progression begins at zero, advances by three, and excludes the stop value ten.
Next → 5 / 10 -
Keep the ones you got wrong
In the app, every question you miss comes back exactly when you’re about to forget it.
-
Question 6 of 10
What happens when a loop executes `continue`?
- AThe loop's `else` clause executes immediately.
- BThe loop proceeds with its next iteration.
- CEvery enclosing loop terminates immediately.
- DThe nearest loop terminates immediately.
Show the answer
`continue` rejects the remainder of one iteration but preserves the loop; `break` is the terminating contrast.
Next → 6 / 10 -
Question 7 of 10
When does an `else` clause attached to a `while` loop execute?
- AImmediately after any `continue` skips an iteration.
- BAfter the condition becomes false, provided no `break` ended the loop.
- CAfter every iteration whose condition was true.
- DOnly after a `break` terminates the loop early.
Show the answer
A loop `else` marks normal completion without `break`; for `while`, normal completion occurs when its condition becomes false.
Next → 7 / 10 -
Question 8 of 10
A nested search must stop checking candidates for the current record when it finds a match, but it must continue processing later records. Where should control exit?
- AUse `pass` in the inner candidate loop so both loops advance immediately.
- BUse `continue` in the inner candidate loop so candidate checking terminates.
- CUse `break` in the inner candidate loop so the outer record loop continues.
- DUse `break` in the outer record loop so the inner candidate loop continues.
Show the answer
Because `break` targets the innermost enclosing loop, placing it in the candidate loop ends only that search and leaves outer iteration available.
Next → 8 / 10 -
Question 9 of 10
A search should report `not found` only after examining every item, but must suppress that report when a match ends the search early. Which structure expresses both conditions?
- APlace the report after the matching `if` and use `continue` when a match is found.
- BPlace the report in the loop's `else` clause and use `return` after every nonmatch.
- CPlace the report in the loop's `else` clause and use `break` when a match is found.
- DPlace the report before the loop and use `break` when the first item is examined.
Show the answer
The loop `else` runs after exhaustion without `break`, so a matching `break` cleanly separates found from not-found completion.
Next → 9 / 10 -
Question 10 of 10
A routing rule receives a dictionary that may contain extra keys. It must capture `bandwidth` and `latency`, accept the case only when latency is below a threshold, and otherwise try the next case. Which pattern design fits?
- AUse two dotted constant patterns so the values are assigned before any case is selected.
- BUse a mapping pattern for both keys and `break` when the latency check is false.
- CUse a mapping pattern for both keys with an `if` guard on the captured latency.
- DUse a sequence pattern for both keys and `_` as a guard that rejects extra dictionary keys.
Show the answer
Mapping patterns capture requested dictionary values while ignoring extra keys, and a false guard continues matching at the next case.
Next → 10 / 10 -
You’ve finished this set
That’s 10 questions on Control Flow and Patterns. In the app the ones you miss come back exactly when you’re about to forget them.
The whole course, on your phone
Lessons you can read, audio you can listen to on the way to work, and practice that remembers what you got wrong.