Exercises
Predict branches and exact boundaries, trace both loop forms, build a summary, repair a stopping error, and account for unavailable or unvisited readings.
Decide and Trace
Q1. Predict a comparison and branch
What does this program display?
Select one choice, then check.
HintCheck equality at the boundary
> does not include equality.
SolutionThe else branch runs
22 > 22 is false, so the program displays not high.
Q2. Test exact interval boundaries
Which condition treats 18 as ordinary and 22 as high?
Select one choice, then check.
HintTest both boundary values
The condition must be true at 18 and false at 22.
SolutionUse a half-open interval
18 <= reading < 22 includes 18 but excludes 22, matching the chapter's
ordinary and high branches.
Q3. Trace a for loop
What is the final value of count?
Compute it first, then check your number.
HintMake three trace rows
Record whether 18, 21, and 20 are each greater than or equal to 20.
SolutionTwo readings are counted
The condition is false for 18 and true for 21 and 20, so the final count is 2.
Summarize and Repair
Q4. Complete total and count
Complete the loop so the program displays total: 82, count: 4, and
mean: 20.5.
Editable Python
Ready to run.
HintCarry state across iterations
Use total += reading and count += 1 inside the loop.
SolutionUpdate both running values
Q5. Repair an infinite loop
Repair the program so it displays 0, 1, and 2, then stops.
Editable Python
Ready to run.
HintChange step inside the loop
Add step = step + 1 after the print instruction.
SolutionAdvance once per iteration
Q6. Repair an off-by-one boundary
The program should display 1, 2, and 3. Which condition repairs it?
Select one choice, then check.
HintInclude the last value
The body must run when step is exactly 3.
SolutionUse less than or equal to
step <= 3 is true for 1, 2, and 3, then false after the update to 4.
Account for Missing and Unvisited Data
Q7. Summarize available readings
Use continue to skip None, then complete the loop so the program displays
mean: 20.5.
Editable Python
Ready to run.
HintHandle absence first
Use if reading is None: continue, then update total and count below that
condition.
SolutionSkip before numerical work
Q8. Choose break, continue, or a condition
A marker means that all later values are invalid and must remain unvisited. Which control states that policy?
Select one choice, then check.
HintDistinguish skip from stop
continue starts the next iteration; the required policy must prevent it.
SolutionBreak at the marker
break ends the nearest loop immediately. Values after the marker are
unvisited and should be reported that way.