Version (2024/2025) Graded A+
How do nested loops work?
✔✔ A loop runs inside another loop, completing all inner iterations before moving to the next
outer iteration.
What is an example of when to use a nested loop?
✔✔ When working with grids, tables, or multidimensional lists.
How do you count the number of iterations a loop makes?
✔✔ Use a counter variable that increments inside the loop.
What happens if you forget to update the loop control variable in a while loop?
✔✔ It creates an infinite loop.
How can you iterate backwards in a for loop?
✔✔ By using a negative step value in the loop declaration.
1
,What is the result of using a loop with a condition that always evaluates to false?
✔✔ The loop never runs.
How do you loop through each character in a string?
✔✔ Use a for loop or a while loop with indexing.
Why is it important to test loops with different inputs?
✔✔ To ensure they work correctly in all cases, including edge cases.
What is the best way to iterate over a dictionary?
✔✔ Use a for loop with `.keys()`, `.values()`, or `.items()`.
Can you modify a list while iterating through it?
✔✔ It's possible, but it can cause errors; using a copy of the list is safer.
What is an off-by-one error in loops?
✔✔ When a loop runs one time too many or one time too few.
2
,How do you stop a loop when a certain condition is met?
✔✔ Use an `if` statement with a `break` inside the loop.
What happens if you use `continue` in a loop?
✔✔ It skips the rest of the current iteration and moves to the next one.
How do you iterate through a range of numbers?
✔✔ Use the `range()` function in a for loop.
Why is it important to use efficient loops?
✔✔ To prevent performance issues and unnecessary computations.
How do you repeat an action multiple times without using a loop?
✔✔ Use recursion, though it is less efficient for simple repetition.
What is iteration in programming?
✔✔ Iteration is the process of repeating a set of instructions until a specific condition is met.
3
, What are the two main types of loops used in programming?
✔✔ For loops and while loops.
How does a while loop work?
✔✔ A while loop continues executing as long as its condition remains true.
What is the purpose of a for loop?
✔✔ A for loop is used to repeat a block of code a fixed number of times.
How can you prevent an infinite loop?
✔✔ Ensure the loop condition will eventually become false.
What is an infinite loop?
✔✔ A loop that never stops because its condition always remains true.
What keyword is used to exit a loop early?
✔✔ The `break` keyword.
4