Chapter 5 Quiz: Loops and Iteration

Python for Business for Beginners — Chapter 5

Answer all questions before checking the answer key at the bottom. Some questions have a single correct answer; others ask you to choose the best answer from the options given.


Questions


Question 1

What is the output of the following code?

total = 0
for value in [10, 20, 30, 40]:
    total += value
print(total)

A) 10 B) 40 C) 100 D) [10, 20, 30, 40]


Question 2

Which of the following is a correct for loop that prints each item in the list ["Northeast", "Southeast", "Midwest"]?

A)

for i in 3:
    print(i)

B)

for region in ["Northeast", "Southeast", "Midwest"]:
    print(region)

C)

for ["Northeast", "Southeast", "Midwest"]:
    print(region)

D)

loop region in ["Northeast", "Southeast", "Midwest"]:
    print(region)

Question 3

What does range(2, 10, 2) produce?

A) The integers 2, 4, 6, 8, 10 B) The integers 2, 4, 6, 8 C) The integers 0, 2, 4, 6, 8, 10 D) The integers 2, 3, 4, 5, 6, 7, 8, 9


Question 4

Priya writes the following loop to process invoice records. What is the purpose of the continue statement?

for invoice in invoices:
    if invoice["status"] == "draft":
        continue
    total += invoice["amount"]

A) To stop the loop entirely when a draft invoice is encountered B) To skip the current invoice and move to the next one in the list C) To delete the draft invoice from the list D) To repeat the current invoice's processing a second time


Question 5

What is the output of this code?

numbers = [5, 12, 3, 19, 8, 25]
for n in numbers:
    if n > 15:
        break
    print(n)

A) 5 12 3 19 B) 5 12 3 C) 19 25 D) 5 12 3 19 8 25


Question 6

Which code correctly uses enumerate() to print a numbered list starting at 1?

A)

for i, item in enumerate(items):
    print(f"{i}. {item}")

B)

for i, item in enumerate(items, start=1):
    print(f"{i}. {item}")

C)

for i in range(1, len(items)):
    print(f"{i}. {items[i]}")

D)

for item in enumerate(items, 1):
    print(f"{item}.")

Question 7

What does the following list comprehension produce?

sales = [8_500, 2_000, 34_000, 950, 12_500]
large_sales = [s for s in sales if s > 5_000]

A) [8500, 34000, 12500] B) [8500, 2000, 34000, 950, 12500] C) [2000, 950] D) A syntax error


Question 8

What is the purpose of zip() in the following code?

regions = ["Northeast", "Southeast", "Midwest"]
targets = [150_000, 100_000, 115_000]

for region, target in zip(regions, targets):
    print(f"{region}: ${target:,}")

A) To add targets to the end of regions B) To combine the two lists into a list of tuples so both can be iterated simultaneously C) To sort the two lists together D) To compress the two lists into a single flat list


Question 9

Priya initializes highest_sales = 0 before a loop and updates it with if sales > highest_sales: highest_sales = sales inside the loop. This is an example of which pattern?

A) List comprehension B) Guard clause C) Accumulator / "find the maximum" pattern D) Sentinel value pattern


Question 10

What is wrong with the following while loop?

balance = 5_000
while balance > 0:
    print(f"Balance: ${balance}")

A) while loops cannot be used with numeric conditions B) The variable balance is never modified inside the loop, so it will run forever C) The condition should be while balance >= 0 D) Nothing is wrong with it


Question 11

Which of the following correctly uses zip(*data) to calculate the sum of each column in a 2D list?

data = [
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90],
]

A)

column_sums = [sum(col) for col in data]

B)

column_sums = [sum(col) for col in zip(*data)]

C)

column_sums = [sum(row) for row in data]

D)

column_sums = zip(data)

Question 12

What does pass do inside a loop body?

A) Pauses execution for one second B) Skips to the next iteration, like continue C) Does nothing — it is a syntactic placeholder D) Exits the loop, like break


Question 13

Maya writes a loop to process her client list and wants to stop processing after she has found the client with the highest outstanding balance. Which keyword should she use?

A) pass B) return C) continue D) break


Question 14

What is the output of the following code?

totals = {}
entries = [("Northeast", 12_000), ("Southeast", 8_000), ("Northeast", 5_000)]

for region, amount in entries:
    if region not in totals:
        totals[region] = 0
    totals[region] += amount

print(totals)

A) {'Northeast': 12000, 'Southeast': 8000} B) {'Northeast': 17000, 'Southeast': 8000} C) {'Northeast': 5000, 'Southeast': 8000} D) A KeyError


Question 15

Which of the following list comprehensions correctly converts each sales figure from USD to EUR at a 0.92 conversion rate?

usd_amounts = [1_000, 2_500, 800, 4_200]

A) [amount for amount in usd_amounts * 0.92] B) [amount * 0.92 for amount in usd_amounts] C) [amount for amount in usd_amounts if 0.92] D) [usd_amounts * 0.92]


Question 16

Sandra asks Priya to find how many invoices exceed $10,000. Which is the most Pythonic way to count them from a list of invoice amounts?

A)

count = 0
for amount in invoice_amounts:
    if amount > 10_000:
        count = count + 1

B)

count = sum(1 for amount in invoice_amounts if amount > 10_000)

C)

count = len([a for a in invoice_amounts if a > 10_000])

D) All of the above produce the correct result


Question 17

What does the inner loop print on its second execution (when region is "Southeast")?

regions = ["Northeast", "Southeast"]
quarters = ["Q1", "Q2"]

for region in regions:
    for quarter in quarters:
        print(f"{region}-{quarter}")

A) Northeast-Q1 and Northeast-Q2 B) Southeast-Q1 and Southeast-Q2 C) Southeast-Q1 only D) Southeast-Q2 only


Question 18

Priya wants to generate a report line for every entry in sales_data but skip any entry where amount is zero. She should use:

A) break when amount == 0 B) pass when amount == 0 C) continue when amount == 0 D) return when amount == 0


Question 19

What is the primary risk of a nested loop when working with large business datasets?

A) Nested loops cannot access variables from the outer loop B) Nested loops always produce incorrect results C) The number of iterations multiplies (outer × inner), which can make execution very slow for large datasets D) Python does not support more than two levels of nesting


Question 20

Which of the following is the best use case for a while loop rather than a for loop?

A) Printing every item in a customer list B) Applying the same discount to all products in an inventory list C) Continuing to prompt a user for input until they enter a valid value D) Calculating the commission for each sales rep in a fixed list


Answer Key

Q Answer Explanation
1 C The accumulator starts at 0 and adds 10 + 20 + 30 + 40 = 100.
2 B The correct for loop syntax is for variable in sequence:. Option A tries to iterate over the integer 3 (not valid). Options C and D have syntax errors.
3 B range(start, stop, step) generates start, start+step, ... up to but NOT including stop. range(2, 10, 2) = 2, 4, 6, 8.
4 B continue skips the rest of the current loop body and moves to the next iteration. It does not stop the loop.
5 B The loop prints 5, 12, 3 — then hits 19, which is > 15, so break exits the loop. 19 is not printed.
6 B enumerate(items, start=1) begins counting from 1. Option A starts from 0 (the default). Option C has an off-by-one error (misses index 0).
7 A The comprehension keeps only values greater than 5,000: 8,500, 34,000, and 12,500.
8 B zip() combines two (or more) sequences into an iterator of tuples, allowing both sequences to be iterated in parallel.
9 C This is the "find the maximum" variant of the accumulator pattern — a running maximum rather than a running sum.
10 B balance is never modified inside the loop, so balance > 0 is always True and the loop runs forever (an infinite loop).
11 B zip(*data) transposes the 2D list, grouping items by column. Then sum(col) adds up each column. Option A sums rows (inner lists), not columns.
12 C pass is a no-op. It satisfies Python's syntactic requirement for a non-empty block but does nothing. Unlike continue, it does not skip to the next iteration.
13 D break exits the loop immediately. Once Maya has found the client with the highest balance, she uses break to stop searching.
14 B The Northeast entries are accumulated: 12,000 + 5,000 = 17,000. Southeast is 8,000. The result is {'Northeast': 17000, 'Southeast': 8000}.
15 B The correct syntax is [expression for variable in iterable]. amount * 0.92 is the expression applied to each item.
16 D All three options produce the correct count. Option B (generator expression with sum) is generally considered the most idiomatic Python, but all three are valid. In a real codebase, clarity matters more than which idiom you choose.
17 B When region = "Southeast", the inner loop iterates over ["Q1", "Q2"], printing Southeast-Q1 then Southeast-Q2.
18 C continue skips the rest of the current loop body and moves to the next entry without stopping the loop. break would stop the entire loop.
19 C With a nested loop, the total iterations = len(outer) × len(inner). 1,000 outer × 1,000 inner = 1,000,000 iterations, which can be very slow for large datasets.
20 C A while loop is the right choice when you do not know in advance how many iterations you need — like prompting until valid input is received. Options A, B, and D have a fixed, countable collection to iterate over, which suits a for loop.

Score Interpretation

  • 18–20 correct: You have a solid command of loops and iteration. Move on to Chapter 6 with confidence.
  • 14–17 correct: Good understanding with a few gaps. Review the sections corresponding to the questions you missed, then try the Tier 3 exercises.
  • 10–13 correct: Revisit Sections 5.2 through 5.6 and work through Tier 1 and Tier 2 exercises before proceeding.
  • Below 10: Loop concepts need more practice. Re-read the chapter, type out the code examples by hand (do not copy-paste), and run each one to see the output.